-
Notifications
You must be signed in to change notification settings - Fork 2
/
keys-each-vs-each_key.cr
53 lines (49 loc) · 1.36 KB
/
keys-each-vs-each_key.cr
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
require "benchmark"
puts Crystal::DESCRIPTION
HASH = {
"provider" => "facebook",
"uid" => "1234567",
"info" => {
"nickname" => "jbloggs",
"email" => "[email protected]",
"name" => "Joe Bloggs",
"first_name" => "Joe",
"last_name" => "Bloggs",
"image" => "http://graph.facebook.com/1234567/picture?type=square",
"urls" => {"Facebook" => "http://www.facebook.com/jbloggs"},
"location" => "Palo Alto, California",
"verified" => true,
},
"credentials" => {
"token" => "ABCDEF...",
"expires_at" => 1321747205,
"expires" => true,
},
"extra" => {
"raw_info" => {
"id" => "1234567",
"name" => "Joe Bloggs",
"first_name" => "Joe",
"last_name" => "Bloggs",
"link" => "http://www.facebook.com/jbloggs",
"username" => "jbloggs",
"location" => {"id" => "123456789", "name" => "Palo Alto, California"},
"gender" => "male",
"email" => "[email protected]",
"timezone" => -8,
"locale" => "en_US",
"verified" => true,
"updated_time" => "2011-11-11T06:21:03+0000",
},
},
}
def slow
HASH.keys.each(&.chars)
end
def fast
HASH.each_key(&.chars)
end
Benchmark.ips do |x|
x.report("Hash#keys.each") { slow }
x.report("Hash#each_key") { fast }
end