Skip to content

Instantly share code, notes, and snippets.

@drhuffman12
Last active September 28, 2016 11:22
Show Gist options
  • Save drhuffman12/1a04c23559c4fb55521572cd9420fa91 to your computer and use it in GitHub Desktop.
Save drhuffman12/1a04c23559c4fb55521572cd9420fa91 to your computer and use it in GitHub Desktop.
Benchmark threads (Conclusion: best is either loops or fibers, depending on use case; threads are problematic, tending to either lock or crash the app)
require "benchmark"
require "big"
# def fib(n : BigInt)
# n < 2 ? n : fib(n-1) + fib(n-2)
# end
struct BigInt
def add!(other : BigInt) : BigInt
LibGMP.add(self, self, other)
self
end
end
def fib(n, verbose)
a = BigInt.new(1)
b = BigInt.new(1)
# puts "a: #{a}, b: #{b}"
n.times do
a.add!(b)
a, b = b, a
# puts "a: #{a}, b: #{b}"
end
puts "a: #{a}, b: #{b}" if verbose
a
end
def test_fib(count, i, mycount, fib_qty)
# puts "i = #{i} " + "v"*40
# sleep(rand/qty)
fib(fib_qty)
# Thread.current["mycount"] = count
mycount[i] = count
count += 1
# puts "i = #{i} " + "^"*40
end
def test(thread_qty, fib_qty, loop_qty = 1)
puts "\nthread_qty: #{thread_qty}, fib_qty: #{fib_qty}, loop_qty: #{loop_qty}"
puts Benchmark.measure{
count = 0
arr = {} of Int32 => Fiber
mycount = {} of Int32 => Int32
result = nil
thread_qty.times do |i|
verbose = i == (thread_qty - 1)
# arr[i] = Thread.new { test_fib(count, i, mycount, fib_qty) }
arr[i] = spawn {
loop_qty.times do |j|
# puts "i = #{i}, j: #{j} " + "v"*40
# sleep(rand/qty)
result = fib(fib_qty, verbose)
# Thread.current["mycount"] = count
mycount[i] = count
count += 1
puts("i = #{i}, j: #{j} " + "^"*40) if verbose
end
}
end
# arr.each {|t| t.join; print t["mycount"], ", " }
# arr.each {|i, t| t.join; print mycount[i], ", " }
Fiber.yield
# arr.each {|i, t| print mycount[i], ", " }
puts "count = #{count}"
puts "result = #{result}"
}
puts "\n"
end
def multi_test
test(1,1)
test(1,2)
test(1,4)
test(1,40)
test(1,400)
test(1,4000)
test(8,1)
test(8,10)
# test(8,100)
# test(8,1000)
# test(8,1)
# test(8,2)
# test(8,4)
# test(8,8)
# test(8,16)
# test(8,32)
# test(8,64)
# test(8,128)
# test(8,1280)
# test(4,64)
# test(8,10)
# test(8,1000)
# test(8,10000)
# test(800,1000)
# test(80000,10)
# test(8,100000)
# test(80,10000)
end
# test(8,20000)
# test(80,2000)
# test(4,2000,20)
test(8,20000,10)
# test(40000,100)
require "benchmark"
require "big"
# def fib(n : BigInt)
# n < 2 ? n : fib(n-1) + fib(n-2)
# end
struct BigInt
def add!(other : BigInt) : BigInt
LibGMP.add(self, self, other)
self
end
end
def fib(n, verbose)
a = BigInt.new(1)
b = BigInt.new(1)
# puts "a: #{a}, b: #{b}"
n.times do
a.add!(b)
a, b = b, a
# puts "a: #{a}, b: #{b}"
end
puts "a: #{a}, b: #{b}" if verbose
a
end
def test_fib(count, i, mycount, fib_qty)
# puts "i = #{i} " + "v"*40
# sleep(rand/qty)
fib(fib_qty)
# Thread.current["mycount"] = count
mycount[i] = count
count += 1
# puts "i = #{i} " + "^"*40
end
def test(thread_qty, fib_qty)
puts "\nthread_qty: #{thread_qty}, fib_qty: #{fib_qty}"
puts Benchmark.measure {
count = 0
arr = {} of Int32 => Fiber # Thread
mycount = {} of Int32 => Int32
result = nil
thread_qty.times do |i|
verbose = i == (thread_qty - 1)
# arr[i] = spawn test_fib(count, i, mycount, fib_qty)
arr[i] = spawn {
# puts "i = #{i} " + "v"*40
# sleep(rand/qty)
result = fib(fib_qty, verbose)
# Thread.current["mycount"] = count
mycount[i] = count
count += 1
puts("i = #{i} " + "^"*40) if verbose
}
end
# arr.each {|t| t.join; print t["mycount"], ", " }
# arr.each {|i, t| t.join; print mycount[i], ", " }
Fiber.yield
# arr.each {|i, t| print mycount[i], ", " }
puts "count = #{count}"
puts "result = #{result}"
}
puts "\n"
end
def multi_test
test(1,1)
test(1,2)
test(1,4)
test(1,40)
test(1,400)
test(1,4000)
test(8,1)
test(8,10)
# test(8,100)
# test(8,1000)
# test(8,1)
# test(8,2)
# test(8,4)
# test(8,8)
# test(8,16)
# test(8,32)
# test(8,64)
# test(8,128)
# test(8,1280)
# test(4,64)
# test(8,10)
# test(8,1000)
# test(8,10000)
# test(800,1000)
# test(80000,10)
# test(8,100000)
# test(80,10000)
end
# test(8,20000)
test(80,2000)
# test(40000,100)
require "benchmark"
require "big"
# def fib(n : BigInt)
# n < 2 ? n : fib(n-1) + fib(n-2)
# end
struct BigInt
def add!(other : BigInt) : BigInt
LibGMP.add(self, self, other)
self
end
end
def fib(n, verbose)
a = BigInt.new(1)
b = BigInt.new(1)
# puts "a: #{a}, b: #{b}"
n.times do
a.add!(b)
a, b = b, a
# puts "a: #{a}, b: #{b}"
end
puts "a: #{a}, b: #{b}" if verbose
a
end
def test_fib(count, i, mycount, fib_qty)
# puts "i = #{i} " + "v"*40
# sleep(rand/qty)
fib(fib_qty)
# Thread.current["mycount"] = count
mycount[i] = count
count += 1
# puts "i = #{i} " + "^"*40
end
def test(thread_qty, fib_qty, loop_qty = 1)
puts "\nthread_qty: #{thread_qty}, fib_qty: #{fib_qty}, loop_qty: #{loop_qty}"
puts Benchmark.measure {
count = 0
arr = {} of Int32 => Fiber # Thread
mycount = {} of Int32 => Int32
result = nil
thread_qty.times do |i|
verbose = i == (thread_qty - 1)
loop_qty.times do |j|
# arr[i] = spawn test_fib(count, i, mycount, fib_qty)
# arr[i] = spawn {
# puts "i = #{i} " + "v"*40
# sleep(rand/qty)
result = fib(fib_qty, verbose)
# Thread.current["mycount"] = count
mycount[i] = count
count += 1
puts("i = #{i} " + "^"*40) if verbose
# }
end
end
# arr.each {|t| t.join; print t["mycount"], ", " }
# arr.each {|i, t| t.join; print mycount[i], ", " }
# Fiber.yield
# mycount.each {|i, j| print j, ", " }
puts "count = #{count}"
puts "result = #{result}"
}
puts "\n"
end
def multi_test
test(1,1)
test(1,2)
test(1,4)
test(1,40)
test(1,400)
test(1,4000)
test(8,1)
test(8,10)
# test(8,100)
# test(8,1000)
# test(8,1)
# test(8,2)
# test(8,4)
# test(8,8)
# test(8,16)
# test(8,32)
# test(8,64)
# test(8,128)
# test(8,1280)
# test(4,64)
# test(8,10)
# test(8,1000)
# test(8,10000)
# test(800,1000)
# test(80000,10)
# test(8,100000)
# test(80,10000)
end
# test(8,20000)
# test(80,2000)
# test(4,2000,20)
test(8,20000,10)
# test(40000,100)
require "benchmark"
require "big"
# def fib(n : BigInt)
# n < 2 ? n : fib(n-1) + fib(n-2)
# end
struct BigInt
def add!(other : BigInt) : BigInt
LibGMP.add(self, self, other)
self
end
end
def fib(n, verbose)
a = BigInt.new(1)
b = BigInt.new(1)
# puts "a: #{a}, b: #{b}"
n.times do
a.add!(b)
a, b = b, a
# puts "a: #{a}, b: #{b}"
end
puts "a: #{a}, b: #{b}" if verbose
a
end
def test_fib(count, i, mycount, fib_qty)
# puts "i = #{i} " + "v"*40
# sleep(rand/qty)
fib(fib_qty)
# Thread.current["mycount"] = count
mycount[i] = count
count += 1
# puts "i = #{i} " + "^"*40
end
def test(thread_qty, fib_qty)
puts "\nthread_qty: #{thread_qty}, fib_qty: #{fib_qty}"
puts Benchmark.measure {
count = 0
arr = {} of Int32 => Fiber # Thread
mycount = {} of Int32 => Int32
result = nil
thread_qty.times do |i|
verbose = i == (thread_qty - 1)
# arr[i] = spawn test_fib(count, i, mycount, fib_qty)
# arr[i] = spawn {
# puts "i = #{i} " + "v"*40
# sleep(rand/qty)
result = fib(fib_qty, verbose)
# Thread.current["mycount"] = count
mycount[i] = count
count += 1
puts("i = #{i} " + "^"*40) if verbose
# }
end
# arr.each {|t| t.join; print t["mycount"], ", " }
# arr.each {|i, t| t.join; print mycount[i], ", " }
# Fiber.yield
# mycount.each {|i, j| print j, ", " }
puts "count = #{count}"
puts "result = #{result}"
}
puts "\n"
end
def multi_test
test(1,1)
test(1,2)
test(1,4)
test(1,40)
test(1,400)
test(1,4000)
test(8,1)
test(8,10)
# test(8,100)
# test(8,1000)
# test(8,1)
# test(8,2)
# test(8,4)
# test(8,8)
# test(8,16)
# test(8,32)
# test(8,64)
# test(8,128)
# test(8,1280)
# test(4,64)
# test(8,10)
# test(8,1000)
# test(8,10000)
# test(800,1000)
# test(80000,10)
# test(8,100000)
# test(80,10000)
end
# test(8,20000)
test(80,2000)
# test(40000,100)
require "benchmark"
=begin
require "big"
# def fib(n : BigInt)
# n < 2 ? n : fib(n-1) + fib(n-2)
# end
struct BigInt
def add!(other : BigInt) : BigInt
LibGMP.add(self, self, other)
self
end
end
=end
def fib(n, verbose)
a = 1 # BigInt.new(1)
b = 1 # BigInt.new(1)
# puts "a: #{a}, b: #{b}"
n.times do
a += b
a, b = b, a
# puts "a: #{a}, b: #{b}"
end
puts "a: #{a}, b: #{b}" if verbose
a
end
def test_fib(count, i, mycount, fib_qty)
# puts "i = #{i} " + "v"*40
# sleep(rand/qty)
fib(fib_qty)
# Thread.current["mycount"] = count
mycount[i] = count
count += 1
# puts "i = #{i} " + "^"*40
end
def test(thread_qty, fib_qty)
puts "\nthread_qty: #{thread_qty}, fib_qty: #{fib_qty}"
puts Benchmark.measure {
count = 0
arr = {} # of Int32 => Thread
mycount = {} # of Int32 => Int32
result = nil
thread_qty.times do |i|
verbose = i == (thread_qty - 1)
# arr[i] = Thread.new { test_fib(count, i, mycount, fib_qty) }
# arr[i] = Thread.new {
# puts "i = #{i} " + "v"*40
# sleep(rand/qty)
result = fib(fib_qty, verbose)
# Thread.current["mycount"] = count
mycount[i] = count
count += 1
puts("i = #{i} " + "^"*40) if verbose
# }
end
# arr.each {|t| t.join; print t["mycount"], ", " }
# arr.each_pair {|i, t| t.join; print mycount[i], ", " }
# mycount.each_pair {|i, j| print j, ", " }
puts "count = #{count}"
puts "result = #{result}"
}
puts "\n"
end
def multi_test
test(1,1)
test(1,2)
test(1,4)
test(1,40)
test(1,400)
test(1,4000)
test(8,1)
test(8,10)
# test(8,100)
# test(8,1000)
# test(8,1)
# test(8,2)
# test(8,4)
# test(8,8)
# test(8,16)
# test(8,32)
# test(8,64)
# test(8,128)
# test(8,1280)
# test(4,64)
# test(8,10)
# test(8,1000)
# test(8,10000)
# test(800,1000)
# test(80000,10)
# test(8,100000)
# test(80,10000)
end
# test(8,20000)
test(80,2000)
# test(40000,100)
require "benchmark"
require "big"
# def fib(n : BigInt)
# n < 2 ? n : fib(n-1) + fib(n-2)
# end
struct BigInt
def add!(other : BigInt) : BigInt
LibGMP.add(self, self, other)
self
end
end
def fib(n, verbose)
a = BigInt.new(1)
b = BigInt.new(1)
# puts "a: #{a}, b: #{b}"
n.times do
a.add!(b)
a, b = b, a
# puts "a: #{a}, b: #{b}"
end
puts "a: #{a}, b: #{b}" if verbose
a
end
def test_fib(count, i, mycount, fib_qty)
# puts "i = #{i} " + "v"*40
# sleep(rand/qty)
fib(fib_qty)
# Thread.current["mycount"] = count
mycount[i] = count
count += 1
# puts "i = #{i} " + "^"*40
end
def test(thread_qty, fib_qty)
puts "\nthread_qty: #{thread_qty}, fib_qty: #{fib_qty}"
puts Benchmark.measure{
max_retries = 5
count = 0
arr = {} of Int32 => Thread
mycount = {} of Int32 => Int32
result = nil
thread_qty.times do |i|
verbose = i == (thread_qty - 1)
# arr[i] = Thread.new { test_fib(count, i, mycount, fib_qty) }
arr[i] = Thread.new {
retries = 0
done = false
begin
while !done && retries < max_retries
# puts "i = #{i} " + "v"*40
# sleep(rand/qty)
result = fib(fib_qty, verbose)
# Thread.current["mycount"] = count
mycount[i] = count
count += 1
puts("i = #{i} " + "^"*40) if verbose
done = true
end
rescue error
retries += 1
puts "i: #{i}, retries: #{retries}, error.message: #{error.message}"
end
}
end
# arr.each {|t| t.join; print t["mycount"], ", " }
# arr.each {|i, t| t.join; print mycount[i], ", " }
arr.each {|i, t| t.join }
# arr.each {|i, t| print mycount[i], ", " }
puts "count = #{count}"
puts "result = #{result}"
}
puts "\n"
end
def multi_test
test(1,1)
test(1,2)
test(1,4)
test(1,40)
test(1,400)
test(1,4000)
test(8,1)
test(8,10)
# test(8,100)
# test(8,1000)
# test(8,1)
# test(8,2)
# test(8,4)
# test(8,8)
# test(8,16)
# test(8,32)
# test(8,64)
# test(8,128)
# test(8,1280)
# test(4,64)
# test(8,10)
# test(8,1000)
# test(8,10000)
# test(800,1000)
# test(80000,10)
# test(8,100000)
# test(80,10000)
end
# test(8,20000)
test(80,2000)
# test(40000,100)
require "benchmark"
require "big"
# def fib(n : BigInt)
# n < 2 ? n : fib(n-1) + fib(n-2)
# end
struct BigInt
def add!(other : BigInt) : BigInt
LibGMP.add(self, self, other)
self
end
end
def fib(n, verbose)
a = BigInt.new(1)
b = BigInt.new(1)
# puts "a: #{a}, b: #{b}"
n.times do
a.add!(b)
a, b = b, a
# puts "a: #{a}, b: #{b}"
end
puts "a: #{a}, b: #{b}" if verbose
a
end
def test_fib(count, i, mycount, fib_qty)
# puts "i = #{i} " + "v"*40
# sleep(rand/qty)
fib(fib_qty)
# Thread.current["mycount"] = count
mycount[i] = count
count += 1
# puts "i = #{i} " + "^"*40
end
def test(thread_qty, fib_qty, loop_qty = 1)
puts "\nthread_qty: #{thread_qty}, fib_qty: #{fib_qty}, loop_qty: #{loop_qty}"
puts Benchmark.measure{
max_retries = 5
count = 0
arr = {} of Int32 => Thread
mycount = {} of Int32 => Int32
result = nil
thread_qty.times do |i|
verbose = i == (thread_qty - 1)
# arr[i] = Thread.new { test_fib(count, i, mycount, fib_qty) }
arr[i] = Thread.new {
loop_qty.times do |j|
# puts "i = #{i}, j: #{j} " + "v"*40
# sleep(rand/qty)
result = fib(fib_qty, verbose)
# Thread.current["mycount"] = count
mycount[i] = count
count += 1
puts("i = #{i}, j: #{j} " + "^"*40) if verbose
end
}
end
# arr.each {|t| t.join; print t["mycount"], ", " }
# arr.each {|i, t| t.join; print mycount[i], ", " }
arr.each {|i, t| t.join }
# arr.each {|i, t| print mycount[i], ", " }
puts "count = #{count}"
puts "result = #{result}"
}
puts "\n"
end
def multi_test
test(1,1)
test(1,2)
test(1,4)
test(1,40)
test(1,400)
test(1,4000)
test(8,1)
test(8,10)
# test(8,100)
# test(8,1000)
# test(8,1)
# test(8,2)
# test(8,4)
# test(8,8)
# test(8,16)
# test(8,32)
# test(8,64)
# test(8,128)
# test(8,1280)
# test(4,64)
# test(8,10)
# test(8,1000)
# test(8,10000)
# test(800,1000)
# test(80000,10)
# test(8,100000)
# test(80,10000)
end
# test(8,20000)
# test(80,2000)
# test(4,2000,20)
test(8,20000,10)
# test(40000,100)
require "benchmark"
require "big"
# def fib(n : BigInt)
# n < 2 ? n : fib(n-1) + fib(n-2)
# end
struct BigInt
def add!(other : BigInt) : BigInt
LibGMP.add(self, self, other)
self
end
end
def fib(n, verbose)
a = BigInt.new(1)
b = BigInt.new(1)
# puts "a: #{a}, b: #{b}"
n.times do
a.add!(b)
a, b = b, a
# puts "a: #{a}, b: #{b}"
end
puts "a: #{a}, b: #{b}" if verbose
a
end
def test_fib(count, i, mycount, fib_qty)
# puts "i = #{i} " + "v"*40
# sleep(rand/qty)
fib(fib_qty)
# Thread.current["mycount"] = count
mycount[i] = count
count += 1
# puts "i = #{i} " + "^"*40
end
def test(thread_qty, fib_qty)
puts "\nthread_qty: #{thread_qty}, fib_qty: #{fib_qty}"
puts Benchmark.measure{
count = 0
arr = {} of Int32 => Thread
mycount = {} of Int32 => Int32
result = nil
thread_qty.times do |i|
verbose = i == (thread_qty - 1)
# arr[i] = Thread.new { test_fib(count, i, mycount, fib_qty) }
arr[i] = Thread.new {
# puts "i = #{i} " + "v"*40
# sleep(rand/qty)
result = fib(fib_qty, verbose)
# Thread.current["mycount"] = count
mycount[i] = count
count += 1
puts("i = #{i} " + "^"*40) if verbose
}
end
# arr.each {|t| t.join; print t["mycount"], ", " }
# arr.each {|i, t| t.join; print mycount[i], ", " }
arr.each {|i, t| t.join }
# arr.each {|i, t| print mycount[i], ", " }
puts "count = #{count}"
puts "result = #{result}"
}
puts "\n"
end
def multi_test
test(1,1)
test(1,2)
test(1,4)
test(1,40)
test(1,400)
test(1,4000)
test(8,1)
test(8,10)
# test(8,100)
# test(8,1000)
# test(8,1)
# test(8,2)
# test(8,4)
# test(8,8)
# test(8,16)
# test(8,32)
# test(8,64)
# test(8,128)
# test(8,1280)
# test(4,64)
# test(8,10)
# test(8,1000)
# test(8,10000)
# test(800,1000)
# test(80000,10)
# test(8,100000)
# test(80,10000)
end
# test(8,20000)
test(80,2000)
# test(40000,100)
require "benchmark"
=begin
require "big"
# def fib(n : BigInt)
# n < 2 ? n : fib(n-1) + fib(n-2)
# end
struct BigInt
def add!(other : BigInt) : BigInt
LibGMP.add(self, self, other)
self
end
end
=end
def fib(n, verbose)
a = 1 # BigInt.new(1)
b = 1 # BigInt.new(1)
# puts "a: #{a}, b: #{b}"
n.times do
a += b
a, b = b, a
# puts "a: #{a}, b: #{b}"
end
puts "a: #{a}, b: #{b}" if verbose
a
end
def test_fib(count, i, mycount, fib_qty)
# puts "i = #{i} " + "v"*40
# sleep(rand/qty)
fib(fib_qty)
# Thread.current["mycount"] = count
mycount[i] = count
count += 1
# puts "i = #{i} " + "^"*40
end
def test(thread_qty, fib_qty)
puts "\nthread_qty: #{thread_qty}, fib_qty: #{fib_qty}"
puts Benchmark.measure {
count = 0
arr = {} # of Int32 => Thread
mycount = {} # of Int32 => Int32
result = nil
thread_qty.times do |i|
verbose = i == (thread_qty - 1)
# arr[i] = Thread.new { test_fib(count, i, mycount, fib_qty) }
arr[i] = Thread.new {
# puts "i = #{i} " + "v"*40
# sleep(rand/qty)
result = fib(fib_qty, verbose)
# Thread.current["mycount"] = count
mycount[i] = count
count += 1
puts("i = #{i} " + "^"*40) if verbose
}
end
# arr.each {|t| t.join; print t["mycount"], ", " }
# arr.each_pair {|i, t| t.join; print mycount[i], ", " }
puts "count = #{count}"
puts "result = #{result}"
}
puts "\n"
end
def multi_test
test(1,1)
test(1,2)
test(1,4)
test(1,40)
test(1,400)
test(1,4000)
test(8,1)
test(8,10)
# test(8,100)
# test(8,1000)
# test(8,1)
# test(8,2)
# test(8,4)
# test(8,8)
# test(8,16)
# test(8,32)
# test(8,64)
# test(8,128)
# test(8,1280)
# test(4,64)
# test(8,10)
# test(8,1000)
# test(8,10000)
# test(800,1000)
# test(80000,10)
# test(8,100000)
# test(80,10000)
end
# test(8,20000)
test(80,2000)
# test(40000,100)
require 'benchmark'
def fib(n)
n < 2 ? n : fib(n-1) + fib(n-2)
end
def test(thread_qty,fib_qty)
# puts "\n#{self.class.name}##{__method__} .. thread_qty: #{thread_qty}, fib_qty: #{fib_qty}"
puts "\nthread_qty: #{thread_qty}, fib_qty: #{fib_qty}"
puts Benchmark.measure{
count = 0
arr = []
thread_qty.times do |i|
arr[i] = Thread.new {
# sleep(rand/qty)
fib(fib_qty)
Thread.current["mycount"] = count
count += 1
}
end
arr.each {|t| t.join; print t["mycount"], ", " }
puts "count = #{count}"
}
puts "\n"
end
test(8,1)
test(8,2)
test(8,4)
test(8,8)
test(8,16)
test(8,32)
test(8,38)
# test(4,64)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment