-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathruby_racer.rb
More file actions
50 lines (46 loc) · 1.11 KB
/
ruby_racer.rb
File metadata and controls
50 lines (46 loc) · 1.11 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
43
44
45
46
47
48
49
50
require "benchmark"
require "stringio"
Shoes.app(title: "Ruby Racer") do
racer1 = nil
racer2 = nil
flow do
stack width: 0.45, margin: 5 do
para "Racer 1", size: :caption
code1 = <<~RUBY
for i in 1..10
a = "1"
end
RUBY
racer1 = edit_box(code1, width: "100%", height: 100)
end
stack width: 0.45, margin: 5 do
para "Racer 2", size: :caption
code2 = <<~RUBY
10.times do
a = "1"
end
RUBY
racer2 = edit_box(code2, width: "100%", height: 100)
end
end
stack margin: 10 do
@push = button "Race!"
@results = para ""
@push.click {
@results.replace "And they're off!"
run = run_benchmark(racer1.text, racer2.text)
@results.replace "<pre>Results:<br/>#{run}</pre>"
}
def run_benchmark(code1, code2)
current_stdout = $stdout
$stdout = StringIO.new
Benchmark.bmbm do |x|
x.report("Code 1") { eval(code1) }
x.report("Code 2") { eval(code2) }
end
$stdout.string.gsub("\n", "<br />")
ensure
$stdout = current_stdout
end
end
end