Last active
August 29, 2015 14:08
-
-
Save vorg/6e70eda9938553cc1e7d to your computer and use it in GitHub Desktop.
Beautiful code = Slow code :(
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GeomUtils.center3 = function(points) { | |
var sum = new Vec3(0, 0, 0); | |
for(var i=0; i<points.length; i++) { | |
sum.add(points[i]); | |
} | |
sum.scale(1/points.length); | |
return sum; | |
} | |
GeomUtils.center3R = function(points) { | |
var center = points.reduce(function(center, p) { | |
return center.add(p); | |
}, new Vec3(0, 0, 0)).scale(1/points.length); | |
} | |
GeomUtils.average = function(list) { | |
var sum = 0; | |
for(var i=0; i<list.length; i++) { | |
sum += list[i]; | |
} | |
return sum / list.length; | |
} | |
var R = require('ramda'); | |
GeomUtils.center3SR = function(points) { | |
var x = GeomUtils.average(points.map(R.prop('x'))); | |
var y = GeomUtils.average(points.map(R.prop('y'))) | |
var z = GeomUtils.average(points.map(R.prop('z'))) | |
return new Vec3(x, y, z); | |
} | |
//Results for 1000'000 points | |
//center3: 7ms | |
//center3R: 45ms | |
//center3SR: 1594ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
At the same time in usual scenario for 10'000 points
center3: 0ms
center3R: 1ms
center3SR: 3ms