Skip to content

Instantly share code, notes, and snippets.

@aboltart
Created September 24, 2014 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aboltart/f90cab21db56073ff947 to your computer and use it in GitHub Desktop.
Save aboltart/f90cab21db56073ff947 to your computer and use it in GitHub Desktop.
Ruby benchmarking test cases
#===========================================
# 1. Benchmark
#
require 'benchmark'
Benchmark.bmbm do |x|
x.report ('<<') do
1_000_000.times do
one = 'one'
two = 'two'
one << two
end
end
x.report('+') do
1_000_000.times do
one = 'one'
two = 'two'
one + two
end
end
x.report('#{one}#{two}') do
1_000_000.times do
one = 'one'
two = 'two'
"#{one} #{two}"
end
end
x.report('concat') do
1_000_000.times do
one = 'one'
two = 'two'
one.concat(two)
end
end
end
#===========================================
# 2. Benchmark
#
require 'benchmark'
Benchmark.bmbm do |x|
x.report ('<<') do
1_000_000.times do
one = 'one'*1000
two = 'two'*1000
one << two
end
end
x.report('+') do
1_000_000.times do
one = 'one'*1000
two = 'two'*1000
one + two
end
end
x.report('#{one}#{two}') do
1_000_000.times do
one = 'one'*1000
two = 'two'*1000
"#{one} #{two}"
end
end
x.report('concat') do
1_000_000.times do
one = 'one'*1000
two = 'two'*1000
one.concat(two)
end
end
end
#===========================================
# 3. Benchmark
#
require 'benchmark'
Benchmark.bmbm do |x|
x.report ('-') do
1_000_000.times do
["Ru", "by"]*"-"
end
end
x.report ('join') do
1_000_000.times do
["Ru", "by"].join("-")
end
end
end
#===========================================
# 4. Benchmark
#
require 'benchmark'
Benchmark.bmbm do |x|
x.report ('double') do
10_000_000.times do
"Hello world!"
end
end
x.report ('single') do
10_000_000.times do
'Hello world!'
end
end
x.report ('%q') do
10_000_000.times do
%q{Hello world!}
end
end
end
#===========================================
# 5. Benchmark
#
require 'benchmark'
Benchmark.bmbm do |x|
x.report ('each_with_index') do
100_000.times do
[*1..100].each_with_index do |val, i|
val
end
end
end
x.report ('while loop') do
100_000.times do
i = 0
array = [*1..100]
while i < array.size
array[i]
i += 1
end
end
end
end
#===========================================
# 6. Benchmark
#
require 'benchmark'
st1 = 'tests'
st2 = 'tests'
sym1 = :tests
sym2 = :tests
Benchmark.bmbm do |x|
x.report ('string') do
10_000_000.times do
st1 == st2
end
end
x.report ('symbol') do
10_000_000.times do
sym1 == sym2
end
end
end
#===========================================
# 7. Benchmark
#
require 'benchmark'
GC.disable
Benchmark.bmbm do |x|
x.report ('string') do
10_000_000.times do
'tests'
end
end
x.report ('symbol') do
10_000_000.times do
:tests
end
end
end
#===========================================
# 8. Benchmark
#
require 'benchmark'
GC.disable
N = 10000
Benchmark.bmbm do |x|
x.report("define_method") {
class Foo
N.times { |i| define_method("foo_#{i}") { } }
end
}
x.report("class_eval") {
class Bar
N.times { |i|
class_eval <<-eoruby, __FILE__, __LINE__ + 1
def bar_#{i}
end
eoruby
}
end
}
end
#===========================================
# 9. Benchmark
#
require 'benchmark'
class Metric
attr_accessor :var
def initialize(n)
@n = n
@var = 22
end
def run
Benchmark.bmbm do |x|
x.report("@var") { @n.times { @var } }
x.report("var" ) { @n.times { var } }
x.report("@var =") { @n.times {|i| @var = i } }
x.report("self.var =") { @n.times {|i| self.var = i } }
end
end
end
metric = Metric.new(100_000_000)
metric.run
#===========================================
# 10. Benchmark
#
require 'benchmark'
N = 10_000_000
Benchmark.bmbm do |x|
x.report('parallel') do
N.times do
a, b = 10, 20
end
end
x.report('consequentially') do |x|
N.times do
a = 10
b = 20
end
end
end
#===========================================
# 11. Benchmark
#
require 'benchmark'
N = 100_000
r = (1..100)
Benchmark.bmbm do |x|
x.report('block') do
N.times do
r.map{ |i| i.to_s }
end
end
x.report('proc') do |x|
N.times do
r.map(&:to_s)
end
end
end
#===========================================
# 12. Benchmark
#
require 'benchmark'
N = 100_000
h = {:a => 123}
Benchmark.bmbm do |x|
x.report('fetch') do
N.times do
h.fetch(:a, [*1..10].join('-'))
end
end
x.report('fetch proc') do |x|
N.times do
h.fetch(:a) {[*1..10].join('-')}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment