Last active
December 24, 2015 09:49
-
-
Save tessi/6779700 to your computer and use it in GitHub Desktop.
Revisions
-
tessi revised this gist
Oct 1, 2013 . 2 changed files with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,7 @@ def use_map_join(arr) def use_inject(arr) join_str = ', ' arr.inject(nil) {|str, arr| str ? (str << join_str << arr[2]) : arr[2].dup} end Benchmark.bm do |bm| This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,3 @@ user system total real map/join 1.440000 0.000000 1.440000 ( 1.441858) inject 2.220000 0.000000 2.220000 ( 2.234554) -
tessi created this gist
Oct 1, 2013 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ require 'benchmark' iterations = 10_000 arr = Array.new(1000) { Array.new(2) << (rand()*100).to_s} def use_map_join(arr) arr.map {|a| a[2]}.join ', ' end def use_inject(arr) join_str = ', ' arr.inject("") {|str, arr| str.empty? ? arr[2] : (str << join_str + arr[2])} end Benchmark.bm do |bm| bm.report('map/join') do iterations.times do use_map_join arr end end bm.report('inject') do iterations.times do use_inject arr end end end This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ user system total real map/join 1.440000 0.000000 1.440000 ( 1.450691) inject 2.540000 0.040000 2.580000 ( 2.580606)