Commit e14699c5 authored by Théotime Grohens's avatar Théotime Grohens Committed by Commit Bot

[dataview] Improve JS performance test coverage for DataView

This CL adds a comparison for the performance of getting and setting
float32 and float64 values with DataViews and with TypedArrays.

Since TypedArrays do not specify endianness, we can't compare
performance across both possible endiannesses, but this is better
than no comparison at all.

Change-Id: Iea54b942c0bb8168e9d8002d94e2bb9bc6566331
Reviewed-on: https://chromium-review.googlesource.com/1120250Reviewed-by: 's avatarMichael Stanton <mvstanton@chromium.org>
Commit-Queue: Théotime Grohens <theotime@google.com>
Cr-Commit-Position: refs/heads/master@{#54158}
parent 249f6069
......@@ -10,6 +10,10 @@ new BenchmarkSuite('DataViewTest-DataView-LittleEndian', [1000], [
new Benchmark('DataViewTest-DataView-LittleEndian', false, false, 0, doTestDataViewLittleEndian),
]);
new BenchmarkSuite('DataViewTest-DataView-Floats', [1000], [
new Benchmark('DataViewTest-DataView-Floats', false, false, 0, doTestDataViewFloats),
]);
new BenchmarkSuite('DataViewTest-TypedArray-BigEndian', [1000], [
new Benchmark('DataViewTest-TypedArray-BigEndian', false, false, 0, doTestTypedArrayBigEndian),
]);
......@@ -18,6 +22,10 @@ new BenchmarkSuite('DataViewTest-TypedArray-LittleEndian', [1000], [
new Benchmark('DataViewTest-TypedArray-LittleEndian', false, false, 0, doTestTypedArrayLittleEndian),
]);
new BenchmarkSuite('DataViewTest-TypedArray-Floats', [1000], [
new Benchmark('DataViewTest-TypedArray-Floats', false, false, 0, doTestTypedArrayFloats),
]);
function doTestDataViewBigEndian() {
doIterations(false, true);
}
......@@ -34,6 +42,14 @@ function doTestTypedArrayLittleEndian() {
doIterations(true, false);
}
function doTestDataViewFloats() {
doFloatIterations(true);
}
function doTestTypedArrayFloats() {
doFloatIterations(false);
}
function doIterations(littleEndian, dataView) {
var buffer = makeBuffer(1000, littleEndian);
var iterations = 10;
......@@ -91,6 +107,50 @@ function doOneIterationJS(buffer, littleEndian) {
}
}
function doFloatIterations(dataView) {
var buffer = makeFloatBuffer(1000);
var iterations = 10;
if (dataView) {
for (var i = 0; i < iterations; i++)
doOneFloatIterationDV(buffer);
} else {
for (var i = 0; i < iterations; i++)
doOneFloatIterationJS(buffer);
}
}
function makeFloatBuffer(size) {
var buffer = new ArrayBuffer(size * 16);
var view = new DataView(buffer);
for (var i = 0; i < size; i++) {
view.setFloat64(i * 16, Math.log10(i + 1));
view.setFloat32(i * 16 + 8, Math.sqrt(i));
view.setFloat32(i * 16 + 12, Math.cos(i));
}
return buffer;
}
function doOneFloatIterationDV(buffer) {
var sum = 0;
var view = new DataView(buffer);
for (var i = 0; i < view.byteLength; i += 16) {
sum += view.getFloat64(i);
sum += view.getFloat32(i + 8);
sum += view.getFloat32(i + 12);
}
}
function doOneFloatIterationJS(buffer) {
var sum = 0;
var float32array = new Float32Array(buffer);
var float64array = new Float64Array(buffer);
for (var i = 0; i < buffer.byteLength; i += 16) {
sum += float64array[i/8];
sum += float32array[i/4 + 2];
sum += float32array[i/4 + 3];
}
}
function BigEndian(buffer, opt_byteOffset) {
this.uint8View_ = new Uint8Array(buffer, opt_byteOffset || 0);
this.int8View_ = new Int8Array(buffer, opt_byteOffset || 0);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment