Skip to content

Instantly share code, notes, and snippets.

@lcowell
Created November 7, 2011 00:12
Show Gist options
  • Save lcowell/1343845 to your computer and use it in GitHub Desktop.
Save lcowell/1343845 to your computer and use it in GitHub Desktop.
Benchmarking
n = 1_000_000
start_time = Time.now
for i in 1..n; a = "1"; end
end_time = Time.now
puts "total time for the operation was #{end_time - start_time} seconds"
start_time = Time.now
n.times do; a = "1"; end
end_time = Time.now
puts "total time for the operation was #{end_time - start_time} seconds"
start_time = Time.now
1.upto(n) do ; a = "1"; end
end_time = Time.now
puts "total time for the operation was #{end_time - start_time} seconds"
n = 1_000_000
def benchmark(title = 'the operation')
start_time = Time.now
yield
end_time = Time.now
puts "total time for #{title} was #{end_time - start_time} seconds"
end
benchmark("for") { for i in 1..n; a = "1"; end }
benchmark("times") { n.times do; a = "1"; end }
benchmark("upto") { 1.upto(n) do ; a = "1"; end }
require 'benchmark'
n = 1_000_000
Benchmark.bm(5) do |b|
b.report("for") { for i in 1..n; a = "1"; end }
b.report("times") { n.times do; a = "1"; end }
b.report("upto") { 1.upto(n) do ; a = "1"; end }
end
require 'benchmark'
require 'find'
Benchmark.bm do |b|
b.report("fs_scan") { Find.find(File.expand_path('~/Library/Preferences')) { |f| } }
end
# user system total real
# fs_scan 0.010000 0.030000 0.040000 ( 0.627269)
# user system total real
# fs_scan 0.000000 0.000000 0.000000 ( 0.007788)
require 'benchmark'
require 'find'
Benchmark.bmbm do |b|
b.report("fs_scan") { Find.find(File.expand_path('~/Library/Application Support')) { |f| } }
end
#Rehearsal
#-------------------------------------------
#fs_scan 0.490000 1.830000 2.320000 ( 13.592359)
#---------------------------------- total: 2.320000sec
#
# user system total real
#fs_scan 0.440000 0.830000 1.270000 ( 1.263131)
require 'benchmark'
test_scores = 500.times.inject([]) {|acc, a| acc << rand(100)}
count = 1000
my_proc = Proc.new {|x| x.even? }
Benchmark.bm(14) do |x|
x.report("map cached-proc") { count.times {test_scores.map(&my_proc)}}
x.report("map proc") { count.times {test_scores.map(&:even?)}}
x.report("map block") { count.times {test_scores.map {|n| n.even?}}}
end
# count = 1
# user system total real
# map proc 0.000000 0.000000 0.000000 ( 0.000042)
# map block 0.000000 0.000000 0.000000 ( 0.000047)
# count = 1000
# user system total real
# map-proc 0.010000 0.000000 0.010000 ( 0.006569)
# map 0.000000 0.000000 0.000000 ( 0.009616)
require 'benchmark'
string1 = "hello" * 1000
string2 = "world" * 1000
count = 1000
Benchmark.bmbm(10) do |b|
b.report("+") { count.times {string1 + string2} }
b.report("interpolate") { count.times {"#{string1}#{string2}" } }
b.report("concat") { count.times {string1 << string2 } }
end
# Rehearsal -----------------------------------------------
# + 0.000000 0.000000 0.000000 ( 0.004151)
# interpolate 0.000000 0.000000 0.000000 ( 0.002999)
# concat 0.010000 0.000000 0.010000 ( 0.002928)
# -------------------------------------- total: 0.010000sec
#
# user system total real
# + 1.170000 0.470000 1.640000 ( 1.645940)
# interpolate 1.570000 2.450000 4.020000 ( 4.024045)
# concat 0.000000 0.010000 0.010000 ( 0.005437)
require 'benchmark'
a, b, c = [], [], []
1_000_000.times { a << rand; b << rand; c << rand }
Benchmark.bmbm(12) do |x|
x.report("sort") { a.sort }
x.report("sort_by") { b.sort_by {|n| n} }
x.report("sort!") { c.sort! }
x.report("sort join split") { a.sort.join(",").split(",") }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment