Multiple embedded loops in NodeJS

2019-07-11 02:12发布

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;
}

1条回答
对你真心纯属浪费
2楼-- · 2019-07-11 02:29

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();
  });
查看更多
登录 后发表回答