Skip to content

Instantly share code, notes, and snippets.

@will
Created May 26, 2015 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save will/29908441649005e2c3e1 to your computer and use it in GitHub Desktop.
Save will/29908441649005e2c3e1 to your computer and use it in GitHub Desktop.
require "benchmark"
STR = "8675309".cstr
lib LibC
fun atoi(str : UInt8*) : Int32
end
def crystal
String.new(STR).to_i
end
def libc
LibC.atoi(STR)
end
p crystal == libc
n = 10_000_000
Benchmark.bm do |x|
x.report("crystal") { n.times { crystal }}
x.report("libc") { n.times { libc }}
end
true
user system total real
crystal 1.150000 0.230000 1.380000 ( 1.139501)
libc 0.000000 0.000000 0.000000 ( 0.000001)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment