tmm1 (owner)

Revisions

  • 3171b9 tmm1 Mon Jun 01 20:44:51 -0700 2009
  • 614ea3 tmm1 Fri May 29 23:33:37 -0700 2009
gist: 120414 Download_button fork
public
Description:
string benchmarks in MRI
Public Clone URL: git://gist.github.com/120414.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# GC.disable
require 'benchmark'
 
Constant = 'abc'.freeze
 
Benchmark.bm(30) do |b|
  b.report('baseline') do
    var = 'abc'
    5_000_000.times{ var == var }
  end
 
  b.report('constant lookup') do
    5_000_000.times{ Constant == Constant }
  end
 
  b.report('one inline string') do
    var = 'abc'
    5_000_000.times{ 'abc' == var }
  end
 
  b.report('inline string + constant lookup') do
    5_000_000.times{ 'abc' == Constant }
  end
 
  b.report('two inline strings') do
    5_000_000.times{ 'abc' == 'abc' }
  end
end
 
__END__
 
user system total real
baseline 2.350000 0.010000 2.360000 ( 2.365716)
constant lookup 2.760000 0.000000 2.760000 ( 2.775171)
one inline string 3.270000 0.010000 3.280000 ( 3.288727)
inline string + constant lookup 3.540000 0.020000 3.560000 ( 3.592176)
two inline strings 4.040000 0.020000 4.060000 ( 4.093472)