Project

General

Profile

Feature #15151 ยป bench_slice_bang.rb

chopraanmol1 (Anmol Chopra), 09/23/2018 03:32 PM

 
1
require 'memory_profiler'
2
require 'benchmark'
3

    
4
def generate_string(times = 1000)
5
  ('0'..'z').to_a.join * times
6
end
7

    
8

    
9
def each_str!(string, len = 20)
10
  while (str = string.slice!(0, len)).size > 0
11
    yield str
12
  end
13
end
14

    
15

    
16
string = generate_string
17
MemoryProfiler.report(top: 0){ each_str!(string){} }.pretty_print(detailed_report: false)
18

    
19

    
20
string = generate_string
21
puts Benchmark.measure{ each_str!(string){} }
22