Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array rotate
(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
Created:
6 years ago
by:
Registered User
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);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
Recursive Small
Recursive Medium
Recursive Long
Direct Small
Direct Medium
Direct Long
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):
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark tests two implementations of array rotation: 1. Recursive approach: This implementation uses function calls to rotate the array by a specified number of positions. 2. Direct approach: This implementation uses array slicing to rotate the array by a specified number of positions. **Options Being Compared** The options being compared are: * **Recursive Approach** + Small array size (12 elements) + Medium array size (1200 elements) + Large array size (120000 elements) * **Direct Approach** + Small array size (12 elements) + Medium array size (1200 elements) + Large array size (120000 elements) **Pros and Cons of Each Approach** **Recursive Approach** Pros: * Easy to implement * Can handle large arrays by using recursive function calls Cons: * May lead to stack overflow errors for very large arrays due to deep recursion * May be slower than direct approach due to overhead of function calls **Direct Approach** Pros: * Typically faster than recursive approach, especially for large arrays * Avoids potential stack overflow errors Cons: * Can be more complex to implement, especially for array rotation by non-integer values * Requires knowledge of array slicing syntax **Latest Benchmark Results** The latest benchmark results show the execution rates per second for each test case. The direct approach appears to outperform the recursive approach in most cases, indicating that the direct approach is generally faster and more efficient. However, it's worth noting that the recursive approach performed poorly in the "Recursive Long" test case, likely due to stack overflow errors caused by deep recursion. **Additional Insights** The benchmark results suggest that: * The performance difference between the two approaches increases as the array size increases. * The direct approach performs consistently well across all test cases, while the recursive approach is less reliable and may fail for very large arrays.
Related benchmarks:
spread vs slice vs splice
for vs map
Map vs preallocation vs slice vs spread
rev test2
Insert multiple items in an array
Comments
Confirm delete:
Do you really want to delete benchmark?