admin管理员组文章数量:1417070
I'm creating some algorithms that are very performance heavy, e.g. evolutionary and artificial intelligence. What matters to me is that my update function gets called often (precision), and I just can't get setInterval
to update faster than once per millisecond.
Initially I wanted to just use a while loop, but I'm not sure that those kinds of blocking loops are a viable solution in the Node.js environment. Will Socket.io's socket.on("id", cb) work if I run into an "infinite" loop? Does my code somehow need to return to Node.js to let it check for all the events, or is that done automatically?
And last (but not least), if while loops will indeed block my code, what is another solution to getting really low delta-times between my update functions? I think threads could help, but I doubt that they're possible, my Socket.io server and other classes need to somehow municate, and by "other classes" I mean the main World
class, which has an update method that needs to get called and does the heavy lifting, and a getInfo
method that is used by my server. I feel like most of the time the program is just sitting there, waiting for the interval to fire, wasting time instead of doing calculations...
Also, I'd like to know if Node.js is even suited for these sorts of tasks.
I'm creating some algorithms that are very performance heavy, e.g. evolutionary and artificial intelligence. What matters to me is that my update function gets called often (precision), and I just can't get setInterval
to update faster than once per millisecond.
Initially I wanted to just use a while loop, but I'm not sure that those kinds of blocking loops are a viable solution in the Node.js environment. Will Socket.io's socket.on("id", cb) work if I run into an "infinite" loop? Does my code somehow need to return to Node.js to let it check for all the events, or is that done automatically?
And last (but not least), if while loops will indeed block my code, what is another solution to getting really low delta-times between my update functions? I think threads could help, but I doubt that they're possible, my Socket.io server and other classes need to somehow municate, and by "other classes" I mean the main World
class, which has an update method that needs to get called and does the heavy lifting, and a getInfo
method that is used by my server. I feel like most of the time the program is just sitting there, waiting for the interval to fire, wasting time instead of doing calculations...
Also, I'd like to know if Node.js is even suited for these sorts of tasks.
Share Improve this question edited Sep 2, 2012 at 13:58 corazza asked Aug 22, 2012 at 13:58 corazzacorazza 32.4k39 gold badges121 silver badges191 bronze badges 4- Is update used as a callback for some asynchronous processing? – hvgotcodes Commented Aug 22, 2012 at 14:03
- The update is a callback of setInterval, so yes. – corazza Commented Aug 22, 2012 at 14:06
- what i am getting at is 1) what does your update do? and 2) where are the (blocking) calculations performed? – hvgotcodes Commented Aug 22, 2012 at 14:10
- I'm simulating virtual environments, like Polyworld. Currently, I was just writign the server code and some other things, so the algorithms aren't yet implemented. Think a scene with physics, up to a 1000 objects. These objects are animals, each with its own neural network. Basically, shorter the delta time, more precision in my simulation. – corazza Commented Aug 22, 2012 at 14:17
1 Answer
Reset to default 9You can execute havy algorithms in separate thread using child_process.fork and wait results in main thread via child.on('message', function (message) { });
app.js
var child_process = require('child_process');
var child = child_process.fork('./heavy.js', [ 'some', 'argv', 'params' ]);
child.on('message', function(message) {
// heavy results here
});
heavy.js
while (true) {
if (Math.random() < 0.001) {
process.send({ result: 'wow!' });
}
}
本文标签: javascriptPerformance heavy algorithms on NodejsStack Overflow
版权声明:本文标题:javascript - Performance heavy algorithms on Node.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745257105a2650166.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论