Skip to content

Instantly share code, notes, and snippets.

@voxxit
Last active September 15, 2015 17:32
Show Gist options
  • Select an option

  • Save voxxit/f57dd3dffdcc748c0cb6 to your computer and use it in GitHub Desktop.

Select an option

Save voxxit/f57dd3dffdcc748c0cb6 to your computer and use it in GitHub Desktop.
Ruby is 966.06x stupider at maths on my MacBook Pro... http://c7.se/go-and-ruby-ffi
package main
import "C"
//export add
func add(a, b int) int {
return a + b
}
func main() {}
% ruby test.rb
Calculating -------------------------------------
ruby-addition 101.788k i/100ms
go-addition 25.521k i/100ms
-------------------------------------------------
ruby-addition 7.253M (± 4.2%) i/s - 36.237M
go-addition 7.007B (±19.5%) i/s - 13.287B
Comparison:
go-addition: 7006956592.1 i/s
ruby-addition: 7253157.8 i/s - 966.06x slower
Calculating -------------------------------------
ruby-addition 123.752k i/100ms
go-addition 26.315k i/100ms
-------------------------------------------------
ruby-addition 8.967M (± 4.9%) i/s - 44.798M
go-addition 7.650B (±18.4%) i/s - 23.153B
Comparison:
go-addition: 7650203181.1 i/s
ruby-addition: 8966529.8 i/s - 853.20x slower
require 'benchmark/ips'
require 'ffi'
module Sum
extend FFI::Library
ffi_lib './libsum.so'
attach_function :add, [:int, :int], :int
end
Benchmark.ips do |x|
# Configure the number of seconds used during
# the warmup phase (default 2) and calculation phase (default 5)
x.config(:time => 5, :warmup => 2)
# These parameters can also be configured this way
x.time = 5
x.warmup = 2
# Typical mode, runs the block as many times as it can
x.report("ruby-addition") { 1 + 2 }
# To reduce overhead, the number of iterations is passed in
# and the block must run the code the specific number of times.
# Used for when the workload is very small and any overhead
# introduces incorrectable errors.
x.report("go-addition") do |times|
Sum.add(1,2)
end
# Compare the iterations per second of the various reports!
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment