Project

General

Profile

Feature #15899 ยป test.rb

edd314159 (Edd Morgan), 07/09/2019 06:33 PM

 
1
require "benchmark"
2

    
3
ITERATIONS = 1_000_000
4
STRING = "the quick brown fox jumps over the lazy dog"
5
SEPERATOR = "fox" # or /fox/
6

    
7
Benchmark.bm(16) do |benchmark|
8
  benchmark.report("String#before") do
9
    ITERATIONS.times do
10
      head = STRING.before(SEPERATOR)
11
    end
12
  end
13

    
14
  benchmark.report("String#partition") do
15
    ITERATIONS.times do
16
      head, _seperator, _tail = STRING.partition(SEPERATOR)
17
    end
18
  end
19
end
20

    
21
Benchmark.bm(16) do |benchmark|
22
  benchmark.report("String#after") do
23
    ITERATIONS.times do
24
      tail = STRING.after(SEPERATOR)
25
    end
26
  end
27

    
28
  benchmark.report("String#partition") do
29
    ITERATIONS.times do
30
      _head, _seperator, tail = STRING.partition(SEPERATOR)
31
    end
32
  end
33
end