Last active
March 2, 2016 09:37
-
-
Save peteygao/63870f81db380f63e429 to your computer and use it in GitHub Desktop.
Ruby micro benchmark code to test across implementations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def bench name | |
warmup = yield | |
puts "Warming up '##{name}'" | |
GC.start | |
first = yield | |
GC.start | |
second = yield | |
GC.start | |
third = yield | |
puts "Average of 3 runs: #{(first + second + third) / 3}" | |
puts "=" * 50 | |
end | |
def sum count | |
num = (1..count).map { |n| n } | |
start = Time.now | |
num.inject(0) { |sum, n| sum + n } | |
Time.now - start | |
end | |
def md5 count, threads=nil | |
require 'parallel' | |
require 'digest/md5' | |
if threads | |
num = (1..count).map { |n| n } | |
start = Time.now | |
Parallel.map(num, :in_threads => threads) { |n| Digest::MD5.hexdigest n.to_s } | |
Time.now - start | |
else | |
i = 0 | |
start = Time.now | |
while i < count | |
i += 1 | |
Digest::MD5.hexdigest i.to_s | |
end | |
Time.now - start | |
end | |
end | |
def arithmetic count | |
i = 0 | |
start = Time.now | |
while i < count | |
i += 1 | |
count + count | |
count - count | |
count / count | |
count * count | |
end | |
Time.now - start | |
end | |
def array_shovel count | |
i = 0 | |
start = Time.now | |
array = [] | |
while i < count | |
i += 1 | |
array << i | |
end | |
Time.now - start | |
end | |
def array_set_ops count | |
i = 0 | |
set1 = (1..11_000).map { |n| n } | |
set2 = (9_000..22_000).map { |n| n } | |
start = Time.now | |
while i < count | |
i += 1 | |
set1 & set2 # Union | |
set1 | set2 # Intersection | |
set1 - set2 # Difference | |
end | |
Time.now - start | |
end | |
def set_operations count | |
require 'set' | |
i = 0 | |
set1 = Set.new (1..11_000).map { |n| n } | |
set2 = Set.new (9_000..20_000).map { |n| n } | |
start = Time.now | |
while i < count | |
i += 1 | |
set1.union set2 | |
set1.intersection set2 | |
set1.difference set2 | |
end | |
Time.now - start | |
end | |
def array_filtering count | |
i = 0 | |
array = (1..count).map { |n| n } | |
start = Time.now | |
while i < count | |
i += 1 | |
array.reject { |n| n != count } | |
array.select { |n| n != count } | |
end | |
Time.now - start | |
end | |
bench("sum 10 million elements") { |i| sum 10_000_000 } | |
bench("md5 200k elements with NO threading") { |i| md5 200_000 } | |
bench("md5 200k elements with 1 thread") { |i| md5 200_000, 1 } | |
bench("md5 200k elements with 2 threads") { |i| md5 200_000, 2 } | |
bench("md5 200k elements with 3 threads") { |i| md5 200_000, 3 } | |
bench("md5 200k elements with 4 threads") { |i| md5 200_000, 4 } | |
bench("arithmetic 10 million elements") { |i| arithmetic 10_000_000 } | |
bench("array_shovel 10 million elements") { |i| array_shovel 10_000_000 } | |
bench("set_operations 100 times") { |i| set_operations 100 } | |
bench("array_set_ops 100 times") { |i| array_set_ops 100 } | |
bench("array_filtering 2000 times") { |i| array_filtering 2000 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment