Last active
December 9, 2015 23:31
-
-
Save ChrisLundquist/d06b6cdf1c208d2e7f18 to your computer and use it in GitHub Desktop.
This file contains 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
$ ruby -v | |
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14] | |
Rehearsal -------------------------------------------------------------- | |
map_compact 0.020000 0.000000 0.020000 ( 0.015315) | |
map_inplace_compact 0.010000 0.000000 0.010000 ( 0.016069) | |
reject_map 0.010000 0.000000 0.010000 ( 0.014038) | |
reject_inplace_map 0.860000 0.010000 0.870000 ( 0.860999) | |
reject_map_inplace 0.010000 0.000000 0.010000 ( 0.013710) | |
reject_inplace_map_inplace 0.860000 0.000000 0.860000 ( 0.866945) | |
----------------------------------------------------- total: 1.780000sec | |
user system total real | |
map_compact 0.010000 0.000000 0.010000 ( 0.012554) | |
map_inplace_compact 0.010000 0.000000 0.010000 ( 0.012114) | |
reject_map 0.010000 0.000000 0.010000 ( 0.021661) | |
reject_inplace_map 0.850000 0.000000 0.850000 ( 0.855091) | |
reject_map_inplace 0.010000 0.000000 0.010000 ( 0.015349) | |
reject_inplace_map_inplace 0.870000 0.000000 0.870000 ( 0.876531) |
This file contains 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
#!/usr/bin/env ruby | |
require 'benchmark' | |
def data | |
(0...100_000).to_a | |
end | |
def map_compact | |
data.map do |i| | |
next if i.odd? | |
i + i | |
end.compact | |
end | |
def map_inplace_compact | |
data.map! do |i| | |
next if i.odd? | |
i + i | |
end.compact | |
end | |
def reject_map | |
data.reject { |i| i.odd? }.map { |i| i + i } | |
end | |
def reject_inplace_map | |
data.reject! { |i| i.odd? }.map { |i| i + i } | |
end | |
def reject_inplace_map_inplace | |
data.reject! { |i| i.odd? }.map! { |i| i + i } | |
end | |
def reject_map_inplace | |
data.reject { |i| i.odd? }.map! { |i| i + i } | |
end | |
Benchmark.bmbm do |x| | |
x.report("map_compact") { map_compact } | |
x.report("map_inplace_compact") { map_inplace_compact } | |
x.report("reject_map") { reject_map } | |
x.report("reject_inplace_map") { reject_inplace_map } | |
x.report("reject_map_inplace") { reject_map_inplace } | |
x.report("reject_inplace_map_inplace") { reject_inplace_map_inplace } | |
end |
require "time"
def data(size)
(0...size).to_a
end
def reject_inplace(size)
filtered = data(size)
filtered = filtered.reject! { |i| i.odd? }
filtered
end
def graph
40.times.reduce({}) do |acc,i|
size = ( i + 1) * 10000
start = Time.now
reject_inplace(size)
finish = Time.now
duration = finish - start
acc[size] = duration
acc
end
end
data = graph
data.keys.sort.each do |k| puts("#{ k },#{data[k]}") end
That's a pretty good N^2 curve fit.
Today I learned not to use reject!
until ruby > 2.2.3
Fixed in ruby/ruby@5ec029d
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using ruby-prof and 1_000_000 elements it took 262 seconds to run.
Using only 1_000_000 elements and no profiler took 131s.
100_000 elements took about 5.5 seconds