From ff57f1f6ac4dd03a447f745afe4b4784860a86f1 Mon Sep 17 00:00:00 2001 From: alekseyl Date: Sat, 5 Nov 2022 18:49:52 +0300 Subject: [PATCH 1/9] randomized string concatenation comparison --- code/string/concatenation_randomized.rb | 104 ++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 code/string/concatenation_randomized.rb diff --git a/code/string/concatenation_randomized.rb b/code/string/concatenation_randomized.rb new file mode 100644 index 0000000..b58f3af --- /dev/null +++ b/code/string/concatenation_randomized.rb @@ -0,0 +1,104 @@ +require 'benchmark/ips' + +module RandStr + RND_STRINGS_AMOUNT = 1000 + @rand_strs = { + lt100: [], + lt10: [], + lt1000: [], + eq10: [], + eq100: [], + } + + def self.generate_rand_strs + chars = ('A'..'z').to_a * 20 + RND_STRINGS_AMOUNT.times do + @rand_strs[:lt10] << chars.sample( rand(10) ).join + @rand_strs[:lt100] << chars.sample( rand(100) ).join + @rand_strs[:lt1000] << chars.sample( rand(1000) ).join + @rand_strs[:eq10] << chars.sample(10).join + @rand_strs[:eq100] << chars.sample(100).join + end + end + + self.generate_rand_strs + + def self.rand_str( named_range ) + @rand_strs[named_range][rand(RND_STRINGS_AMOUNT)] + end + + def self.method_missing(symbol) + return super unless @rand_strs.keys.include?(symbol) + + define_singleton_method(symbol) { rand_str( symbol ) } + return rand_str( symbol ) + end + +end + + +# 2 + 1 = 3 object +def slow_plus(foo, bar) + foo + bar +end + +# 2 + 1 = 3 object +def slow_concat(foo, bar) + foo.concat bar +end + +# 2 + 1 = 3 object +def slow_append(foo, bar) + foo << bar +end + + +def fast_interpolation(foo, bar) + "#{foo}#{bar}" +end + +def bench_100_to_100 + Benchmark.ips(2) do |x| + sarr1, sarr2 = [], [] + (x.time * 5_000_000).times { sarr1 << RandStr.eq100.dup } + (x.time * 5_000_000).times { sarr2 << RandStr.eq100.dup } + + i, j, k = 0, 0, 0 + x.report("String#+") { k+=1; sarr1[k]; slow_plus(RandStr.eq100, RandStr.lt100) } + x.report('"#{\'foo\'}#{\'bar\'}"') { k+=1; sarr2[k]; fast_interpolation(RandStr.eq100, RandStr.lt100) } + x.report("String#concat") { i+=1; RandStr.eq100; slow_concat(sarr1[i], RandStr.lt100) } + x.report("String#append") { j+=1; RandStr.eq100; slow_append(sarr2[j], RandStr.lt100) } + x.compare! + end; :done +end + +def bench_100_to_1000 + Benchmark.ips(2) do |x| + sarr1, sarr2 = [], [] + (x.time * 5_000_000).times { sarr1 << RandStr.eq100.dup } + (x.time * 5_000_000).times { sarr2 << RandStr.eq100.dup } + + i, j, k = 0, 0, 0 + x.report("String#+") { k+=1; sarr1[k]; slow_plus(RandStr.eq100, RandStr.lt1000) } + x.report('"#{\'foo\'}#{\'bar\'}"') { k+=1; sarr2[k]; fast_interpolation(RandStr.eq100, RandStr.lt1000) } + x.report("String#concat") { i+=1; RandStr.eq100; slow_concat(sarr1[i], RandStr.lt1000) } + x.report("String#append") { j+=1; RandStr.eq100; slow_append(sarr2[j], RandStr.lt1000) } + x.compare! + end; :done +end + +def bench_10_to_100 + Benchmark.ips(2) do |x| + sarr1, sarr2 = [], [] + (x.time * 5_000_000).times { sarr1 << RandStr.eq10.dup } + (x.time * 5_000_000).times { sarr2 << RandStr.eq10.dup } + + i, j, k = 0, 0, 0 + x.report("String#+") { k+=1; sarr1[k]; slow_plus(RandStr.eq10, RandStr.lt100) } + x.report('"#{\'foo\'}#{\'bar\'}"') { k+=1; sarr2[k]; fast_interpolation(RandStr.eq10, RandStr.lt100) } + x.report("String#concat") { i+=1; RandStr.eq10; slow_concat(sarr1[i], RandStr.lt100) } + x.report("String#append") { j+=1; RandStr.eq10; slow_append(sarr2[j], RandStr.lt100) } + x.compare! + end; :done +end + From 9aa67f8fb5647cbcbf99e18756482796b5a547b0 Mon Sep 17 00:00:00 2001 From: alekseyl Date: Sat, 12 Nov 2022 12:45:54 +0300 Subject: [PATCH 2/9] bmbm approach + collateral actions extracted and measured --- code/string/concatenation_randomized.rb | 144 ++++++++++++++++-------- 1 file changed, 98 insertions(+), 46 deletions(-) diff --git a/code/string/concatenation_randomized.rb b/code/string/concatenation_randomized.rb index b58f3af..ea9db21 100644 --- a/code/string/concatenation_randomized.rb +++ b/code/string/concatenation_randomized.rb @@ -1,4 +1,4 @@ -require 'benchmark/ips' +require 'benchmark' module RandStr RND_STRINGS_AMOUNT = 1000 @@ -12,13 +12,11 @@ module RandStr def self.generate_rand_strs chars = ('A'..'z').to_a * 20 - RND_STRINGS_AMOUNT.times do - @rand_strs[:lt10] << chars.sample( rand(10) ).join - @rand_strs[:lt100] << chars.sample( rand(100) ).join - @rand_strs[:lt1000] << chars.sample( rand(1000) ).join - @rand_strs[:eq10] << chars.sample(10).join - @rand_strs[:eq100] << chars.sample(100).join - end + @rand_strs[:lt10] = Array.new(RND_STRINGS_AMOUNT) { chars.sample(rand(10)).join } + @rand_strs[:lt100] = Array.new(RND_STRINGS_AMOUNT) { chars.sample( rand(100) ).join } + @rand_strs[:lt1000] = Array.new(RND_STRINGS_AMOUNT) { chars.sample( rand(1000) ).join } + @rand_strs[:eq10] = Array.new(RND_STRINGS_AMOUNT) { chars.sample(10).join } + @rand_strs[:eq100] = Array.new(RND_STRINGS_AMOUNT) { chars.sample(100).join } end self.generate_rand_strs @@ -38,7 +36,7 @@ def self.method_missing(symbol) # 2 + 1 = 3 object -def slow_plus(foo, bar) +def fastest_plus(foo, bar) foo + bar end @@ -57,48 +55,102 @@ def fast_interpolation(foo, bar) "#{foo}#{bar}" end +# bench_100_to_100 +# Rehearsal ----------------------------------------------------------- +# String#+ 1.263725 0.027868 1.291593 ( 1.292498) +# "#{foo}#{bar}" 1.139442 0.022956 1.162398 ( 1.163574) +# String#concat 2.017746 0.014836 2.032582 ( 2.034682) +# String#append 1.320778 0.000000 1.320778 ( 1.321896) +# Collateral actions only 0.713309 0.000000 0.713309 ( 0.714402) +# -------------------------------------------------- total: 6.520660sec +# +# user system total real nomalized ratio +# Collateral actions only 0.703668 0.000000 0.703668 ( 0.705658) +# String#+ 1.014123 0.000000 1.014123 ( 1.015003) 0.30934 +# "#{foo}#{bar}" 1.101751 0.000585 1.102336 ( 1.103558) 0.3979 x 1.3 slower +# String#concat 1.382647 0.000000 1.382647 ( 1.385333) 0.679675 x 2.2 slower +# String#append 1.319974 0.000000 1.319974 ( 1.324772) 0.619114 x 2 slower + def bench_100_to_100 - Benchmark.ips(2) do |x| - sarr1, sarr2 = [], [] - (x.time * 5_000_000).times { sarr1 << RandStr.eq100.dup } - (x.time * 5_000_000).times { sarr2 << RandStr.eq100.dup } - - i, j, k = 0, 0, 0 - x.report("String#+") { k+=1; sarr1[k]; slow_plus(RandStr.eq100, RandStr.lt100) } - x.report('"#{\'foo\'}#{\'bar\'}"') { k+=1; sarr2[k]; fast_interpolation(RandStr.eq100, RandStr.lt100) } - x.report("String#concat") { i+=1; RandStr.eq100; slow_concat(sarr1[i], RandStr.lt100) } - x.report("String#append") { j+=1; RandStr.eq100; slow_append(sarr2[j], RandStr.lt100) } - x.compare! - end; :done + Benchmark.bmbm do |x| + # 1M for rehearsal + 1M for bm + sarr1 = Array.new(2_000_000) { RandStr.eq100.dup } + sarr2 = Array.new(2_000_000) { RandStr.eq100.dup } + + i, j = 0, 0 + # if we want compare apples with apples, we need to measure and exclude "collateral" operations: + # integer += 1, access to an array of randomized strings 100 symbols length, + # then two methods invocation from RandStr module eq100 / lt100. + # + # and only then we can compare string concat methods properly + x.report("Collateral actions only") { k=0; 1_000_000.times { k+=1; RandStr.eq100; sarr2[k]; RandStr.lt100; } } + + x.report("String#+") { k=0; 1_000_000.times { k+=1; sarr1[k]; fastest_plus(RandStr.eq100, RandStr.lt100) } } + x.report('"#{foo}#{bar}"') { k=0; 1_000_000.times { k+=1; sarr2[k]; fast_interpolation(RandStr.eq100, RandStr.lt100) } } + x.report("String#concat") { 1_000_000.times { RandStr.eq100; slow_concat(sarr1[i], RandStr.lt100); i+=1; } } + x.report("String#append") { 1_000_000.times { RandStr.eq100; slow_append(sarr2[j], RandStr.lt100); j+=1; } } + end end +# bench_100_to_1000 +# Rehearsal ----------------------------------------------------------- +# Collateral actions only 0.674168 0.000016 0.674184 ( 0.675031) +# String#+ 2.148756 0.032954 2.181710 ( 2.187042) +# "#{foo}#{bar}" 1.570816 0.004948 1.575764 ( 1.579080) +# String#concat 2.223220 0.160917 2.384137 ( 2.387601) +# String#append 2.005056 0.202962 2.208018 ( 2.211476) +# -------------------------------------------------- total: 9.023813sec +# +# user system total real nomalized ratio +# Collateral actions only 0.666190 0.000000 0.666190 ( 0.666398) +# String#+ 1.077629 0.036944 1.114573 ( 1.115465) 0.449067 +# "#{foo}#{bar}" 1.230489 0.001029 1.231518 ( 1.232423) 0.566025 x 1.25 slower +# String#concat 1.881313 0.149949 2.031262 ( 2.033965) 1.367567 x 3.05 slower +# String#append 1.913785 0.177921 2.091706 ( 2.094298) 1.4279 x 3.18 slower + def bench_100_to_1000 - Benchmark.ips(2) do |x| - sarr1, sarr2 = [], [] - (x.time * 5_000_000).times { sarr1 << RandStr.eq100.dup } - (x.time * 5_000_000).times { sarr2 << RandStr.eq100.dup } - - i, j, k = 0, 0, 0 - x.report("String#+") { k+=1; sarr1[k]; slow_plus(RandStr.eq100, RandStr.lt1000) } - x.report('"#{\'foo\'}#{\'bar\'}"') { k+=1; sarr2[k]; fast_interpolation(RandStr.eq100, RandStr.lt1000) } - x.report("String#concat") { i+=1; RandStr.eq100; slow_concat(sarr1[i], RandStr.lt1000) } - x.report("String#append") { j+=1; RandStr.eq100; slow_append(sarr2[j], RandStr.lt1000) } - x.compare! - end; :done + Benchmark.bmbm do |x| + sarr1 = Array.new(2_000_000) { RandStr.eq100.dup } + sarr2 = Array.new(2_000_000) { RandStr.eq100.dup } + + i, j = 0, 0 + x.report("Collateral actions only") { k=0; 1_000_000.times { k+=1; RandStr.eq100; sarr2[k]; RandStr.lt1000; } } + + x.report("String#+") { k=0; 1_000_000.times { k+=1; sarr1[k]; fastest_plus(RandStr.eq100, RandStr.lt1000) } } + x.report('"#{foo}#{bar}"') { k=0; 1_000_000.times { k+=1; sarr2[k]; fast_interpolation(RandStr.eq100, RandStr.lt1000) } } + x.report("String#concat") { 1_000_000.times { RandStr.eq100; slow_concat(sarr1[i], RandStr.lt1000); i+=1; } } + x.report("String#append") { 1_000_000.times { RandStr.eq100; slow_append(sarr2[j], RandStr.lt1000); j+=1; } } + end end -def bench_10_to_100 - Benchmark.ips(2) do |x| - sarr1, sarr2 = [], [] - (x.time * 5_000_000).times { sarr1 << RandStr.eq10.dup } - (x.time * 5_000_000).times { sarr2 << RandStr.eq10.dup } - - i, j, k = 0, 0, 0 - x.report("String#+") { k+=1; sarr1[k]; slow_plus(RandStr.eq10, RandStr.lt100) } - x.report('"#{\'foo\'}#{\'bar\'}"') { k+=1; sarr2[k]; fast_interpolation(RandStr.eq10, RandStr.lt100) } - x.report("String#concat") { i+=1; RandStr.eq10; slow_concat(sarr1[i], RandStr.lt100) } - x.report("String#append") { j+=1; RandStr.eq10; slow_append(sarr2[j], RandStr.lt100) } - x.compare! - end; :done +# bench_10_to_100 +# Rehearsal ----------------------------------------------------------- +# Collateral actions only 0.681273 0.000000 0.681273 ( 0.681611) +# String#+ 1.188326 0.000701 1.189027 ( 1.196455) +# "#{foo}#{bar}" 1.182554 0.003851 1.186405 ( 1.191678) +# String#concat 1.707191 0.006764 1.713955 ( 1.720055) +# String#append 1.177368 0.000831 1.178199 ( 1.184116) +# -------------------------------------------------- total: 5.948859sec +# +# user system total real nomalized ratio +# Collateral actions only 0.682486 0.000000 0.682486 ( 0.682818) +# String#+ 0.914002 0.000000 0.914002 ( 0.917294) 0.234476 +# "#{foo}#{bar}" 1.096633 0.000966 1.097599 ( 1.100782) 0.417964 x 1.78 slower +# String#concat 1.373582 0.000910 1.374492 ( 1.375239) 0.692421 x 2.95 slower +# String#append 1.300632 0.000000 1.300632 ( 1.300807) 0.617989 x 2.63 slower +# => + + def bench_10_to_100 + Benchmark.bmbm do |x| + sarr1 = Array.new(2_000_000) { RandStr.eq100.dup } + sarr2 = Array.new(2_000_000) { RandStr.eq100.dup } + + i, j = 0, 0 + x.report("Collateral actions only") { k=0; 1_000_000.times { k+=1; RandStr.eq10; sarr2[k]; RandStr.lt100; } } + x.report("String#+") { k=0; 1_000_000.times { k+=1; sarr1[k]; fastest_plus(RandStr.eq10, RandStr.lt100) } } + x.report('"#{foo}#{bar}"') { k=0; 1_000_000.times { k+=1; sarr2[k]; fast_interpolation(RandStr.eq10, RandStr.lt100) } } + x.report("String#concat") { 1_000_000.times { RandStr.eq10; slow_concat(sarr1[i], RandStr.lt100); i+=1; } } + x.report("String#append") { 1_000_000.times { RandStr.eq10; slow_append(sarr2[j], RandStr.lt100); j+=1; } } + end end From 7b8ee37989951d3c862c72521ba81126a78da930 Mon Sep 17 00:00:00 2001 From: Aleksey Leshchuk Date: Sat, 7 Jan 2023 15:16:28 +0300 Subject: [PATCH 3/9] Update code/string/concatenation_randomized.rb MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Juan Vásquez --- code/string/concatenation_randomized.rb | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/code/string/concatenation_randomized.rb b/code/string/concatenation_randomized.rb index ea9db21..b6dc44f 100644 --- a/code/string/concatenation_randomized.rb +++ b/code/string/concatenation_randomized.rb @@ -141,16 +141,16 @@ def bench_100_to_1000 # => def bench_10_to_100 - Benchmark.bmbm do |x| - sarr1 = Array.new(2_000_000) { RandStr.eq100.dup } - sarr2 = Array.new(2_000_000) { RandStr.eq100.dup } - - i, j = 0, 0 - x.report("Collateral actions only") { k=0; 1_000_000.times { k+=1; RandStr.eq10; sarr2[k]; RandStr.lt100; } } - x.report("String#+") { k=0; 1_000_000.times { k+=1; sarr1[k]; fastest_plus(RandStr.eq10, RandStr.lt100) } } - x.report('"#{foo}#{bar}"') { k=0; 1_000_000.times { k+=1; sarr2[k]; fast_interpolation(RandStr.eq10, RandStr.lt100) } } - x.report("String#concat") { 1_000_000.times { RandStr.eq10; slow_concat(sarr1[i], RandStr.lt100); i+=1; } } - x.report("String#append") { 1_000_000.times { RandStr.eq10; slow_append(sarr2[j], RandStr.lt100); j+=1; } } - end + Benchmark.bmbm do |x| + sarr1 = Array.new(2_000_000) { RandStr.eq100.dup } + sarr2 = Array.new(2_000_000) { RandStr.eq100.dup } + + i, j = 0, 0 + x.report("Collateral actions only") { k=0; 1_000_000.times { k+=1; RandStr.eq10; sarr2[k]; RandStr.lt100; } } + x.report("String#+") { k=0; 1_000_000.times { k+=1; sarr1[k]; fastest_plus(RandStr.eq10, RandStr.lt100) } } + x.report('"#{foo}#{bar}"') { k=0; 1_000_000.times { k+=1; sarr2[k]; fast_interpolation(RandStr.eq10, RandStr.lt100) } } + x.report("String#concat") { 1_000_000.times { RandStr.eq10; slow_concat(sarr1[i], RandStr.lt100); i+=1; } } + x.report("String#append") { 1_000_000.times { RandStr.eq10; slow_append(sarr2[j], RandStr.lt100); j+=1; } } + end end From 78c226790028f7053c1050bb84362f4906f41208 Mon Sep 17 00:00:00 2001 From: Aleksey Leshchuk Date: Sat, 7 Jan 2023 15:16:35 +0300 Subject: [PATCH 4/9] Update code/string/concatenation_randomized.rb MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Juan Vásquez --- code/string/concatenation_randomized.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/code/string/concatenation_randomized.rb b/code/string/concatenation_randomized.rb index b6dc44f..93f97f5 100644 --- a/code/string/concatenation_randomized.rb +++ b/code/string/concatenation_randomized.rb @@ -138,7 +138,6 @@ def bench_100_to_1000 # "#{foo}#{bar}" 1.096633 0.000966 1.097599 ( 1.100782) 0.417964 x 1.78 slower # String#concat 1.373582 0.000910 1.374492 ( 1.375239) 0.692421 x 2.95 slower # String#append 1.300632 0.000000 1.300632 ( 1.300807) 0.617989 x 2.63 slower -# => def bench_10_to_100 Benchmark.bmbm do |x| From c200c20ffbf3bdf08c83b26dd008a99efaa859d4 Mon Sep 17 00:00:00 2001 From: Aleksey Leshchuk Date: Sat, 7 Jan 2023 15:16:44 +0300 Subject: [PATCH 5/9] Update code/string/concatenation_randomized.rb MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Juan Vásquez --- code/string/concatenation_randomized.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/string/concatenation_randomized.rb b/code/string/concatenation_randomized.rb index 93f97f5..104abdd 100644 --- a/code/string/concatenation_randomized.rb +++ b/code/string/concatenation_randomized.rb @@ -13,8 +13,8 @@ module RandStr def self.generate_rand_strs chars = ('A'..'z').to_a * 20 @rand_strs[:lt10] = Array.new(RND_STRINGS_AMOUNT) { chars.sample(rand(10)).join } - @rand_strs[:lt100] = Array.new(RND_STRINGS_AMOUNT) { chars.sample( rand(100) ).join } - @rand_strs[:lt1000] = Array.new(RND_STRINGS_AMOUNT) { chars.sample( rand(1000) ).join } + @rand_strs[:lt100] = Array.new(RND_STRINGS_AMOUNT) { chars.sample(rand(100)).join } + @rand_strs[:lt1000] = Array.new(RND_STRINGS_AMOUNT) { chars.sample(rand(1000)).join } @rand_strs[:eq10] = Array.new(RND_STRINGS_AMOUNT) { chars.sample(10).join } @rand_strs[:eq100] = Array.new(RND_STRINGS_AMOUNT) { chars.sample(100).join } end From 2f8c8b1a6b64a0a8f7de3806c25e77db611085bc Mon Sep 17 00:00:00 2001 From: Aleksey Leshchuk Date: Sat, 7 Jan 2023 15:16:49 +0300 Subject: [PATCH 6/9] Update code/string/concatenation_randomized.rb MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Juan Vásquez --- code/string/concatenation_randomized.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/string/concatenation_randomized.rb b/code/string/concatenation_randomized.rb index 104abdd..567af78 100644 --- a/code/string/concatenation_randomized.rb +++ b/code/string/concatenation_randomized.rb @@ -21,7 +21,7 @@ def self.generate_rand_strs self.generate_rand_strs - def self.rand_str( named_range ) + def self.rand_str(named_range) @rand_strs[named_range][rand(RND_STRINGS_AMOUNT)] end From c0332060c75d4587d4290e705f565a0f1efc83a3 Mon Sep 17 00:00:00 2001 From: Aleksey Leshchuk Date: Sat, 7 Jan 2023 15:16:56 +0300 Subject: [PATCH 7/9] Update code/string/concatenation_randomized.rb MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Juan Vásquez --- code/string/concatenation_randomized.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/string/concatenation_randomized.rb b/code/string/concatenation_randomized.rb index 567af78..8ee24c1 100644 --- a/code/string/concatenation_randomized.rb +++ b/code/string/concatenation_randomized.rb @@ -28,8 +28,8 @@ def self.rand_str(named_range) def self.method_missing(symbol) return super unless @rand_strs.keys.include?(symbol) - define_singleton_method(symbol) { rand_str( symbol ) } - return rand_str( symbol ) + define_singleton_method(symbol) { rand_str(symbol) } + return rand_str(symbol) end end From ed192b64be4e1b304082bbe3f9d43c96bfc687f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20V=C3=A1squez?= Date: Thu, 12 Jan 2023 10:03:44 -0600 Subject: [PATCH 8/9] Update code/string/concatenation_randomized.rb --- code/string/concatenation_randomized.rb | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/code/string/concatenation_randomized.rb b/code/string/concatenation_randomized.rb index 8ee24c1..cf6e64c 100644 --- a/code/string/concatenation_randomized.rb +++ b/code/string/concatenation_randomized.rb @@ -139,17 +139,17 @@ def bench_100_to_1000 # String#concat 1.373582 0.000910 1.374492 ( 1.375239) 0.692421 x 2.95 slower # String#append 1.300632 0.000000 1.300632 ( 1.300807) 0.617989 x 2.63 slower - def bench_10_to_100 - Benchmark.bmbm do |x| - sarr1 = Array.new(2_000_000) { RandStr.eq100.dup } - sarr2 = Array.new(2_000_000) { RandStr.eq100.dup } +def bench_10_to_100 + Benchmark.bmbm do |x| + sarr1 = Array.new(2_000_000) { RandStr.eq100.dup } + sarr2 = Array.new(2_000_000) { RandStr.eq100.dup } - i, j = 0, 0 - x.report("Collateral actions only") { k=0; 1_000_000.times { k+=1; RandStr.eq10; sarr2[k]; RandStr.lt100; } } - x.report("String#+") { k=0; 1_000_000.times { k+=1; sarr1[k]; fastest_plus(RandStr.eq10, RandStr.lt100) } } - x.report('"#{foo}#{bar}"') { k=0; 1_000_000.times { k+=1; sarr2[k]; fast_interpolation(RandStr.eq10, RandStr.lt100) } } - x.report("String#concat") { 1_000_000.times { RandStr.eq10; slow_concat(sarr1[i], RandStr.lt100); i+=1; } } - x.report("String#append") { 1_000_000.times { RandStr.eq10; slow_append(sarr2[j], RandStr.lt100); j+=1; } } - end + i, j = 0, 0 + x.report("Collateral actions only") { k=0; 1_000_000.times { k+=1; RandStr.eq10; sarr2[k]; RandStr.lt100; } } + x.report("String#+") { k=0; 1_000_000.times { k+=1; sarr1[k]; fastest_plus(RandStr.eq10, RandStr.lt100) } } + x.report('"#{foo}#{bar}"') { k=0; 1_000_000.times { k+=1; sarr2[k]; fast_interpolation(RandStr.eq10, RandStr.lt100) } } + x.report("String#concat") { 1_000_000.times { RandStr.eq10; slow_concat(sarr1[i], RandStr.lt100); i+=1; } } + x.report("String#append") { 1_000_000.times { RandStr.eq10; slow_append(sarr2[j], RandStr.lt100); j+=1; } } + end end From 50f30f6c74ec444ce17907e571e8a7ec398812ab Mon Sep 17 00:00:00 2001 From: Juan Vasquez Date: Wed, 8 Mar 2023 17:18:51 -0600 Subject: [PATCH 9/9] Use benckmark/ips --- code/string/concatenation_randomized.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/code/string/concatenation_randomized.rb b/code/string/concatenation_randomized.rb index cf6e64c..8a5e7a9 100644 --- a/code/string/concatenation_randomized.rb +++ b/code/string/concatenation_randomized.rb @@ -1,4 +1,4 @@ -require 'benchmark' +require 'benchmark/ips' module RandStr RND_STRINGS_AMOUNT = 1000 @@ -72,7 +72,7 @@ def fast_interpolation(foo, bar) # String#append 1.319974 0.000000 1.319974 ( 1.324772) 0.619114 x 2 slower def bench_100_to_100 - Benchmark.bmbm do |x| + Benchmark.ips do |x| # 1M for rehearsal + 1M for bm sarr1 = Array.new(2_000_000) { RandStr.eq100.dup } sarr2 = Array.new(2_000_000) { RandStr.eq100.dup } @@ -109,7 +109,7 @@ def bench_100_to_100 # String#append 1.913785 0.177921 2.091706 ( 2.094298) 1.4279 x 3.18 slower def bench_100_to_1000 - Benchmark.bmbm do |x| + Benchmark.ips do |x| sarr1 = Array.new(2_000_000) { RandStr.eq100.dup } sarr2 = Array.new(2_000_000) { RandStr.eq100.dup } @@ -140,10 +140,10 @@ def bench_100_to_1000 # String#append 1.300632 0.000000 1.300632 ( 1.300807) 0.617989 x 2.63 slower def bench_10_to_100 - Benchmark.bmbm do |x| + Benchmark.ips do |x| sarr1 = Array.new(2_000_000) { RandStr.eq100.dup } sarr2 = Array.new(2_000_000) { RandStr.eq100.dup } - + i, j = 0, 0 x.report("Collateral actions only") { k=0; 1_000_000.times { k+=1; RandStr.eq10; sarr2[k]; RandStr.lt100; } } x.report("String#+") { k=0; 1_000_000.times { k+=1; sarr1[k]; fastest_plus(RandStr.eq10, RandStr.lt100) } } @@ -152,4 +152,3 @@ def bench_10_to_100 x.report("String#append") { 1_000_000.times { RandStr.eq10; slow_append(sarr2[j], RandStr.lt100); j+=1; } } end end -