dataviewtest.js 6.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

new BenchmarkSuite('DataViewTest-DataView-BigEndian', [1000], [
  new Benchmark('DataViewTest-DataView-BigEndian', false, false, 0, doTestDataViewBigEndian),
]);

new BenchmarkSuite('DataViewTest-DataView-LittleEndian', [1000], [
  new Benchmark('DataViewTest-DataView-LittleEndian', false, false, 0, doTestDataViewLittleEndian),
]);

13 14 15 16
new BenchmarkSuite('DataViewTest-DataView-Floats', [1000], [
  new Benchmark('DataViewTest-DataView-Floats', false, false, 0, doTestDataViewFloats),
]);

17 18 19 20 21 22 23 24
new BenchmarkSuite('DataViewTest-TypedArray-BigEndian', [1000], [
  new Benchmark('DataViewTest-TypedArray-BigEndian', false, false, 0, doTestTypedArrayBigEndian),
]);

new BenchmarkSuite('DataViewTest-TypedArray-LittleEndian', [1000], [
  new Benchmark('DataViewTest-TypedArray-LittleEndian', false, false, 0, doTestTypedArrayLittleEndian),
]);

25 26 27 28
new BenchmarkSuite('DataViewTest-TypedArray-Floats', [1000], [
  new Benchmark('DataViewTest-TypedArray-Floats', false, false, 0, doTestTypedArrayFloats),
]);

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
function doTestDataViewBigEndian() {
  doIterations(false, true);
}

function doTestDataViewLittleEndian() {
  doIterations(true, true);
}

function doTestTypedArrayBigEndian() {
  doIterations(false, false);
}

function doTestTypedArrayLittleEndian() {
  doIterations(true, false);
}

45 46 47 48 49 50 51 52
function doTestDataViewFloats() {
  doFloatIterations(true);
}

function doTestTypedArrayFloats() {
  doFloatIterations(false);
}

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
function doIterations(littleEndian, dataView) {
  var buffer = makeBuffer(1000, littleEndian);
  var iterations = 10;
  if (dataView) {
    for (var i = 0; i < iterations; i++)
      doOneIterationDV(buffer, littleEndian);
  } else {
    for (var i = 0; i < iterations; i++)
      doOneIterationJS(buffer, littleEndian);
  }
}

function makeBuffer(size, littleEndian) {
  var buffer = new ArrayBuffer(size * 14);
  var view = new DataView(buffer);
  for (var i = 0; i < size; ++i) {
    view.setInt8(i * 14, i);
    view.setUint8(i * 14 + 1, i);
    view.setInt16(i * 14 + 2, i * i, littleEndian);
    view.setUint16(i * 14 + 4, i * i, littleEndian);
    view.setInt32(i * 14 + 6, i * i * i, littleEndian);
    view.setUint32(i * 14 + 10, i * i * i, littleEndian);
  }
  return buffer;
}

function doOneIterationDV(buffer, littleEndian) {
  var xor = 0;
  var view = new DataView(buffer);
  for (var i = 0; i < view.byteLength; i += 14) {
    xor ^= view.getInt8(i);
    xor ^= view.getUint8(i + 1);
    xor ^= view.getInt16(i + 2, littleEndian);
    xor ^= view.getUint16(i + 4, littleEndian);
    xor ^= view.getInt32(i + 6, littleEndian);
    xor ^= view.getUint32(i + 10, littleEndian);
  }
}

function doOneIterationJS(buffer, littleEndian) {
  var xor = 0;
  var reader;
  if (littleEndian) {
    reader = new LittleEndian(buffer);
  } else {
    reader = new BigEndian(buffer);
  }
  for (var i = 0; i < buffer.byteLength; i += 14) {
    xor ^= reader.getInt8(i);
    xor ^= reader.getUint8(i + 1);
    xor ^= reader.getInt16(i + 2);
    xor ^= reader.getUint16(i + 4);
    xor ^= reader.getInt32(i + 6);
    xor ^= reader.getUint32(i + 10);
  }
}

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
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];
  }
}

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
function BigEndian(buffer, opt_byteOffset) {
  this.uint8View_ = new Uint8Array(buffer, opt_byteOffset || 0);
  this.int8View_ = new Int8Array(buffer, opt_byteOffset || 0);
}
BigEndian.prototype.getInt8 = function(byteOffset) {
  return this.int8View_[byteOffset];
};
BigEndian.prototype.getUint8 = function(byteOffset) {
  return this.uint8View_[byteOffset];
};
BigEndian.prototype.getInt16 = function(byteOffset) {
  return this.uint8View_[byteOffset + 1] | (this.int8View_[byteOffset] << 8);
};
BigEndian.prototype.getUint16 = function(byteOffset) {
  return this.uint8View_[byteOffset + 1] | (this.uint8View_[byteOffset] << 8);
};
BigEndian.prototype.getInt32 = function(byteOffset) {
  return this.uint8View_[byteOffset + 3] |
      (this.uint8View_[byteOffset + 2] << 8) |
      (this.uint8View_[byteOffset + 1] << 16) |
      (this.int8View_[byteOffset] << 24);
};
BigEndian.prototype.getUint32 = function(byteOffset) {
  return this.uint8View_[byteOffset + 3] +
      (this.uint8View_[byteOffset + 2] << 8) +
      (this.uint8View_[byteOffset + 1] << 16) +
      (this.uint8View_[byteOffset] * (1 << 24));
};

function LittleEndian(buffer, opt_byteOffset) {
  this.uint8View_ = new Uint8Array(buffer, opt_byteOffset || 0);
  this.int8View_ = new Int8Array(buffer, opt_byteOffset || 0);
}
LittleEndian.prototype.getInt8 = function(byteOffset) {
  return this.int8View_[byteOffset];
};
LittleEndian.prototype.getUint8 = function(byteOffset) {
  return this.uint8View_[byteOffset];
};
LittleEndian.prototype.getInt16 = function(byteOffset) {
  return this.uint8View_[byteOffset] | (this.int8View_[byteOffset + 1] << 8);
};
LittleEndian.prototype.getUint16 = function(byteOffset) {
  return this.uint8View_[byteOffset] | (this.uint8View_[byteOffset + 1] << 8);
};
LittleEndian.prototype.getInt32 = function(byteOffset) {
  return this.uint8View_[byteOffset] |
      (this.uint8View_[byteOffset + 1] << 8) |
      (this.uint8View_[byteOffset + 2] << 16) |
      (this.int8View_[byteOffset + 3] << 24);
};
LittleEndian.prototype.getUint32 = function(byteOffset) {
  return this.uint8View_[byteOffset] +
      (this.uint8View_[byteOffset + 1] << 8) +
      (this.uint8View_[byteOffset + 2] << 16) +
      (this.uint8View_[byteOffset + 3] * (1 << 24));
};