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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
require 'strscan'
require 'benchmark'
class Scanner < StringScanner
def initialize code
super code
end
def tokenize encoder = Tokens.new
scan_tokens encoder
encoder
end
protected
def scan_tokens encoder
until eos?
if matched = scan(/\s+/)
encoder.text_token matched, :space
elsif matched = scan(/!/)
encoder.text_token matched, :not_going_to_happen
elsif matched = scan(/=/)
encoder.text_token matched, :not_going_to_happen
elsif matched = scan(/%/)
encoder.text_token matched, :not_going_to_happen
elsif matched = scan(/\w+/)
encoder.text_token matched, :word
elsif matched = scan(/[,.]/)
encoder.text_token matched, :op
elsif scan(/\(/)
encoder.open :par
elsif scan(/\)/)
encoder.close :par
else
raise
end
end
end
end
class Tokens < Array
alias token push
alias text_token push
alias block_token push
def open kind; push :open, kind end
def close kind; push :close, kind end
end
class Encoder
def setup
@out = ''
@opened = []
end
def finish
while kind = @opened.pop
close kind
end
@out
end
def encode_tokens tokens
setup
compile tokens
finish
end
def encode_stream scanner
setup
scanner.tokenize self
finish
end
def token content, kind
if content.is_a? ::String
text_token content, kind
elsif content.is_a? ::Symbol
block_token content, kind
else
raise 'Unknown token content type: %p' % [content]
end
end
def text_token text, kind
@out <<
if kind == :space
text
else
text.gsub!(/[)\\]/, '\\\\\0') # escape ) and \
"#{kind}(#{text})"
end
end
def block_token action, kind
case action
when :open
open kind
when :close
close kind
else
raise
end
end
def open kind
@opened << kind
@out << "#{kind}<"
end
def close kind
@opened.pop
@out << '>'
end
protected
def compile tokens
content = nil
for item in tokens
if content
case content
when ::String
text_token content, item
content = nil
when :open
open item
content = nil
when :close
close item
content = nil
when ::Symbol
block_token content, kind
content = nil
else
raise
end
else
content = item
end
end
raise if content
end
end
N = (10 ** (ARGV.first || 5).to_i)
code = " alpha, beta, (gamma).\n" * N
scanner = Scanner.new code
encoder = Encoder.new
tokens = nil
time_scanning = Benchmark.realtime do
tokens = scanner.tokenize
end
puts 'Scanning: %0.2fs -- %0.0f kTok/s' % [time_scanning, tokens.size / 2 / time_scanning / 1000]
time_encoding = Benchmark.realtime do
encoder.encode_tokens tokens
end
puts 'Encoding: %0.2fs -- %0.0f kTok/s' % [time_encoding, tokens.size / 2 / time_encoding / 1000]
time = time_scanning + time_encoding
puts 'Together: %0.2fs -- %0.0f kTok/s' % [time, tokens.size / 2 / time / 1000]
scanner.reset
time = Benchmark.realtime do
encoder.encode_stream scanner
end
puts 'Scanning + Encoding: %0.2fs -- %0.0f kTok/s' % [time, (N * 11 + 1) / time / 1000]
|