Skip to content

Instantly share code, notes, and snippets.

@guybrush guybrush/ndarray-vs-obj.js
Last active Aug 29, 2015

Embed
What would you like to do?
bench ndarray vs obj
/*
node@0.10.28
ndarr : 2030.75ops/sec avg:0.49 max:2 min:0
obj : 8245ops/sec avg:0.12 max:5 min:0
ndarr : 1958.5ops/sec avg:0.51 max:1 min:0
obj : 8099.25ops/sec avg:0.12 max:1 min:0
node@0.11.13
ndarr : 8205.75ops/sec avg:0.12 max:2 min:0
obj : 27084.25ops/sec avg:0.04 max:2 min:0
ndarr : 8197.5ops/sec avg:0.12 max:2 min:0
obj : 27161.5ops/sec avg:0.04 max:1 min:0
chrome@37.0.2008.2
ndarr : 8508.5ops/sec avg:0.12 max:1 min:0
obj : 27552.75ops/sec avg:0.04 max:1 min:0
ndarr : 8580ops/sec avg:0.12 max:2 min:0
obj : 27746.25ops/sec avg:0.04 max:1 min:0
firefox@29.0.1
"ndarr : 34367.5ops/sec avg:0.03 max:2 min:0"
"obj : 31713ops/sec avg:0.03 max:1 min:0"
"ndarr : 11812.06ops/sec avg:0.08 max:5573 min:0"
"obj : 32993.25ops/sec avg:0.03 max:1 min:0"
*/
var ndarray = require('ndarray')
var n = 2e3
var arr = new Float64Array(n*9)
var ndarr = ndarray(arr,[n,9])
function Obj() {
this.position0 = 1
this.position1 = 2
this.position2 = 3
this.velocity0 = 1
this.velocity1 = 2
this.velocity2 = 3
this.acceleration0 = 1
this.acceleration1 = 2
this.acceleration2 = 3
}
Obj.prototype.accelerate = function(x,y,z){
var p0 = this.position0
var p1 = this.position1
var p2 = this.position2
var v0 = this.velocity0
var v1 = this.velocity1
var v2 = this.velocity2
var a0 = this.acceleration0
var a1 = this.acceleration1
var a2 = this.acceleration2
a0 += x
a1 += y-50
a2 += z
v0 += a0
v1 += a1
v2 += a2
v0 *= .95
v1 *= 1
v2 *= .95
p0 += v0
p1 += v1
p2 += v2
this.position0 = p0
this.position1 = p1
this.position2 = p2
this.velocity0 = v0
this.velocity1 = v1
this.velocity2 = v2
this.acceleration0 = a0
this.acceleration1 = a1
this.acceleration2 = a2
}
var objs = []
for (var i=0;i<n;i++) {
objs.push(new Obj)
ndarr.set(i,0,1)
ndarr.set(i,1,2)
ndarr.set(i,2,3)
ndarr.set(i,3,1)
ndarr.set(i,4,2)
ndarr.set(i,5,3)
ndarr.set(i,6,1)
ndarr.set(i,7,2)
ndarr.set(i,8,3)
}
function accelerateNdarr(i,x,y,z) {
var p0 = ndarr.get(i,0)
var p1 = ndarr.get(i,1)
var p2 = ndarr.get(i,2)
var v0 = ndarr.get(i,3)
var v1 = ndarr.get(i,4)
var v2 = ndarr.get(i,5)
var a0 = ndarr.get(i,6)
var a1 = ndarr.get(i,7)
var a2 = ndarr.get(i,8)
a0 += x
a1 += y-50
a2 += z
v0 += a0
v1 += a1
v2 += a2
v0 *= .95
v1 *= 1
v2 *= .95
p0 += v0
p1 += v1
p2 += v2
ndarr.set(i,0,p0)
ndarr.set(i,1,p1)
ndarr.set(i,2,p2)
ndarr.set(i,3,v0)
ndarr.set(i,4,v1)
ndarr.set(i,5,v2)
ndarr.set(i,6,a0)
ndarr.set(i,7,a1)
ndarr.set(i,8,a2)
}
function benchObj(i) {
for (var j=0;j<2000;j++) objs[i%n].accelerate(i%100,i%100,i%100)
}
function benchNdarr(i) {
for (var j=0;j<2000;j++) accelerateNdarr(i%n,i%100,i%100,i%100)
}
var t = 4e3
var tt = 1e3
if (process.versions) console.log('node@%s',process.versions.node)
bench('ndarr ',benchNdarr ,t,tt)
bench('obj ',benchObj ,t,tt)
bench('ndarr ',benchNdarr ,t,tt)
bench('obj ',benchObj ,t,tt)
function bench(name, fn, dur, warmup){
dur = dur || 1000
name = name || 'unnamed'
// name = name+Array(16).slice(name.length).join(' ')
if (warmup) while(warmup--) fn(0)
var now = Date.now()
var start = now
var tLast = now
var time = 0, count = 0, dt = 0, tMin = 0, tMax = 0, i = 0
while (time<dur) {
fn(i)
i++
time = (Date.now()-start)
sample()
}
var msPerOp = time/i
var opsPerSec = 1000/msPerOp
console.log(name+': '
+ parseFloat(opsPerSec.toFixed(2))+'ops/sec'
+ ' avg:'+parseFloat(msPerOp.toFixed(2))
+ ' max:'+tMax
+ ' min:'+tMin
)
function sample() {
count++
now = Date.now()
dt = now - tLast
if (count==1) {
tMin = dt
tMax = dt
}
else {
tMin = tMin > dt ? dt : tMin
tMax = tMax < dt ? dt : tMax
}
tLast = now
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.