Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
two dimensional array row-major vs. column-major order
(version: 7)
Evaluating array reference performance: row-major vs. column-major order
Comparing performance of:
row-major vs column-major
Created:
2 years ago
by:
Registered User
Jump to the latest result
Tests:
row-major
let array2d = new Array(1000); for (let i = 0; i < array2d.length; i++) { let arrayRow = []; for (let j = 0; j < 1000; j++) { arrayRow.push(j * i + 1); } array2d[i] = arrayRow; } let total = 0; for (let i = 0; i < array2d.length; i++) { for (let j = 0; j < array2d[i].length; j++) { console.log(array2d[i][j]); total = total + array2d[i][j]; } } console.log(total);
column-major
let array2d = new Array(1000); for (let i = 0; i < array2d.length; i++) { let arrayRow = []; for (let j = 0; j < 1000; j++) { arrayRow.push(j * i + 1); } array2d[i] = arrayRow; } let total = 0; for (let i = 0; i < array2d.length; i++) { for (let j = 0; j < array2d[i].length; j++) { console.log(array2d[j][i]); total = total + array2d[j][i]; } } console.log(total);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
row-major
column-major
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. **Benchmark Overview** The benchmark is comparing the performance of two approaches when accessing a 2D array in JavaScript: 1. Row-major order 2. Column-major order In both cases, a 2D array `array2d` is created with 1000 rows and columns, where each element's value is calculated as `j * i + 1`. The benchmark measures the time taken to iterate through each element of the array in both orders. **Benchmark Preparation Code** The preparation code is not provided, but based on the Benchmark Definition JSON, it appears that a simple loop is used to create the 2D array and calculate its elements' values. This is likely done using JavaScript's native `Array` data type. **Options Compared** Two options are compared: 1. **Row-major order**: In this approach, each row is accessed sequentially from left to right (i.e., `array2d[i][j]`). 2. **Column-major order**: In this approach, each column is accessed sequentially from top to bottom (i.e., `array2d[j][i]`). **Pros and Cons of Each Approach** * **Row-major order**: + Pros: Typically more cache-friendly, as adjacent elements in memory are often close together. + Cons: Can lead to slower performance on modern CPUs with out-of-order execution. * **Column-major order**: + Pros: Can be beneficial for certain algorithms that rely on column-wise processing (e.g., matrix multiplication). + Cons: May not be as cache-friendly, leading to slower performance. **Library Usage** The benchmark does not explicitly use any external libraries. However, it relies on the native `Array` data type and JavaScript's built-in functions for array operations. **Special JS Feature or Syntax** There are no special JavaScript features or syntaxes used in this benchmark beyond what is standard for JavaScript programming. **Other Considerations** When interpreting these results, keep in mind that modern CPUs have evolved to handle out-of-order execution and cache hierarchies more efficiently. Therefore, the performance differences between row-major and column-major orders may be less significant than expected. **Alternative Approaches** If you're interested in exploring alternative approaches or optimizing your code for better performance, consider the following: * Using NumJS or another matrix library optimized for performance. * Leveraging SIMD instructions (e.g., SSE or AVX) to parallelize operations on arrays of similar elements. * Employing caching techniques or memory alignment to improve cache locality. Keep in mind that these optimizations may require a deeper understanding of computer architecture and CPU-specific details.
Related benchmarks:
Maping BooleanArray vs uInt8 Array2 vs uint8 with bitMasking _2
Maping numeric vs f32 vs f64
subarray vs. constructor perf
Maping numeric vs f32 vs f64 with add
array[index] vs array.at(index)
Comments
Confirm delete:
Do you really want to delete benchmark?