summaryrefslogtreecommitdiff
path: root/node_modules/webworker-threads/benchmark/pi.rb
blob: 5f8596511cecbd4fb81e6ac7bf7677d8a3849c2f (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
#!/usr/bin/env ruby

def pi(i)
  pi= 0
  num= 4.0
  den= 1
  plus= true

  while den < 5e7
    if plus
      pi+= num/den
      plus= false
    else
      pi-= num/den
      plus= true
    end
    den+= 2
  end
  puts "#{pi} -> #{i}"
end

threads=[]
count=ARGV.shift || 1
puts "Using #{count} threads"

t= Time.new
count.to_i.times do |i|
  threads << Thread.new{pi(i)}  
end

threads.each do |j|
  j.join
end

t= (Time.new- t)* 1e3
tps= Integer(count) * 1e3 / t
puts "\nTiempo total (ms) -> %.0f" % t
puts "Threads por segundo -> %.1f" % tps
puts "Total de threads ejecutadas -> #{count}"