Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array rotate juggling
(version: 0)
Two implementations of array rotatee
Comparing performance of:
Recursive Small vs Recursive Medium vs Recursive Long vs Direct Small vs Direct Medium vs Direct Long vs Juggling small vs Juggling medium vs Juggling large
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arraySmall = new Array(12).map((v,i)=>i); var arrayMedium = new Array(1200).map((v,i)=>i); var arrayLong = new Array(120000).map((v,i)=>i);
Tests:
Recursive Small
const rotateArrayRecursive = (numberRotations, array) => { const n = Math.abs(numberRotations); if (n === 0) { return [...array]; } else if (n === 1) { return [array[array.length], ...array.slice(0,array.length-1) ]; } else { return rotateArrayRecursive(1, rotateArrayRecursive(n - 1, array)); } } rotateArrayRecursive(4,arraySmall);
Recursive Medium
const rotateArrayRecursive = (numberRotations, array) => { const n = Math.abs(numberRotations); if (n === 0) { return [...array]; } else if (n === 1) { return [array[array.length], ...array.slice(0,array.length-1) ]; } else { return rotateArrayRecursive(1, rotateArrayRecursive(n - 1, array)); } } rotateArrayRecursive(456,arrayMedium);
Recursive Long
const rotateArrayRecursive = (numberRotations, array) => { const n = Math.abs(numberRotations); if (n === 0) { return [...array]; } else if (n === 1) { return [array[array.length], ...array.slice(0,array.length-1) ]; } else { return rotateArrayRecursive(1, rotateArrayRecursive(n - 1, array)); } } rotateArrayRecursive(45678,arrayLong);
Direct Small
const rotateArrayDirect = (desiredRotation, array) => { // Size of the array, needed later on const arrayLength = array.length; // Ensure we rotate by the right amount, whatever the desiredRotation value // This formula is needed because JS % modulo operator returns negative output for negative inputs const effectiveRotation = ((desiredRotation % arrayLength) + 2 * arrayLength) % arrayLength; // Cut the array in two parts and merge them return [ ...array.slice(effectiveRotation, arrayLength), ...array.slice(0, effectiveRotation) ]; } rotateArrayDirect(4,arraySmall);
Direct Medium
const rotateArrayDirect = (desiredRotation, array) => { // Size of the array, needed later on const arrayLength = array.length; // Ensure we rotate by the right amount, whatever the desiredRotation value // This formula is needed because JS % modulo operator returns negative output for negative inputs const effectiveRotation = ((desiredRotation % arrayLength) + 2 * arrayLength) % arrayLength; // Cut the array in two parts and merge them return [ ...array.slice(effectiveRotation, arrayLength), ...array.slice(0, effectiveRotation) ]; } rotateArrayDirect(456,arrayMedium);
Direct Long
const rotateArrayDirect = (desiredRotation, array) => { // Size of the array, needed later on const arrayLength = array.length; // Ensure we rotate by the right amount, whatever the desiredRotation value // This formula is needed because JS % modulo operator returns negative output for negative inputs const effectiveRotation = ((desiredRotation % arrayLength) + 2 * arrayLength) % arrayLength; // Cut the array in two parts and merge them return [ ...array.slice(effectiveRotation, arrayLength), ...array.slice(0, effectiveRotation) ]; } rotateArrayDirect(45678,arrayLong);
Juggling small
const gcd = (a, b) => { if(b==0){ return a; }else{ return gcd(b, a%b); } } const rotateArrayJuggling = (desiredRotation, array) => { const n = array.length; let temp = null; let j = null; let k = null; const gcdResult = gcd(desiredRotation, n); for (let i = 0; i < gcdResult; i++){ temp = array[i]; j = i; while (true){ k = j + desiredRotation; if (k >= n){ k = k - n; } if (k== i){ break; } array[j] = array[k]; j = k; } array[j] = temp; } return array; } rotateArrayJuggling(4,arraySmall);
Juggling medium
const gcd = (a, b) => { if(b==0){ return a; }else{ return gcd(b, a%b); } } const rotateArrayJuggling = (desiredRotation, array) => { const n = array.length; let temp = null; let j = null; let k = null; const gcdResult = gcd(desiredRotation, n); for (let i = 0; i < gcdResult; i++){ temp = array[i]; j = i; while (true){ k = j + desiredRotation; if (k >= n){ k = k - n; } if (k== i){ break; } array[j] = array[k]; j = k; } array[j] = temp; } return array; } rotateArrayJuggling(456,arrayMedium);
Juggling large
const gcd = (a, b) => { if(b==0){ return a; }else{ return gcd(b, a%b); } } const rotateArrayJuggling = (desiredRotation, array) => { const n = array.length; let temp = null; let j = null; let k = null; const gcdResult = gcd(desiredRotation, n); for (let i = 0; i < gcdResult; i++){ temp = array[i]; j = i; while (true){ k = j + desiredRotation; if (k >= n){ k = k - n; } if (k== i){ break; } array[j] = array[k]; j = k; } array[j] = temp; } return array; } rotateArrayJuggling(45678,arrayLong);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (9)
Previous results
Fork
Test case name
Result
Recursive Small
Recursive Medium
Recursive Long
Direct Small
Direct Medium
Direct Long
Juggling small
Juggling medium
Juggling large
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The code snippet provided appears to be a test benchmark for an algorithm designed to rotate elements in an array using various techniques such as direct assignment, recursive copying, and juggling (a mix of the two). The results are presented in a JSON-like format with information about the browser, device platform, operating system, executions per second, and the specific test name. To answer your question, I'll focus on the "Juggling large" row in the benchmark results: **TestName:** Juggling large **ExecutionsPerSecond:** 1208.181640625 This result indicates that the algorithm being tested (likely the juggling technique) has an average execution speed of approximately 1.208 seconds for this particular test case with a large array size (45678). This value suggests that the algorithm is relatively efficient, but further analysis or tuning might be necessary to achieve better performance. Without more context about the algorithm, its intended use case, or any specific optimization goals, it's difficult to provide additional insights. If you'd like to discuss potential optimizations or have further questions, feel free to ask!
Related benchmarks:
spread vs slice vs splice
for vs map
Map vs preallocation vs slice vs spread
Array.from() vs new Array() vs [..Array()]
toSpliced vs Spread
Comments
Confirm delete:
Do you really want to delete benchmark?