Skip to content

Instantly share code, notes, and snippets.

@goalaleo
Last active May 5, 2019 22:07
Show Gist options
  • Save goalaleo/fd29f6be199815256c1719ec23b28610 to your computer and use it in GitHub Desktop.
Save goalaleo/fd29f6be199815256c1719ec23b28610 to your computer and use it in GitHub Desktop.
Ruby benchmark for String#chars vs String#each_char
Rehearsal --------------------------------------------
option1: 0.711160 0.076554 0.787714 ( 0.796558)
option2: 0.327121 0.003232 0.330353 ( 0.334768)
----------------------------------- total: 1.118067sec
user system total real
option1: 0.718185 0.075168 0.793353 ( 0.802384)
option2: 0.322129 0.001361 0.323490 ( 0.325277)
require "benchmark"
require "memory_profiler"
BASES = %w(A G T C)
STRAND_LENGTH = 100
S1 = STRAND_LENGTH.times.map { BASES.sample }.join
S2 = STRAND_LENGTH.times.map { BASES.sample }.join
def option1
S1.each_char.zip(S2.each_char).count { |a,b| a != b }
end
def option2
S1.chars.zip(S2.chars).count { |a,b| a != b }
end
# == speed bechmark
n = 10_000
Benchmark.bmbm do |x|
x.report("option1:") { n.times { option1 } }
x.report("option2:") { n.times { option2 } }
end
# ==
# == memory benchmark
report1 = MemoryProfiler.report do
option1
end
#
report2 = MemoryProfiler.report do
option2
end
# ==
report1.pretty_print(to_file: "./option1.txt")
report2.pretty_print(to_file: "./option2.txt")
Total allocated: 22136 bytes (407 objects)
Total retained: 120 bytes (3 objects)
allocated memory by gem
-----------------------------------
22136 other
allocated memory by file
-----------------------------------
14136 bm.rb
8000 (no name)
allocated memory by location
-----------------------------------
14136 bm.rb:10
8000 (no name):0
allocated memory by class
-----------------------------------
9104 Array
8000 String
4568 Fiber
384 Enumerator
80 Proc
allocated objects by gem
-----------------------------------
407 other
allocated objects by file
-----------------------------------
207 bm.rb
200 (no name)
allocated objects by location
-----------------------------------
207 bm.rb:10
200 (no name):0
allocated objects by class
-----------------------------------
202 Array
200 String
3 Enumerator
1 Fiber
1 Proc
retained memory by gem
-----------------------------------
120 other
retained memory by file
-----------------------------------
120 (no name)
retained memory by location
-----------------------------------
120 (no name):0
retained memory by class
-----------------------------------
80 String
40 Array
retained objects by gem
-----------------------------------
3 other
retained objects by file
-----------------------------------
3 (no name)
retained objects by location
-----------------------------------
3 (no name):0
retained objects by class
-----------------------------------
2 String
1 Array
Allocated String Report
-----------------------------------
60 "C"
38 bm.rb:10
22 (no name):0
52 "T"
28 bm.rb:10
24 (no name):0
48 "A"
29 (no name):0
19 bm.rb:10
40 "G"
25 (no name):0
15 bm.rb:10
Retained String Report
-----------------------------------
1 "A"
1 (no name):0
1 "G"
1 (no name):0
Total allocated: 13760 bytes (304 objects)
Total retained: 0 bytes (0 objects)
allocated memory by gem
-----------------------------------
13760 other
allocated memory by file
-----------------------------------
13760 bm.rb
allocated memory by location
-----------------------------------
13760 bm.rb:14
allocated memory by class
-----------------------------------
8000 String
5760 Array
allocated objects by gem
-----------------------------------
304 other
allocated objects by file
-----------------------------------
304 bm.rb
allocated objects by location
-----------------------------------
304 bm.rb:14
allocated objects by class
-----------------------------------
200 String
104 Array
retained memory by gem
-----------------------------------
NO DATA
retained memory by file
-----------------------------------
NO DATA
retained memory by location
-----------------------------------
NO DATA
retained memory by class
-----------------------------------
NO DATA
retained objects by gem
-----------------------------------
NO DATA
retained objects by file
-----------------------------------
NO DATA
retained objects by location
-----------------------------------
NO DATA
retained objects by class
-----------------------------------
NO DATA
Allocated String Report
-----------------------------------
60 "C"
60 bm.rb:14
52 "T"
52 bm.rb:14
48 "A"
48 bm.rb:14
40 "G"
40 bm.rb:14
Retained String Report
-----------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment