Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Buffer Access
(version: 0)
Comparing performance of:
DataView AoS vs DataView byte-aligned AoS vs TypedArray AoS vs TypedArray byte-aligned AoS vs TypedArray SoA
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var bench1buffer; var bench2buffer; var bench3buffer; { const stride = 10; bench1buffer = new ArrayBuffer(stride * 1000); /* 0 u16 id 2 f32 x 6 f32 y */ for (let i = 0; i < 1000; i++) { const struct = new DataView(bench1buffer, i * stride, stride); struct.setUint16(0, i); struct.setFloat32(2, i); struct.setFloat32(6, i * 2); } } { const stride = 12; bench2buffer = new ArrayBuffer(stride * 1000); /* 0 u16 id 4 f32 x 8 f32 y */ const floats = new Float32Array(bench2buffer); const u16s = new Uint16Array(bench2buffer); for (let i = 0; i < 1000; i++) { u16s[i * stride] = i; floats[i * stride + 4] = i; floats[i * stride + 8] = i * 2; } } { bench3buffer = { id: new Uint16Array(1000), x: new Float32Array(1000), y: new Float32Array(1000), }; for (let i = 0; i < 1000; i++) { bench3buffer.id[i] = i; bench3buffer.x[i] = i; bench3buffer.y[i] = i * 2; } }
Tests:
DataView AoS
const stride = 10; /* 0 u16 id 2 f32 x 6 f32 y */ let sum = 0; for (let i = 0; i < 1000; i++) { const struct = new DataView(bench1buffer, i * stride, stride); const id = struct.getUint16(0); const x = struct.getFloat32(2); const y = struct.getFloat32(6); sum += id + x * y; }
DataView byte-aligned AoS
const stride = 12; /* 0 u16 id 4 f32 x 8 f32 y */ let sum = 0; for (let i = 0; i < 1000; i++) { const struct = new DataView(bench2buffer, i * stride, stride); const id = struct.getUint16(0); const x = struct.getFloat32(4); const y = struct.getFloat32(8); sum += id + x * y; }
TypedArray AoS
const stride = 10; /* 0 u16 id 2 f32 x 6 f32 y */ let floats = new Float32Array(bench1buffer); let u32s = new Uint32Array(bench1buffer); let sum = 0; for (let i = 0; i < 1000; i++) { const offset = i * stride; const id = floats[offset]; const x = floats[offset + 2]; const y = floats[offset + 6]; sum += id + x * y; }
TypedArray byte-aligned AoS
const stride = 12; /* 0 u16 id 4 f32 x 8 f32 y */ let floats = new Float32Array(bench2buffer); let u32s = new Uint32Array(bench2buffer); let sum = 0; for (let i = 0; i < 1000; i++) { const offset = i * stride; const id = floats[offset]; const x = floats[offset + 4]; const y = floats[offset + 8]; sum += id + x * y; }
TypedArray SoA
let sum = 0; for (let i = 0; i < 1000; i++) { const id = bench3buffer.id[i]; const x = bench3buffer.x[i]; const y = bench3buffer.y[i]; sum += id + x * y; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
DataView AoS
DataView byte-aligned AoS
TypedArray AoS
TypedArray byte-aligned AoS
TypedArray SoA
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 its options. **Benchmark Overview** The benchmark is designed to compare the performance of different data access patterns: Array Of Arrays (AoA), Array Of Structures (SoA), and two variations of TypedArray access patterns. **Data Structure** The benchmark creates three data structures: 1. `bench3buffer.id`, `bench3buffer.x`, and `bench3buffer.y` - These are the arrays that will be used to test SoA. 2. The first 1000 elements of a 32-bit integer array (`floats`) in both TypedArray AoA and TypedArray SoA patterns. 3. A 32-bit integer array (`u32s`) in both TypedArray AoA and TypedArray SoA patterns. **Access Patterns** The benchmark measures the execution time for each access pattern: 1. **TypedArray byte-aligned AoS**: Accessing elements using contiguous memory blocks, where each element is aligned to a 4-byte boundary. 2. **TypedArray AoA**: Accessing elements using two separate arrays, `floats` and `u32s`, with each array containing the x and y coordinates of an object. 3. **TypedArray SoA**: Accessing elements using a single array, where each element is a struct containing the id, x, and y coordinates. **Browser Results** The latest benchmark results show that: 1. TypedArray byte-aligned AoS outperforms all other patterns with an execution time of approximately 145009 executions per second. 2. TypedArray AoA is slower than TypedArray SoA but faster than TypedArray byte-aligned AoS, with an execution time of around 129978 executions per second. 3. TypedArray SoA has the highest execution time among all three patterns, with approximately 5237 executions per second. **Performance Implications** These results suggest that when working with large arrays or complex data structures, using contiguous memory access (TypedArray byte-aligned AoS) can lead to significant performance gains compared to other patterns. However, if the data structure is already optimized for contiguous memory access, it may be unnecessary to use a TypedArray pattern. In summary, this benchmark highlights the importance of considering data access patterns and memory layout when optimizing code performance.
Related benchmarks:
Buffer Access 2
Buffer Access 2 - const arrays
Buffer Access 3
Buffer Access 4
Comments
Confirm delete:
Do you really want to delete benchmark?