public
Fork of mislav/bluecloth
Description: Markdown processor in Ruby; foked from official SVN repo to fix bugs
Homepage: http://www.deveiate.org/projects/BlueCloth
Clone URL: git://github.com/github/bluecloth.git
ged (author)
Sat Apr 10 13:35:02 -0700 2004
commit  d2aae8010c641996fda0e0c397f55d9164b127ed
tree    933c7be122029fe1f8caf5351d9c2ab8f4635a68
bluecloth / experiments / benchstrscan.rb
100644 42 lines (34 sloc) 0.834 kb
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
39
40
41
42
#!/usr/bin/ruby
#
# See if it's with the extra hassle to reuse StringScanner objects instead of
# creating a new one every time.
#
# Time-stamp: <29-Mar-2004 08:10:25 ged>
#
 
BEGIN {
  base = File::dirname( File::dirname(File::expand_path(__FILE__)) )
  $LOAD_PATH.unshift "#{base}/lib"
 
  require "#{base}/utils.rb"
  include UtilityFunctions
}
 
require 'benchmark'
require 'strscan'
 
str = "some strange otaku kidnapped my iBook"
iterations = 100_000
 
puts "Benchmarking StringScanner object creation strategies:\n" +
  " (#{iterations} iterations)"
Benchmark.bm(20) do |x|
  x.report("new one every time:") {
    iterations.times do
      s = StringScanner::new( str )
      s.scan_until( /B/ )
    end
  }
 
  x.report("reused:") {
    s = StringScanner::new( str )
    iterations.times do
      s.string = str
      s.scan_until( /B/ )
    end
  }
end