Skip to content

Instantly share code, notes, and snippets.

@etagwerker
Created March 4, 2016 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save etagwerker/23949d92fecae9a0bf9e to your computer and use it in GitHub Desktop.
Save etagwerker/23949d92fecae9a0bf9e to your computer and use it in GitHub Desktop.
benchmark-hash-default.rb
# Code
def do_benchmark
n = 5000
Benchmark.bm do |x|
x.report "|| approach" do
n.times do
{}[:a] || :b
end
end
x.report "fetch with default value" do
n.times do
{}.fetch(:a, :b)
end
end
x.report "fetch with default block" do
n.times do
{}.fetch(:a) { :b }
end
end
end
end
# Results
2.2.1 :096 > do_benchmark
user system total real
|| approach 0.000000 0.000000 0.000000 ( 0.001169)
fetch with default value 0.000000 0.000000 0.000000 ( 0.001724)
fetch with default block 0.000000 0.000000 0.000000 ( 0.002314)
=> [#<Benchmark::Tms:0x007fb5a086c0d8 @label="|| approach", @real=0.0011689929524436593, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a203a660 @label="fetch with default value", @real=0.0017244820483028889, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a10653e8 @label="fetch with default block", @real=0.0023141619749367237, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>]
2.2.1 :097 > do_benchmark
user system total real
|| approach 0.000000 0.000000 0.000000 ( 0.001379)
fetch with default value 0.010000 0.000000 0.010000 ( 0.001279)
fetch with default block 0.000000 0.000000 0.000000 ( 0.002079)
=> [#<Benchmark::Tms:0x007fb5a10dc920 @label="|| approach", @real=0.0013789760414510965, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a10871c8 @label="fetch with default value", @real=0.0012793209170922637, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.010000000000000009, @total=0.010000000000000009>, #<Benchmark::Tms:0x007fb5a089c328 @label="fetch with default block", @real=0.0020792909199371934, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>]
2.2.1 :098 > do_benchmark
user system total real
|| approach 0.000000 0.000000 0.000000 ( 0.001368)
fetch with default value 0.000000 0.000000 0.000000 ( 0.001277)
fetch with default block 0.000000 0.000000 0.000000 ( 0.002081)
=> [#<Benchmark::Tms:0x007fb5a10ed9f0 @label="|| approach", @real=0.0013683970319107175, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a20107e8 @label="fetch with default value", @real=0.0012765320716425776, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a083e098 @label="fetch with default block", @real=0.0020807109540328383, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>]
2.2.1 :099 > do_benchmark
user system total real
|| approach 0.000000 0.000000 0.000000 ( 0.001376)
fetch with default value 0.000000 0.000000 0.000000 ( 0.001301)
fetch with default block 0.000000 0.000000 0.000000 ( 0.002378)
=> [#<Benchmark::Tms:0x007fb5a10a6668 @label="|| approach", @real=0.0013761849841102958, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a102d948 @label="fetch with default value", @real=0.0013013830175623298, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a111e118 @label="fetch with default block", @real=0.002378238015808165, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>]
2.2.1 :100 > do_benchmark
user system total real
|| approach 0.000000 0.000000 0.000000 ( 0.001186)
fetch with default value 0.000000 0.000000 0.000000 ( 0.001291)
fetch with default block 0.010000 0.000000 0.010000 ( 0.002262)
=> [#<Benchmark::Tms:0x007fb5a2029ef0 @label="|| approach", @real=0.0011858739890158176, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a085d8d0 @label="fetch with default value", @real=0.0012913430109620094, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a10c7f70 @label="fetch with default block", @real=0.0022619999945163727, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.010000000000000009, @total=0.010000000000000009>]
2.2.1 :101 > do_benchmark
user system total real
|| approach 0.000000 0.000000 0.000000 ( 0.001247)
fetch with default value 0.000000 0.000000 0.000000 ( 0.001518)
fetch with default block 0.000000 0.000000 0.000000 ( 0.002085)
=> [#<Benchmark::Tms:0x007fb5a1046bf0 @label="|| approach", @real=0.001247109961695969, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a1135598 @label="fetch with default value", @real=0.001518410979770124, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a1076440 @label="fetch with default block", @real=0.0020848129643127322, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>]
2.2.1 :102 > do_benchmark
user system total real
|| approach 0.000000 0.000000 0.000000 ( 0.001173)
fetch with default value 0.010000 0.000000 0.010000 ( 0.001470)
fetch with default block 0.000000 0.000000 0.000000 ( 0.002073)
=> [#<Benchmark::Tms:0x007fb5a0874210 @label="|| approach", @real=0.001173084951005876, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a1834e20 @label="fetch with default value", @real=0.0014698469312861562, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.010000000000000009, @total=0.010000000000000009>, #<Benchmark::Tms:0x007fb5a1067788 @label="fetch with default block", @real=0.002073268056847155, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>]
2.2.1 :103 > do_benchmark
user system total real
|| approach 0.000000 0.000000 0.000000 ( 0.001381)
fetch with default value 0.000000 0.000000 0.000000 ( 0.001286)
fetch with default block 0.000000 0.000000 0.000000 ( 0.002091)
=> [#<Benchmark::Tms:0x007fb5a10de4f0 @label="|| approach", @real=0.001381426933221519, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a1087768 @label="fetch with default value", @real=0.0012855048989877105, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a0897b48 @label="fetch with default block", @real=0.0020906709833070636, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>]
2.2.1 :104 > do_benchmark
user system total real
|| approach 0.010000 0.000000 0.010000 ( 0.001378)
fetch with default value 0.000000 0.000000 0.000000 ( 0.001286)
fetch with default block 0.000000 0.000000 0.000000 ( 0.002579)
=> [#<Benchmark::Tms:0x007fb5a10e72f8 @label="|| approach", @real=0.0013783000176772475, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.010000000000000009, @total=0.010000000000000009>, #<Benchmark::Tms:0x007fb5a2009678 @label="fetch with default value", @real=0.0012857279507443309, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a08361b8 @label="fetch with default block", @real=0.002579198917374015, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>]
2.2.1 :105 > do_benchmark
user system total real
|| approach 0.000000 0.000000 0.000000 ( 0.001368)
fetch with default value 0.000000 0.000000 0.000000 ( 0.001271)
fetch with default block 0.010000 0.000000 0.010000 ( 0.002272)
=> [#<Benchmark::Tms:0x007fb5a109cf28 @label="|| approach", @real=0.0013679140247404575, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a08bd028 @label="fetch with default value", @real=0.001270572072826326, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a110faf0 @label="fetch with default block", @real=0.0022715789964422584, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.010000000000000009, @total=0.010000000000000009>]
2.2.1 :106 > do_benchmark
user system total real
|| approach 0.000000 0.000000 0.000000 ( 0.001195)
fetch with default value 0.000000 0.000000 0.000000 ( 0.001428)
fetch with default block 0.000000 0.000000 0.000000 ( 0.002308)
=> [#<Benchmark::Tms:0x007fb5a2013da8 @label="|| approach", @real=0.00119479198474437, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a084ca08 @label="fetch with default value", @real=0.0014280170435085893, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a10b4ba0 @label="fetch with default block", @real=0.0023079129168763757, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>]
2.2.1 :107 > do_benchmark
user system total real
|| approach 0.000000 0.000000 0.000000 ( 0.001160)
fetch with default value 0.010000 0.000000 0.010000 ( 0.001482)
fetch with default block 0.000000 0.000000 0.000000 ( 0.003317)
=> [#<Benchmark::Tms:0x007fb5a102cd68 @label="|| approach", @real=0.0011602899758145213, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a111d010 @label="fetch with default value", @real=0.001482360064983368, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.009999999999999953, @total=0.009999999999999953>, #<Benchmark::Tms:0x007fb5a202aa58 @label="fetch with default block", @real=0.0033169089583680034, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>]
2.2.1 :108 > do_benchmark
user system total real
|| approach 0.000000 0.000000 0.000000 ( 0.001170)
fetch with default value 0.000000 0.000000 0.000000 ( 0.001494)
fetch with default block 0.000000 0.000000 0.000000 ( 0.002081)
=> [#<Benchmark::Tms:0x007fb5a08557e8 @label="|| approach", @real=0.0011701580369845033, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a10bee48 @label="fetch with default value", @real=0.0014943029964342713, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a103d410 @label="fetch with default block", @real=0.002081056940369308, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>]
2.2.1 :109 > do_benchmark
user system total real
|| approach 0.000000 0.000000 0.000000 ( 0.001446)
fetch with default value 0.000000 0.000000 0.000000 ( 0.001808)
fetch with default block 0.000000 0.000000 0.000000 ( 0.002541)
=> [#<Benchmark::Tms:0x007fb5a1124180 @label="|| approach", @real=0.001446381094865501, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a180a080 @label="fetch with default value", @real=0.0018075749976560473, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a085fc70 @label="fetch with default block", @real=0.0025410379748791456, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>]
2.2.1 :110 > do_benchmark
user system total real
|| approach 0.000000 0.000000 0.000000 ( 0.001376)
fetch with default value 0.000000 0.000000 0.000000 ( 0.001280)
fetch with default block 0.010000 0.000000 0.010000 ( 0.002236)
=> [#<Benchmark::Tms:0x007fb5a10c59c8 @label="|| approach", @real=0.0013759320136159658, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a1045d68 @label="fetch with default value", @real=0.0012795100919902325, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a112ea90 @label="fetch with default block", @real=0.0022356429835781455, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.010000000000000009, @total=0.010000000000000009>]
2.2.1 :111 > do_benchmark
user system total real
|| approach 0.000000 0.000000 0.000000 ( 0.001175)
fetch with default value 0.000000 0.000000 0.000000 ( 0.001288)
fetch with default block 0.000000 0.000000 0.000000 ( 0.002283)
=> [#<Benchmark::Tms:0x007fb5a180b688 @label="|| approach", @real=0.0011745910160243511, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a0866700 @label="fetch with default value", @real=0.00128799001686275, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a20301d8 @label="fetch with default block", @real=0.0022833129623904824, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>]
2.2.1 :112 > do_benchmark
user system total real
|| approach 0.000000 0.000000 0.000000 ( 0.001169)
fetch with default value 0.000000 0.000000 0.000000 ( 0.001695)
fetch with default block 0.000000 0.000000 0.000000 ( 0.002088)
=> [#<Benchmark::Tms:0x007fb5a1046f88 @label="|| approach", @real=0.0011687609367072582, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a112fc60 @label="fetch with default value", @real=0.001694898004643619, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a106e498 @label="fetch with default block", @real=0.0020879299845546484, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>]
2.2.1 :113 > do_benchmark
user system total real
|| approach 0.000000 0.000000 0.000000 ( 0.001163)
fetch with default value 0.000000 0.000000 0.000000 ( 0.001500)
fetch with default block 0.010000 0.000000 0.010000 ( 0.002586)
=> [#<Benchmark::Tms:0x007fb5a0865df0 @label="|| approach", @real=0.0011634649708867073, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a20304d0 @label="fetch with default value", @real=0.0015002579893916845, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a1054c50 @label="fetch with default block", @real=0.0025856479769572616, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.010000000000000009, @total=0.010000000000000009>]
2.2.1 :114 > do_benchmark
user system total real
|| approach 0.000000 0.000000 0.000000 ( 0.001390)
fetch with default value 0.000000 0.000000 0.000000 ( 0.001321)
fetch with default block 0.000000 0.000000 0.000000 ( 0.002959)
=> [#<Benchmark::Tms:0x007fb5a112e608 @label="|| approach", @real=0.0013902030186727643, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a106cff8 @label="fetch with default value", @real=0.0013213619822636247, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a086cf38 @label="fetch with default block", @real=0.0029587969183921814, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>]
2.2.1 :115 > do_benchmark
user system total real
|| approach 0.000000 0.000000 0.000000 ( 0.001369)
fetch with default value 0.010000 0.000000 0.010000 ( 0.001264)
fetch with default block 0.000000 0.000000 0.000000 ( 0.002244)
=> [#<Benchmark::Tms:0x007fb5a10ccca0 @label="|| approach", @real=0.001368626020848751, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>, #<Benchmark::Tms:0x007fb5a104e170 @label="fetch with default value", @real=0.0012638730695471168, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.010000000000000009, @total=0.010000000000000009>, #<Benchmark::Tms:0x007fb5a11347d8 @label="fetch with default block", @real=0.002244106028228998, @cstime=0.0, @cutime=0.0, @stime=0.0, @utime=0.0, @total=0.0>]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment