Skip to content

Instantly share code, notes, and snippets.

@sirkitree
Created January 21, 2015 15:29
Show Gist options
  • Save sirkitree/d0bb00d1b42e885385ec to your computer and use it in GitHub Desktop.
Save sirkitree/d0bb00d1b42e885385ec to your computer and use it in GitHub Desktop.
coordinate generator
var points = [];
var x = y = z = this.constraint;
var constraintDecrement = this.constraint * -1;
for (var x = this.constraint; x >= constraintDecrement; x--) {
points.push({
'pos': {'x' : x, 'y' : y, 'z' : z}
});
for (var y = this.constraint; y > constraintDecrement; y--) {
points.push({
'pos': {'x' : x, 'y' : y, 'z' : z}
});
for (var z = this.constraint; z > constraintDecrement; z--) {
points.push({
'pos': {'x' : x, 'y' : y, 'z' : z}
});
};
};
};
@sirkitree
Copy link
Author

If this.constraint is equal to 10, it's not problem. But increasing to 100 takes forever. Is there a good way to optimize this?

@sirkitree
Copy link
Author

75 works okay. Using time i get 12.61s user 2.34s system 65% cpu 22.825 total

100 I get: FATAL ERROR: JS Allocation failed - process out of memory @ 125.55s user 3.54s system 89% cpu 2:24.77 total

@sirkitree
Copy link
Author

Article here is saying that do/while loops are much faster, especially with the condition at the end so I may try that.

@m4olivei
Copy link

Humm, if my CS education is coming back to me OK, the snippet above runs on the order of O(n^3), 8n^3 to exact, where n is this.contraint. So for:

this.constraint = 10 ==> 8,000 array entries
this.constraint = 100 ==> 8,000,000 array entries

That's a lot. Even if you could speed it up, the 8,000,000 entries in the array might be too huge?

@m4olivei
Copy link

JS bin to play with: http://jsbin.com/dogogirohu/2/edit

@sirkitree
Copy link
Author

Yup, it's way too huge, 10 will have to suffice for now, lol.

@sirkitree
Copy link
Author

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