Skip to content

Instantly share code, notes, and snippets.

@carlosantoniodasilva
Created April 29, 2012 20:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlosantoniodasilva/2553080 to your computer and use it in GitHub Desktop.
Save carlosantoniodasilva/2553080 to your computer and use it in GitHub Desktop.
Benches on PartialRenderer refactoring
## Running wycats/rails-simple-benches
Changes: https://github.com/carlosantoniodasilva/rails/compare/render-partial-refactor
### Master
$ time ruby minimal.rb
.........
overhead index template_1 partial partial_10 coll_10 partial_100 coll_100 uniq_100 diff_100
195 376 483 519 2144 1447 17997 11109 11438 14620
real 1m3.986s
user 1m1.266s
sys 0m0.489s
$ time ruby minimal.rb
.........
overhead index template_1 partial partial_10 coll_10 partial_100 coll_100 uniq_100 diff_100
188 371 498 532 2117 1424 18050 11125 11325 14720
real 1m1.902s
user 1m0.898s
sys 0m0.440s
$ time ruby minimal.rb
.........
overhead index template_1 partial partial_10 coll_10 partial_100 coll_100 uniq_100 diff_100
188 376 472 531 2167 1441 18028 11450 11721 14647
real 1m2.550s
user 1m1.480s
sys 0m0.409s
### Refactor
$ time ruby minimal.rb
.........
overhead index template_1 partial partial_10 coll_10 partial_100 coll_100 uniq_100 diff_100
187 394 524 694 2143 1441 18103 10985 11295 14550
real 1m1.858s
user 1m0.943s
sys 0m0.390s
$ time ruby minimal.rb
.........
overhead index template_1 partial partial_10 coll_10 partial_100 coll_100 uniq_100 diff_100
191 375 475 541 2096 1409 17876 10993 11191 14494
real 1m1.346s
user 1m0.494s
sys 0m0.420s
$ time ruby minimal.rb
.........
overhead index template_1 partial partial_10 coll_10 partial_100 coll_100 uniq_100 diff_100
190 374 472 540 2124 1437 18182 11177 11478 15174
real 1m2.677s
user 1m1.667s
sys 0m0.421s
require 'benchmark'
TIMES = 1_000_000
COLLECTION = %w(a b c d e)
AS = 'object'
COUNTER = 'counter'
Benchmark.bm do |x|
x.report("ech") do
TIMES.times do
segments, hash, index = [], {}, -1
COLLECTION.each do |item|
hash[AS] = item
hash[COUNTER] = (index += 1)
segments << item
end
segments
end
end
x.report("ehs") do
TIMES.times do
segments, hash = [], {}
hash[COUNTER] = -1
COLLECTION.each do |item|
hash[AS] = item
hash[COUNTER] += 1
segments << item
end
segments
end
end
x.report("edx") do
TIMES.times do
segments, hash = [], {}
COLLECTION.each_with_index do |item, index|
hash[AS] = item
hash[COUNTER] = index
segments << item
end
segments
end
end
x.report("map") do
TIMES.times do
hash, index = {}, -1
COLLECTION.map do |item|
hash[AS] = item
hash[COUNTER] = (index += 1)
item
end
end
end
x.report("mhs") do
TIMES.times do
hash = {}
hash[COUNTER] = -1
COLLECTION.map do |item|
hash[AS] = item
hash[COUNTER] += 1
item
end
end
end
x.report("mdx") do
TIMES.times do
hash = {}
COLLECTION.map.with_index do |item, index|
hash[AS] = item
hash[COUNTER] = index
item
end
end
end
end
=begin
Some results, apparently using Array#map with a local variable as counter is faster :)
### 100_000 times
$ ruby bench.rb
user system total real
ech 0.550000 0.000000 0.550000 ( 0.556331)
ehs 0.620000 0.010000 0.630000 ( 0.629074)
edx 0.560000 0.000000 0.560000 ( 0.569282)
map 0.490000 0.000000 0.490000 ( 0.490205)
mhs 0.560000 0.000000 0.560000 ( 0.558150)
mdx 0.570000 0.000000 0.570000 ( 0.578470)
$ ruby bench.rb
user system total real
ech 0.530000 0.010000 0.540000 ( 0.535028)
ehs 0.600000 0.000000 0.600000 ( 0.601165)
edx 0.560000 0.000000 0.560000 ( 0.575481)
map 0.490000 0.000000 0.490000 ( 0.485297)
mhs 0.540000 0.000000 0.540000 ( 0.539671)
mdx 0.570000 0.000000 0.570000 ( 0.577498)
$ ruby bench.rb
user system total real
ech 0.550000 0.010000 0.560000 ( 0.553918)
ehs 0.590000 0.000000 0.590000 ( 0.596388)
edx 0.580000 0.000000 0.580000 ( 0.584590)
map 0.500000 0.000000 0.500000 ( 0.511220)
mhs 0.560000 0.000000 0.560000 ( 0.561856)
mdx 0.600000 0.000000 0.600000 ( 0.617899)
### 1_000_000 times
$ ruby bench.rb
user system total real
ech 5.350000 0.030000 5.380000 ( 5.684627)
ehs 6.030000 0.030000 6.060000 ( 6.268418)
edx 5.670000 0.010000 5.680000 ( 6.145154)
map 4.820000 0.010000 4.830000 ( 5.049767)
mhs 5.480000 0.010000 5.490000 ( 5.714076)
mdx 5.790000 0.010000 5.800000 ( 5.820524)
$ ruby bench.rb
user system total real
ech 5.360000 0.020000 5.380000 ( 5.735686)
ehs 5.960000 0.000000 5.960000 ( 6.034750)
edx 5.640000 0.010000 5.650000 ( 5.711159)
map 4.920000 0.010000 4.930000 ( 5.029512)
mhs 5.580000 0.020000 5.600000 ( 5.858194)
mdx 5.930000 0.010000 5.940000 ( 6.015423)
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment