summaryrefslogtreecommitdiff
path: root/node_modules/webworker-threads/benchmark/pi_precompiled.js
blob: 7139ac1c58ce016f86b1a26094e76fe14073d4ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
var Threads= require('threads_a_gogo');


function cb (err, msg) {
  this.destroy();
  ++i;
  process.stdout.write('\n'+ msg + ' -> '+ this.id);
}

function pi () {
  var π= 0;
  var num= 4;
  var den= 1;
  var plus= true;

  while (den < 1e7) {
    if (plus) {
      π+= num/den;
      plus= false;
    }
    else {
      π-= num/den;
      plus= true;
    }
    den+= 2;
  }
  return π;
}


var i= +process.argv[2] || 1;
console.log('Using '+ i+ ' threads');

var precompiled= Threads.preCompile('('+ pi+ ')()');
var t= Date.now();
while (i--) {
  Threads.create().eval(precompiled, cb);
}


i= 0;
process.on('exit', function () {
  t= Date.now()- t;
  var tps= (i*1e3/t).toFixed(1);
  console.log('\nTiempo total (ms) -> '+ t);
  console.log('Threads por segundo -> '+ tps);
  console.log('Total de threads ejecutadas -> '+ i);
});