Friday, 18 September 2020

improving performance of a brute force node script by multi-processing/parallel programming

I have 5 lists of words. Lets call them list1, list2, list3, list4, list5.
Each list can have at most 2048 different words.

Example:
list1 = ['one', 'two', 'three'];
list2 = ['a', 'b', 'c'];
list3 = ['d', 'e', 'f'];
list4 = ['g'];
list5 = ['h', 'i', 'j', 'k'];

The final string res is generated by concatenating one word from each list. For example: 'one' + 'b' + 'f' + 'g' + 'k' = 'onebfgk

I have a decode function which takes in res and outputs a number. I am interested in finding the string which decodes to a given number: prize.

Currently, I am generating all possible combinations of res, decoding it and matching it against the given prize.
My current node.js script looks like this:

var prize = 1234;
var list1 = ['one', 'two', 'three'];
var list2 = ['a', 'b', 'c'];
var list3 = ['d', 'e', 'f'];
var list4 = ['g'];
var list5 = ['h', 'i', 'j', 'k'];
var word1 = '';
var word2 = '';
var word3 = '';
var word4 = '';
var word5 = '';

for (var n = 0; n < list1.length; n++) {
    word1 = arr[0][n];
    for (var m = 0; m < list2.length; m++) {
        word2 = arr[1][m];
        for (var o = 0; o < list3.length; o++) {
            word3 = arr[2][o];
            for (var p = 0; p < list4.length; p++) {
                word4 = arr[3][p];
                for (var q = 0; q < list5.length; q++) {
                    word5 = arr[4][q];
                    let res = word1 + word2 + word3 + word4 + word5;
                    if (decode(res) == prize){
                        console.log(res);
                        throw new Error("Found the match. The program will now exit");
                    }
                }
            }
        }
    }
}

I am interested in speeding this up. How can I modify the script to speed it up drastically by having it use GPU or multi-processing/parallel processing or any other way?

I am limited to using node.js because the decode function comes from a node module.



from improving performance of a brute force node script by multi-processing/parallel programming

No comments:

Post a Comment