1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
require_relative 'bench_helper'
module BenchMemptrAlloc
iter = ITER
module LibC
extend FFI::Library
ffi_lib 'c'
attach_function :calloc, [ :size_t, :size_t ], :pointer, :save_errno => false
attach_function :free, [ :pointer ], :void, :save_errno => false
end
puts "Benchmark calloc(1, 4) performance, #{iter}x"
10.times {
puts Benchmark.measure {
i = 0; while i < iter
ptr = LibC.calloc(1, 4)
LibC.free(ptr)
i += 1
end
}
}
puts "Benchmark MemoryPointer.new(:int, 1, true)) performance, #{iter}x"
10.times {
puts Benchmark.measure {
i = 0; while i < iter
FFI::MemoryPointer.new(:int)
i += 1
end
}
}
puts "Benchmark MemoryPointer.new(4, 1, true)) performance, #{iter}x"
10.times {
puts Benchmark.measure {
i = 0; while i < iter
FFI::MemoryPointer.new(4, 1, true)
i += 1
end
}
}
[ 8, 16, 32, 64, 128, 256 ].each do |size|
puts "Benchmark MemoryPointer.new(#{size}, 1, true)) performance, #{iter}x"
10.times {
puts Benchmark.measure {
i = 0; while i < iter
FFI::MemoryPointer.new(size, 1, true)
i += 1
end
}
}
end
if defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby"
require 'java'
puts "calling java gc"
10.times {
java.lang.System.gc
sleep 1
}
end
end
|