File: bench.rb

package info (click to toggle)
ruby-rsec 0.4.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 272 kB
  • sloc: ruby: 2,130; lisp: 13; makefile: 3
file content (59 lines) | stat: -rw-r--r-- 1,529 bytes parent folder | download | duplicates (3)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Compare performance between rsec and treetop
# NOTE simple parser doesn't require much backtracking, so treetop's caching is sure to fail.
#      Next step is to compare really complex parsers.

# string to be parsed
s = '(3+24/5)/10-3*4+((82321+12-3)-3*4+(82321+12-3))/5'

# rsec
$:.unshift "#{File.dirname(__FILE__)}/../lib"
require "#{File.dirname(__FILE__)}/../examples/arithmetic"

# treetop
require "treetop"
require "#{File.dirname(__FILE__)}/little.rb"

require "benchmark"

def measure &proc
  puts proc[]
  200.times &proc
  puts((Benchmark.measure{
    1000.times &proc
  }), '')
end

# ------------------------------------------------------------------------------

puts ''
puts Benchmark::CAPTION
puts ''

print 'rsec result:', "\t"
p = arithmetic()
measure{ p.parse! s }

print 'treetop result:', "\t"
t = ArithmeticParser.new
measure{ t.parse(s).value }

puts 'treetop without calculation'
t = ArithmeticParser.new
measure{ t.parse s }

PARSEC_ARITH_SO = "#{File.dirname(__FILE__)}/parsec/Arithmetic.so"
if File.exist?(PARSEC_ARITH_SO)
  require 'dl/import'
  require 'dl/types'
  module Arithmetic
    extend DL::Importer
    dlload PARSEC_ARITH_SO
    extern "long calculate(char *)", :stdcall
    extern "long donothing(char *)", :stdcall
  end
  print 'Haskell Parsec result:', "\t"
  measure{ Arithmetic.calculate s }
else
  puts 'Haskell Parsec benchmark requires ghc installation. cd bench/parsec and run make.sh(unix) or make.bat(windows)'
end