Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require 'dl'
require 'dl/func'
require 'benchmark'
include DL
libc = dlopen('/usr/lib/libc.dylib')
F = Function.new(DL::CFunc.new(libc['strcpy'], TYPE_VOIDP, 'strcpy'),
[TYPE_VOIDP, TYPE_VOIDP])
CF = CFunc.new(libc['strcpy'], TYPE_VOIDP, 'strcpy')
puts "#{'#' * 10} new #{'#' * 10}"
10_000.step(600_000, 10_000) do |n|
puts "n = #{n}"
Benchmark.bm(7) do |x|
# http://chart.apis.google.com/chart?chdl=After|Before&chs=600x150&cht=lc&chco=ff0000,0000ff&chd=e:A.B9C6D6E5F2G7HwIwJzKzLnMnNoOdPgQZRYSXTYUUVMWNXOYIb1aGebcSgBejiYg2j5k-jSnboOmzqMrqrmr5uqvEv.xoyhzC0X2M2y3V4o7R507V8--l..,ArBWB.CuDYEGEsFaGEGwHgIFIyJcKOKxLbMVMwNpOKO5PlQMQ8RpSPTCTjURU7WSZKXCbNYtbmaCdwbTffczf2f7f1ixgqifk4jolCpEk3mYo2o3o7pKqmre
x.report('new(3)') {
n.times { CFunc.new(libc['strcpy'], TYPE_VOIDP, 'strcpy') }
}
# http://chart.apis.google.com/chart?chdl=After|Before&chs=600x150&cht=lc&chco=ff0000,0000ff&chd=e:BGCEDNELFMGBHAH-JBJ.LMMAM8OBPuP.Q.SCS8UEU9V8XCX.ZGZ9bFcJdBeHfCgKhHiBmAn4k9m-qvoBtsqFuRtivlwbxfyb0Lz14T0B6D2W8A6262.J8U..,AyBoCdDMD-E7FlGaHOH6I2JoKfLRL8MsN0OTPLQGQvRlSmTFUHU2VZWbXFYNYmZjaTbOb1hNdoezjzf.iyk4iToQj-n.m4pmpLqzrGsEs3wlvSwGuh0AxCz5
x.report('new(2)') {
n.times { CFunc.new(libc['strcpy'], TYPE_VOIDP) }
}
# http://chart.apis.google.com/chart?chdl=After|Before&chs=600x150&cht=lc&chco=ff0000,0000ff&chd=e:BBB7C9D3E4F5G9H1I-JyKxLvM9NuOrPpQuRkSvTnUhVsWhXlYZZkaYbfcddcegfNgFhHlNkyj8qPnPr.qTphvBrLxAs8zOvF1GxD3K0n3v5w2s8B706G9s..,AxBnChDQD8EvFeGUHEHzIxJjKKLCLxN.NXOFPGPxQiRZSFS3T9UbVbWBW-XxYgZSaGaubucUgFfoemkGhEhomciknfl5mVqQnQquprsvtuuftXvgwlvgzFwp
x.report('new(1)') {
n.times { CFunc.new(libc['strcpy']) }
}
end
end
#http://chart.apis.google.com/chart?chdl=After%7CBefore&chs=600x150&cht=lc&chco=ff0000,0000ff&chd=e:AxCtEmGjIkKZMUOLQJR9T-V2X5ZxbvdtfghXjOlFnEoxqzsuunwmyb4L,BCDEFMHYJgL5NwP5SJUdWzYvbLdUfShtj7l9oisPs2uww.2f1V6852..
puts "#{'#' * 10} accessor #{'#' * 10}"
100_000.step(7_000_000, 250_000) do |n|
puts "n = #{n}"
Benchmark.bm(7) do |x|
x.report('ptr') { n.times { CF.ptr } }
end
end
# http://chart.apis.google.com/chart?chdl=After%7CBefore&chs=600x150&cht=lc&chco=ff0000,0000ff&chd=e:CAD.GCH.J-L.N-P6R9T0V3X1bNdMfeidjemKogq0tXu1why3242p537q-p..,BvDeFIG5IkKTMCNyPfRMS9VzX4Z.b4dCfJhUjOkQm.pBqfsOvcvYxS0B1W46
puts "#{'#' * 10} call #{'#' * 10}"
10_000.step(300_000, 10_000) do |n|
puts "n = #{n}"
Benchmark.bm(7) do |x|
x.report('call') { n.times { F.call('000', '123') } }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.