在多重的NodeJS嵌入式循环 [英] Multiple embedded loops in NodeJS
问题描述
我喜欢执行以下code ...但你可以看到 - 这将需要10个十亿循环!所以我想知道 - 什么?你们会建议,使其旋转速度更快
为什么的原因 - 我需要像蛮力,以最好的结果 - 是因为最内侧的方法做一些历史数据的一些复杂的计算 - 从DB约700万行...和做点这一切 - 是要找到AF给定参数的最好的设置......提供了最好的结果...
VAR一个= 0.1;而(A< = 10)// 100
{
变种B = 0.1; 而(B< = 10)// 100
{
变种C = 0.1; 而(℃下= 10)// 100
{
变种D = 0.1; 而(D< = 10)// 100
{
变种E = 1; 而(E< = 10)// 10
{
变种F = 1; 而(F< = 10)// 10 10.000.000.000
{
//调用在它们使用的所有参数的方法,并返回给定的计算的结果
// DoSomething的(A,B,C,D,E,F);
F + = 1;
}
E + = 1;
}
D + = 0.1;
}
C + = 0.1;
}
B + = 0.1;
}
A + = 0.1;
}
块引用>解决方案打破环路成小块和叉子过程使用核心集群模块,处理叉子的每个小块。叉子将在单独的线程运行,更好地利用CPU的。
https://nodejs.org/api/cluster.html#cluster_how_it_works
更新。 OK,不要使用集群。使用线程模块,而不是 - 它会容易得多。 https://www.npmjs.com/package/threads ...
VAR核心=要求('操作系统')的CPU()长度。
VAR池=要求('线')池。
VAR池=新池(芯);VAR的DoSomething = pool.run(功能(数据,完成){
VAR的结果= data.a * data.b * data.c * data.d * data.e * data.f;
完成(结果,数据);
})
变种一个= 0.1;
变种B = 0.1;
变种C = 0.1;
变种D = 0.1;
变种E = 0.1;
变种F = 0.1;
而(一个或下; = 10)// 100环
{
而(B< = 10)// 100圈
{
而(℃下= 10)// 100环
{
而(D< = 10)// 100圈
{
而(E< = 10)// 10环
{
而(F< = 10)// 10环
{
//调用在它们使用的所有参数的方法,并返回给定的计算的结果
doSomeThing.send({A:A,B:B,C:C,D:D,E:E,F:F});
F + = 0.1;
}
E + = 0.1;
}
D + = 0.1;
}
C + = 0.1;
}
B + = 1;
}
一个+ = 1;
}池
。对('错误',函数(作业,误差){
console.error('工作出错:,的工作);
})
。对('完了',函数(){
的console.log('所做的一切,关闭线程池。');
pool.killAll();
});I like to execute the following code...but as you can see - it will require 10 billion loops! So i was wondering - what you guys would suggest to make it spin faster?
The reason why - i need to like "brute force" to the best result - is because the most inner method do some complex calculation of some historical data - about 7 million rows from a DB...and the point by doing all this - is to find the best "setting" of the given parameters a-f...which gives the best result...
var a = 0.1; while(a <= 10) // 100 { var b = 0.1; while(b <= 10) // 100 { var c = 0.1; while(c <= 10) // 100 { var d = 0.1; while(d <= 10) // 100 { var e = 1; while(e <= 10) // 10 { var f = 1; while(f <= 10) // 10 10.000.000.000 { // Call at method which use all parameters and return the result of a given calculation //doSomeThing(a,b,c,d,e,f); f += 1; } e += 1; } d += 0.1; } c += 0.1; } b += 0.1; } a += 0.1; }
解决方案Break the loops into smaller chunks and fork processes using the core cluster module, processing each smaller chunk in a fork. The forks will run on separate threads, better leveraging the CPU.
https://nodejs.org/api/cluster.html#cluster_how_it_works
UPDATE. OK, don't use Cluster. Use the threads module instead - it will be much easier. https://www.npmjs.com/package/threads ...
var cores = require('os').cpus().length; var Pool = require('threads').Pool; var pool = new Pool(cores); var doSomeThing = pool.run(function(data,done){ var result = data.a * data.b * data.c * data.d * data.e * data.f; done(result,data); }) var a = 0.1; var b = 0.1; var c = 0.1; var d = 0.1; var e = 0.1; var f = 0.1; while(a <= 10) // 100 loops { while(b <= 10) // 100 loops { while(c <= 10) // 100 loops { while(d <= 10) // 100 loops { while(e <= 10) // 10 loops { while(f <= 10) // 10 loops { // Call at method which use all parameters and return the result of a given calculation doSomeThing.send({a:a,b:b,c:c,d:d,e:e,f:f}); f += 0.1; } e += 0.1; } d += 0.1; } c += 0.1; } b += 1; } a += 1; } pool .on('error', function(job, error) { console.error('Job errored:', job); }) .on('finished', function() { console.log('Everything done, shutting down the thread pool.'); pool.killAll(); });
这篇关于在多重的NodeJS嵌入式循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!