I randomly came across this benchmark of some ruby code ruby Digest::* benchmark · GitHub
and I got curious to see what that would look like in crystal.
Here’s my attempt at the same benchmark:
require "digest"
require "benchmark"
SRC = File.read("/dev/urandom")[0, 4096]
Benchmark.bm do |bm|
bm.report("MD5") { 100000.times { Digest::MD5.hexdigest(SRC) } }
bm.report("SHA1") { 100000.times { Digest::SHA1.hexdigest(SRC) } }
bm.report("SHA256") { 100000.times { Digest::SHA256.hexdigest(SRC) } }
end
but when I run this, I get an Arithmetic overflow error.
Unhandled exception: Arithmetic overflow (OverflowError)
from Math@Math::pw2ceil<Int32>:Int32
from /usr/local/Cellar/crystal/0.36.1_2/src/string/builder.cr:124:5 in 'write'
from /usr/local/Cellar/crystal/0.36.1_2/src/io.cr:1120:7 in '__crystal_main'
from /usr/local/Cellar/crystal/0.36.1_2/src/crystal/main.cr:110:5 in 'main'
I didn’t want to just post a bug in case I was doing something weird in my code example.
❯ crystal -v
Crystal 0.36.1 (2021-02-02)
LLVM: 11.0.1
Default target: x86_64-apple-macosx