-
-
Save kostya/c1748e5dde4aefa4cb14 to your computer and use it in GitHub Desktop.
strange (crystal lang https://github.com/manastech/crystal)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def foo(n) | |
String.new_with_capacity(n * 4 + 10) do |buf| | |
bufc = 0 | |
bar(n) do |byte| | |
buf[bufc] = byte | |
bufc += 1 | |
end | |
bufc | |
end | |
end | |
def bar(n) | |
n.times do | |
yield 33_u8 | |
yield 34_u8 | |
yield 35_u8 | |
yield 36_u8 | |
end | |
case rand(3) | |
when 1 | |
yield 37_u8 | |
yield 38_u8 | |
when 2 | |
yield 39_u8 | |
yield 33_u8 | |
yield 33_u8 | |
end | |
end | |
t = Time.now | |
s = 0_u64 | |
1000.times{ |i| res = foo(100000); s += res[i] } | |
p s | |
p Time.now - t |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def foo(n) | |
String.new_with_capacity(n * 4 + 10) do |buf| | |
bufc = 0 | |
bar(n) do |byte| | |
buf[bufc] = byte | |
bufc += 1 | |
end | |
bufc | |
end | |
end | |
def bar(n) | |
n.times do | |
yield 33_u8 | |
yield 34_u8 | |
yield 35_u8 | |
yield 36_u8 | |
end | |
case rand(3) | |
when 1 | |
yield 37_u8 | |
yield 38_u8 | |
when 2 | |
yield 39_u8 | |
yield 33_u8 | |
#yield 33_u8 | |
end | |
end | |
t = Time.now | |
s = 0_u64 | |
1000.times{ |i| res = foo(100000); s += res[i] } | |
p s | |
p Time.now - t |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
./bin/crystal 1.cr --release && ./1
34500
1.4392
./bin/crystal 2.cr --release && ./2
34500
0.309147