在测试内存映射向量包装器类时,很明显,与数字属性附加的相同属性访问器代码比与非数字属性附加的相同的访问器代码至少慢100倍:
lastrow = Range(ActiveCell, ActiveCell.End(xlDown)).Cells.Count 'gets the last row from active cell
NumRow = ActiveCell.Row 'gets the colum the active cell is in
Cells(lastrow, NumRow).Select 'selects that cell
class Vec2 {
constructor(buf, off) {
this.buf = new Float32Array(buf, off, 2);
}
get x() {
return this.buf[0];
}
get y() {
return this.buf[1];
}
set x(x) {
this.buf[0] = x;
}
set y(y) {
this.buf[1] = y;
}
get "0"() {
return this.buf[0];
}
get "1"() {
return this.buf[1];
}
set "0"(x) {
this.buf[0] = x;
}
set "1"(y) {
this.buf[1] = y;
}
}
有人可以在乎解释可能的原因吗?对于这里的特定情况,可能的解决方法是将类从const bench = require("@thi.ng/bench").bench;
const tx = require("@thi.ng/transducers");
const N = 10000;
const buf = new Float32Array(N * 2).fill(1);
const pts = [...tx.map((i) => new Vec2(buf.buffer, i * 8), tx.range(N))];
const updateXY = () => {
for (let i = 0; i < N; i++) {
pts[i].x += 1;
pts[i].y += 2;
}
};
const update01 = () => {
for (let i = 0; i < N; i++) {
pts[i]["0"] += 1;
pts[i]["1"] += 2;
}
};
bench(updateXY, 1000);
// 58ms
bench(update01, 1000);
// 6604ms
扩展,这确实引起了明显的区别(两个版本现在均为70ms),但我的问题更笼统了...
Float32Array