Skip to content

Instantly share code, notes, and snippets.

@vorg
Last active August 29, 2015 14:08
Show Gist options
  • Save vorg/6e70eda9938553cc1e7d to your computer and use it in GitHub Desktop.
Save vorg/6e70eda9938553cc1e7d to your computer and use it in GitHub Desktop.
Beautiful code = Slow code :(
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
@vorg
Copy link
Author

vorg commented Nov 8, 2014

At the same time in usual scenario for 10'000 points

center3: 0ms
center3R: 1ms
center3SR: 3ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment