forked from dblock/ruby-enum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcase.rb
More file actions
45 lines (37 loc) · 1023 Bytes
/
case.rb
File metadata and controls
45 lines (37 loc) · 1023 Bytes
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
# frozen_string_literal: true
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'benchmark'
require 'ruby-enum'
##
# Test enum
class Color
include Ruby::Enum
include Ruby::Enum::Case
define :RED, :red
define :GREEN, :green
define :BLUE, :blue
end
puts 'Running 1.000.000 normal case statements'
case_statement_time = Benchmark.realtime do
1_000_000.times do
case Color::RED
when Color::RED, Color::GREEN
'red or green'
when Color::BLUE
'blue'
end
end
end
puts 'Running 1.000.000 ruby-enum case statements'
ruby_enum_time = Benchmark.realtime do
1_000_000.times do
Color.case(Color::RED,
{
[Color::RED, Color::GREEN] => -> { 'red or green' },
Color::BLUE => -> { 'blue' }
})
end
end
puts "ruby-enum case: #{ruby_enum_time.round(4)}"
puts "case statement: #{case_statement_time.round(4)}"
puts "ruby-enum case is #{(ruby_enum_time / case_statement_time).round(2)} times slower"