Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array rotate juggling cloned
(version: 0)
Two implementations of array rotatee
Comparing performance of:
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:
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, arr) => { const array = [...arr] 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, arr) => { const array = [...arr] 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, arr) => { const array = [...arr] 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 (6)
Previous results
Fork
Test case name
Result
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):
I think I can help you out! After analyzing the provided benchmark data, it appears that there are two distinct patterns: 1. **Direct Small**: The Firefox 73 browser on a desktop with Mac OS X 10.14 produces significantly higher execution counts (4183160.5 and 3910090.75) for the "Juggling small" test compared to the "Direct Small" test (83661.6640625). This suggests that there is a significant performance difference between running the benchmark directly in Firefox versus using the "Direct Small" method. 2. **Variation within tests**: The execution counts within each test vary widely, with some tests producing extremely low values (586.1472778320312 and 409.5810241699219). These low values may be indicative of optimization or caching issues. Based on these observations, I would recommend the following: * Use the "Direct Small" method for benchmarking small arrays. * Avoid using the original code that performs juggling (the `rotateArrayJuggling` function) unless you have reason to believe it is optimized and performing well. * Investigate potential optimization opportunities within each test, such as caching or reducing unnecessary computations. Please let me know if you'd like more specific advice or further analysis!
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?