14
14
lang = ARGV . fetch ( 0 ) do
15
15
puts <<-HELP
16
16
Usage:
17
- ruby bench.rb (c|ruby|dump ) (null|text|tokens|count|statistic|yaml|html) [size in kB] [stream]
17
+ ruby bench.rb (c|ruby) (null|text|tokens|count|statistic|yaml|html) [size in kB] [stream]
18
18
19
19
SIZE defaults to 100 kB (= 100,000 bytes).
20
20
SIZE = 0 means the whole input.
21
- SIZE is ignored when dump is input.
22
21
23
22
-p generates a profile (slow! use with SIZE = 1)
24
23
-o shows the output
48
47
end
49
48
end
50
49
51
- $dump_input = lang == 'dump'
52
- $dump_output = format == 'dump'
53
- require 'coderay/helpers/gzip_simple.rb' if $dump_input
54
-
55
50
def here fn = nil
56
51
return MYDIR unless fn
57
52
File . join here , fn
@@ -66,59 +61,38 @@ def here fn = nil
66
61
67
62
data = nil
68
63
File . open ( here ( "#$filename." + lang ) , 'rb' ) { |f | data = f . read }
69
- if $dump_input
70
- @size = CodeRay ::Tokens . load ( data ) . text . size
71
- else
72
- raise 'Example file is empty.' if data . empty?
73
- unless @size . zero?
74
- data += data until data . size >= @size
75
- data = data [ 0 , @size ]
76
- end
77
- @size = data . size
64
+ raise 'Example file is empty.' if data . empty?
65
+ unless @size . zero?
66
+ data += data until data . size >= @size
67
+ data = data [ 0 , @size ]
78
68
end
79
-
69
+ @size = data . size
70
+
80
71
options = {
81
72
:tab_width => 2 ,
82
73
# :line_numbers => :inline,
83
74
:css => $style ? :style : :class ,
84
75
}
85
- $hl = CodeRay . encoder ( format , options ) unless $dump_output
76
+ $hl = CodeRay . encoder ( format , options )
86
77
time = bm . report ( 'CodeRay' ) do
87
78
if $stream || true
88
- if $dump_input
89
- raise 'Can\'t stream dump.'
90
- elsif $dump_output
91
- raise 'Can\'t dump stream.'
92
- end
93
79
$o = $hl. encode ( data , lang , options )
94
80
else
95
- if $dump_input
96
- tokens = CodeRay ::Tokens . load data
97
- else
98
- tokens = CodeRay . scan ( data , lang )
99
- end
81
+ tokens = CodeRay . scan ( data , lang )
100
82
tokens . optimize! if $optimize
101
- if $dump_output
102
- $o = tokens . optimize . dump
103
- else
104
- $o = tokens . encode ( $hl)
105
- end
83
+ $o = tokens . encode ( $hl)
106
84
end
107
85
end
108
- $file_created = here ( 'test.' +
109
- ( $dump_output ? 'dump' : $hl. file_extension ) )
86
+ $file_created = here ( 'test.' + $hl. file_extension )
110
87
File . open ( $file_created, 'wb' ) do |f |
111
88
# f.write $o
112
89
end
113
- Dir . chdir ( here ) do
114
- FileUtils . copy 'test.dump' , 'example.dump' if $dump_output
115
- end
116
-
90
+
117
91
time_real = time . real
118
-
92
+
119
93
puts "\t %7.2f KB/s (%d.%d KB)" % [ ( ( @size / 1000.0 ) / time_real ) , @size / 1000 , @size % 1000 ]
120
94
puts $o if ARGV . include? '-o'
121
-
95
+
122
96
end
123
97
end
124
98
puts "Files created: #$file_created"
0 commit comments