diff options
author | Wayne Meissner <wmeissner@gmail.com> | 2012-04-21 07:50:07 +1000 |
---|---|---|
committer | Wayne Meissner <wmeissner@gmail.com> | 2012-04-21 07:50:07 +1000 |
commit | dbd864219877a49038aa7945af0fcbb73d54e4ee (patch) | |
tree | f844b3132abf7c388153b01b280db3c589f3236b | |
parent | 7139defeae16b9032922e5505e1fe3f464c3e92b (diff) | |
download | ffi-dbd864219877a49038aa7945af0fcbb73d54e4ee.tar.gz |
Convert some benchmarks to use a loop instead of a block - block dispatch overhead is starting to show up in some benchmarks on JRuby now.
-rw-r--r-- | bench/bench_FrV.rb | 24 | ||||
-rw-r--r-- | bench/bench_IIIIIIrV.rb | 19 | ||||
-rw-r--r-- | bench/bench_IIIrV.rb | 15 | ||||
-rw-r--r-- | bench/bench_IrV.rb | 31 | ||||
-rw-r--r-- | bench/bench_LLLrV.rb | 14 | ||||
-rw-r--r-- | bench/bench_PPPrV.rb | 34 | ||||
-rw-r--r-- | bench/bench_PrV.rb | 60 | ||||
-rw-r--r-- | bench/bench_VrI.rb | 64 | ||||
-rw-r--r-- | bench/bench_VrV.rb | 84 | ||||
-rw-r--r-- | bench/bench_buffer_alloc.rb | 35 | ||||
-rw-r--r-- | bench/bench_getpid.rb | 15 | ||||
-rw-r--r-- | bench/bench_memptr_alloc.rb | 32 | ||||
-rw-r--r-- | bench/bench_struct.rb | 25 | ||||
-rw-r--r-- | bench/bench_struct_field.rb | 33 |
14 files changed, 377 insertions, 108 deletions
diff --git a/bench/bench_FrV.rb b/bench/bench_FrV.rb index 59a443d..d9bdc2a 100644 --- a/bench/bench_FrV.rb +++ b/bench/bench_FrV.rb @@ -12,14 +12,26 @@ puts "Benchmark [ :float ], :void performance, #{ITER}x calls" f = 1.0 10.times { puts Benchmark.measure { - ITER.times { LibTest.bench_f32_v(f) } + i = 0; while i < ITER + LibTest.bench_f32_v(f) + LibTest.bench_f32_v(f) + LibTest.bench_f32_v(f) + LibTest.bench_f32_v(f) + i += 4 + end } } puts "Benchmark ruby method(1 arg) performance, #{ITER}x calls" 10.times { puts Benchmark.measure { - ITER.times { LibTest.rb_bench(f) } + i = 0; while i < ITER + LibTest.rb_bench(f) + LibTest.rb_bench(f) + LibTest.rb_bench(f) + LibTest.rb_bench(f) + i += 4 + end } } @@ -38,7 +50,13 @@ unless RUBY_PLATFORM == "java" && JRUBY_VERSION < "1.3.0" puts "Benchmark DL void bench(float) performance, #{ITER}x calls" 10.times { puts Benchmark.measure { - ITER.times { LibTest.bench_f32_v(0) } + i = 0; while i < ITER + LibTest.bench_f32_v(0) + LibTest.bench_f32_v(0) + LibTest.bench_f32_v(0) + LibTest.bench_f32_v(0) + i += 4 + end } } end diff --git a/bench/bench_IIIIIIrV.rb b/bench/bench_IIIIIIrV.rb index 929ba9f..84cb16a 100644 --- a/bench/bench_IIIIIIrV.rb +++ b/bench/bench_IIIIIIrV.rb @@ -3,22 +3,28 @@ METHOD = 'bench_s32s32s32s32s32s32_v' module LibTest extend FFI::Library ffi_lib LIBTEST_PATH - attach_function METHOD, [ :int, :int, :int, :int, :int, :int ], :void + attach_function :ffi_bench, METHOD, [ :int, :int, :int, :int, :int, :int ], :void, :save_errno => false def self.rb_bench(i0, i1, i2, i3, i4, i5); nil; end end puts "Benchmark [ :int, :int, :int, :int, :int, :int ], :void performance, #{ITER}x calls" - 10.times { puts Benchmark.measure { - ITER.times { LibTest.bench_s32s32s32s32s32s32_v(1, 2, 3, 4, 5, 6) } + i = 0; while i < ITER + LibTest.ffi_bench(1, 2, 3, 4, 5, 6) + i += 1 + end } } + puts "Benchmark ruby method(6 arg) performance, #{ITER}x calls" 10.times { puts Benchmark.measure { - ITER.times { LibTest.rb_bench(0, 1, 2, 3, 4, 5) } + i = 0; while i < ITER + LibTest.rb_bench(0, 1, 2, 3, 4, 5) + i += 1 + end } } @@ -37,7 +43,10 @@ unless RUBY_PLATFORM == "java" && JRUBY_VERSION < "1.3.0" puts "Benchmark DL void bench(int, int, int) performance, #{ITER}x calls" 10.times { puts Benchmark.measure { - ITER.times { LibTest.bench_s32s32s32s32s32s32_v(0, 1, 2, 3, 4, 5) } + i = 0; while i < ITER + LibTest.bench_s32s32s32s32s32s32_v(0, 1, 2, 3, 4, 5) + i += 1 + end } } end diff --git a/bench/bench_IIIrV.rb b/bench/bench_IIIrV.rb index b42c2d2..257a1cd 100644 --- a/bench/bench_IIIrV.rb +++ b/bench/bench_IIIrV.rb @@ -25,21 +25,30 @@ end puts "Benchmark [ :int, :int, :int ], :void performance, #{ITER}x calls" 10.times { puts Benchmark.measure { - ITER.times { LibTest.ffi_bench(0, 1, 2) } + i = 0; while i < ITER + LibTest.ffi_bench(0, 1, 2) + i += 1 + end } } puts "Benchmark ruby method(3 arg) performance, #{ITER}x calls" 10.times { puts Benchmark.measure { - ITER.times { LibTest.rb_bench(0, 1, 2) } + i = 0; while i < ITER + LibTest.rb_bench(0, 1, 2) + i += 1 + end } } unless RUBY_PLATFORM == "java" && JRUBY_VERSION < "1.3.0" puts "Benchmark DL void bench(int, int, int) performance, #{ITER}x calls" 10.times { puts Benchmark.measure { - ITER.times { LibTest.bench_s32s32s32_v(0, 1, 2) } + i = 0; while i < ITER + LibTest.bench_s32s32s32_v(0, 1, 2) + i += 1 + end } } end diff --git a/bench/bench_IrV.rb b/bench/bench_IrV.rb index 150da40..c44ae30 100644 --- a/bench/bench_IrV.rb +++ b/bench/bench_IrV.rb @@ -23,24 +23,39 @@ end puts "Benchmark [ :int ], :void performance, #{ITER}x calls" 10.times { puts Benchmark.measure { - ITER.times { LibTest.ffi_bench(0) } + i = 0; while i < ITER + LibTest.ffi_bench(0) + LibTest.ffi_bench(0) + LibTest.ffi_bench(0) + LibTest.ffi_bench(0) + i += 4 + end } } - -unless RUBY_PLATFORM == "java" && JRUBY_VERSION < "1.3.0" -puts "Benchmark DL void bench(int) performance, #{ITER}x calls" +puts "Benchmark ruby method(1 arg) performance, #{ITER}x calls" 10.times { puts Benchmark.measure { - ITER.times { LibTest.bench_s32_v(0) } + i = 0; while i < ITER + LibTest.rb_bench(0) + LibTest.rb_bench(0) + LibTest.rb_bench(0) + LibTest.rb_bench(0) + i += 4 + end } } -end -puts "Benchmark ruby method(1 arg) performance, #{ITER}x calls" +unless RUBY_PLATFORM == "java" && JRUBY_VERSION < "1.3.0" +puts "Benchmark DL void bench(int) performance, #{ITER}x calls" 10.times { puts Benchmark.measure { - ITER.times { LibTest.rb_bench(0) } + i = 0; while i < ITER + LibTest.bench_s32_v(0) + i += 1 + end } } +end + diff --git a/bench/bench_LLLrV.rb b/bench/bench_LLLrV.rb index be37d2a..ec658e5 100644 --- a/bench/bench_LLLrV.rb +++ b/bench/bench_LLLrV.rb @@ -11,16 +11,10 @@ puts "Benchmark [ :long_long, :long_long, :long_long ], :void performance, #{ITE 10.times { puts Benchmark.measure { - ITER.times { LibTest.bench_s64s64s64_v(0, 1, 2) } + i = 0; while i < ITER + LibTest.bench_s64s64s64_v(0, 1, 2) + i += 1 + end } } -unless RUBY_PLATFORM =~ /java/ -puts "Benchmark Invoker.call [ :long_long, :long_long, :long_long ], :void performance, #{ITER}x calls" -invoker = FFI.create_invoker(LIBTEST_PATH, 'bench_s64s64s64_v', [ :long_long, :long_long, :long_long ], :void) -10.times { - puts Benchmark.measure { - ITER.times { invoker.call3(0, 1, 2) } - } -} -end diff --git a/bench/bench_PPPrV.rb b/bench/bench_PPPrV.rb index c77e8ca..06f4bdc 100644 --- a/bench/bench_PPPrV.rb +++ b/bench/bench_PPPrV.rb @@ -4,7 +4,7 @@ module LibTest extend FFI::Library ffi_lib LIBTEST_PATH - attach_function :bench, :bench_PPP_v, [ :pointer, :pointer, :pointer ], :void + attach_function :bench, :bench_PPP_v, [ :pointer, :pointer, :pointer ], :void, :save_errno => false end @@ -12,7 +12,13 @@ puts "Benchmark [ :pointer, :pointer, :pointer ], :void performance, #{ITER}x ca ptr = FFI::MemoryPointer.new :int 10.times { puts Benchmark.measure { - ITER.times { LibTest.bench(ptr, ptr, ptr) } + i = 0; while i < ITER + LibTest.bench(ptr, ptr, ptr) + LibTest.bench(ptr, ptr, ptr) + LibTest.bench(ptr, ptr, ptr) + LibTest.bench(ptr, ptr, ptr) + i += 4 + end } } puts "Benchmark [ :pointer, :pointer, :pointer ], :void with Struct parameters performance #{ITER}x calls" @@ -24,7 +30,13 @@ end s = TestStruct.new(FFI::MemoryPointer.new(TestStruct)); 10.times { puts Benchmark.measure { - ITER.times { LibTest.bench(s, s, s) } + i = 0; while i < ITER + LibTest.bench(s, s, s) + LibTest.bench(s, s, s) + LibTest.bench(s, s, s) + LibTest.bench(s, s, s) + i += 4 + end } } @@ -32,14 +44,26 @@ puts "Benchmark [ :pointer, :pointer, :pointer ], :void with Buffer parameters p ptr = FFI::Buffer.new(:int) 10.times { puts Benchmark.measure { - ITER.times { LibTest.bench(ptr, ptr, ptr) } + i = 0; while i < ITER + LibTest.bench(ptr, ptr, ptr) + LibTest.bench(ptr, ptr, ptr) + LibTest.bench(ptr, ptr, ptr) + LibTest.bench(ptr, ptr, ptr) + i += 4 + end } } puts "Benchmark [ :pointer, :pointer, :pointer ], :void with nil parameters performance, #{ITER}x calls" 10.times { puts Benchmark.measure { - ITER.times { LibTest.bench(nil, nil, nil) } + i = 0; while i < ITER + LibTest.bench(nil, nil, nil) + LibTest.bench(nil, nil, nil) + LibTest.bench(nil, nil, nil) + LibTest.bench(nil, nil, nil) + i += 4 + end } } diff --git a/bench/bench_PrV.rb b/bench/bench_PrV.rb index 3b58786..c049fd2 100644 --- a/bench/bench_PrV.rb +++ b/bench/bench_PrV.rb @@ -12,47 +12,89 @@ puts "Benchmark [ :buffer_in ], :void performance (pre allocated pointer), #{ITE ptr = FFI::MemoryPointer.new :int 10.times { puts Benchmark.measure { - ITER.times { LibTest.bench(ptr) } + i = 0; while i < ITER + LibTest.bench(ptr) + LibTest.bench(ptr) + LibTest.bench(ptr) + LibTest.bench(ptr) + i += 4 + end } } puts "Benchmark [ :buffer_in ], :void performance (nil param), #{ITER}x calls" 10.times { puts Benchmark.measure { - ITER.times { LibTest.bench(nil) } + i = 0; while i < ITER + LibTest.bench(nil) + LibTest.bench(nil) + LibTest.bench(nil) + LibTest.bench(nil) + i += 4 + end } } -puts "Benchmark [ :buffer_in ], :void performance (Buffer param), #{ITER}x calls" +puts "Benchmark [ :buffer_in ], :void performance (pre allocated Buffer param), #{ITER}x calls" ptr = FFI::Buffer.new :int 10.times { puts Benchmark.measure { - ITER.times { LibTest.bench(ptr) } + i = 0; while i < ITER + LibTest.bench(ptr) + LibTest.bench(ptr) + LibTest.bench(ptr) + LibTest.bench(ptr) + i += 4 + end } } puts "Benchmark [ :buffer_in ], :void performance (const String param), #{ITER}x calls" -str = 'test' +ptr = 'test' 10.times { puts Benchmark.measure { - ITER.times { LibTest.bench(str) } + i = 0; while i < ITER + LibTest.bench(ptr) + LibTest.bench(ptr) + LibTest.bench(ptr) + LibTest.bench(ptr) + i += 4 + end } } puts "Benchmark [ :buffer_in ], :void performance (loop-allocated Buffer param), #{ITER}x calls" 10.times { puts Benchmark.measure { - ITER.times { LibTest.bench(FFI::Buffer.new(4)) } + i = 0; while i < ITER + LibTest.bench(FFI::Buffer.new(4)) + LibTest.bench(FFI::Buffer.new(4)) + LibTest.bench(FFI::Buffer.new(4)) + LibTest.bench(FFI::Buffer.new(4)) + i += 4 + end } } puts "Benchmark [ :buffer_in ], :void performance (loop-allocated String param), #{ITER}x calls" 10.times { puts Benchmark.measure { - ITER.times { LibTest.bench(String.new) } + i = 0; while i < ITER + LibTest.bench(String.new) + LibTest.bench(String.new) + LibTest.bench(String.new) + LibTest.bench(String.new) + i += 4 + end } } puts "Benchmark [ :buffer_in ], :void performance (loop-allocated MemoryPointer param), #{ITER}x calls" 10.times { puts Benchmark.measure { - ITER.times { LibTest.bench(FFI::MemoryPointer.new(4)) } + i = 0; while i < ITER + LibTest.bench(FFI::MemoryPointer.new(4)) + LibTest.bench(FFI::MemoryPointer.new(4)) + LibTest.bench(FFI::MemoryPointer.new(4)) + LibTest.bench(FFI::MemoryPointer.new(4)) + i += 4 + end } } diff --git a/bench/bench_VrI.rb b/bench/bench_VrI.rb index 1f3ec25..59cf3ff 100644 --- a/bench/bench_VrI.rb +++ b/bench/bench_VrI.rb @@ -4,8 +4,9 @@ module LibTest extend FFI::Library ffi_lib LIBTEST_PATH attach_function :ffi_bench, :returnInt, [ ], :int, :save_errno => false - def self.rb_bench; nil; end + def self.rb_bench; 0xdeadbeef; end end + unless RUBY_PLATFORM == "java" && JRUBY_VERSION < "1.3.0" require 'dl' require 'dl/import' @@ -23,62 +24,43 @@ end puts "Benchmark [ ], :int performance, #{ITER}x calls" 10.times { puts Benchmark.measure { - i = 0 - while i < ITER + i = 0; max = ITER / 4 + while i < max + LibTest.ffi_bench + LibTest.ffi_bench + LibTest.ffi_bench LibTest.ffi_bench i += 1 end } } - -unless RUBY_PLATFORM == "java" && JRUBY_VERSION < "1.3.0" -puts "Benchmark DL void bench() performance, #{ITER}x calls" -10.times { - puts Benchmark.measure { - i = 0 - while i < ITER - LibTest.returnInt - i += 1 - end - } -} -end -puts "Benchmark Invoker.call [ ], :int performance, #{ITER}x calls" - -invoker = FFI.create_invoker(LIBTEST_PATH, 'returnInt', [ ], :int) -10.times { - puts Benchmark.measure { - i = 0 - while i < ITER - invoker.call - i += 1 - end - } -} - -f = FFI::Function.new(:int, [ ], invoker, { :ignore_errno => true, :convention => :default }) -puts "Benchmark [ ], :int no-errno performance, #{ITER}x calls" -module NoErrno ;end -f.attach(NoErrno, "ffi_bench") +puts "Benchmark [ ], :int performance, #{ITER}x calls" 10.times { puts Benchmark.measure { - i = 0 - while i < ITER - NoErrno.ffi_bench + i = 0; max = ITER / 4 + while i < max + LibTest.rb_bench + LibTest.rb_bench + LibTest.rb_bench + LibTest.rb_bench i += 1 end } } -puts "Benchmark ruby method(no arg) performance, #{ITER}x calls" +unless RUBY_PLATFORM == "java" && JRUBY_VERSION < "1.3.0" +puts "Benchmark DL void bench() performance, #{ITER}x calls" 10.times { puts Benchmark.measure { - i = 0 - while i < ITER - LibTest.rb_bench + i = 0; max = ITER / 4 + while i < max + LibTest.returnInt + LibTest.returnInt + LibTest.returnInt + LibTest.returnInt i += 1 end } } - +end diff --git a/bench/bench_VrV.rb b/bench/bench_VrV.rb new file mode 100644 index 0000000..765ff50 --- /dev/null +++ b/bench/bench_VrV.rb @@ -0,0 +1,84 @@ +require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper")) + +module LibTest + extend FFI::Library + ffi_lib LIBTEST_PATH + attach_function :ffi_bench, :returnVoid, [ ], :void, :save_errno => false + def self.rb_bench; nil; end +end +unless RUBY_PLATFORM == "java" && JRUBY_VERSION < "1.3.0" + require 'dl' + require 'dl/import' + module LibTest + if RUBY_VERSION >= "1.9.0" + extend DL::Importer + else + extend DL::Importable + end + dlload LIBTEST_PATH + extern "void returnVoid()" + end +end + +puts "Benchmark [ ], :void performance, #{ITER}x calls" +10.times { + puts Benchmark.measure { + i = 0 + while i < ITER + LibTest.ffi_bench + i += 1 + end + } +} + + +unless RUBY_PLATFORM == "java" && JRUBY_VERSION < "1.3.0" +puts "Benchmark DL void bench() performance, #{ITER}x calls" +10.times { + puts Benchmark.measure { + i = 0 + while i < ITER + LibTest.returnVoid + i += 1 + end + } +} +end +puts "Benchmark Invoker.call [ ], :void performance, #{ITER}x calls" + +invoker = FFI.create_invoker(LIBTEST_PATH, 'returnVoid', [ ], :int) +10.times { + puts Benchmark.measure { + i = 0 + while i < ITER + invoker.call + i += 1 + end + } +} + +f = FFI::Function.new(:void, [ ], invoker, { :save_errno => false, :convention => :default }) +puts "Benchmark [ ], :void no-errno performance, #{ITER}x calls" +module NoErrno ;end +f.attach(NoErrno, "ffi_bench") +10.times { + puts Benchmark.measure { + i = 0 + while i < ITER + NoErrno.ffi_bench + i += 1 + end + } +} + +puts "Benchmark ruby method(no arg) performance, #{ITER}x calls" +10.times { + puts Benchmark.measure { + i = 0 + while i < ITER + LibTest.rb_bench + i += 1 + end + } +} + diff --git a/bench/bench_buffer_alloc.rb b/bench/bench_buffer_alloc.rb index 551209a..9db4138 100644 --- a/bench/bench_buffer_alloc.rb +++ b/bench/bench_buffer_alloc.rb @@ -3,25 +3,54 @@ require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper")) require 'benchmark' require 'ffi' iter = ITER +class Foo < FFI::Buffer + def initialize(a1, a2, a3, a4, a5) + puts "Foo#initialize(#{a1}, #{a2}. #{a3}, #{a4}, #{a5})" + end + +end + +Foo.new(1, 2, 3, 4, 5) + +#FFI::Buffer.fubar(:int, 1, true) puts "Benchmark Buffer.new(:int, 1, true)) performance, #{iter}x" 10.times { puts Benchmark.measure { - iter.times { FFI::Buffer.new(:int, 1, true) } + i = 0; while i < iter + FFI::Buffer.new(:int, 1, true) + i += 1 + end + } +} + +puts "Benchmark Buffer.alloc_out(:int, 1, true)) performance, #{iter}x" +10.times { + puts Benchmark.measure { + i = 0; while i < iter + FFI::Buffer.new_out(:int, 1, true) + i += 1 + end } } puts "Benchmark Buffer.new(4, 1, true)) performance, #{iter}x" 10.times { puts Benchmark.measure { - iter.times { FFI::Buffer.new(4, 1, true) } + i = 0; while i < iter + FFI::Buffer.new(4, 1, true) + i += 1 + end } } puts "Benchmark Buffer.new(256, 1, true)) performance, #{iter}x" 10.times { puts Benchmark.measure { - iter.times { FFI::Buffer.new(256, 1, true) } + i = 0; while i < iter + FFI::Buffer.new(256, 1, true) + i += 1 + end } } diff --git a/bench/bench_getpid.rb b/bench/bench_getpid.rb index ec390da..ac1de66 100644 --- a/bench/bench_getpid.rb +++ b/bench/bench_getpid.rb @@ -4,10 +4,11 @@ iter = ITER module Posix extend FFI::Library + ffi_lib 'c' if FFI::Platform.windows? - attach_function :getpid, :_getpid, [], :uint + attach_function :getpid, :_getpid, [], :uint, :save_errno => false else - attach_function :getpid, [], :uint + attach_function :getpid, [], :uint, :save_errno => false end end @@ -16,12 +17,18 @@ puts "pid=#{Process.pid} Foo.getpid=#{Posix.getpid}" puts "Benchmark FFI getpid performance, #{iter}x calls" 10.times { puts Benchmark.measure { - iter.times { Posix.getpid } + i = 0; while i < iter + Posix.getpid + i += 1 + end } } puts "Benchmark Process.pid performance, #{iter}x calls" 10.times { puts Benchmark.measure { - iter.times { Process.pid } + i = 0; while i < iter + Process.pid + i += 1 + end } } diff --git a/bench/bench_memptr_alloc.rb b/bench/bench_memptr_alloc.rb index da05d64..ab78f28 100644 --- a/bench/bench_memptr_alloc.rb +++ b/bench/bench_memptr_alloc.rb @@ -4,23 +4,49 @@ require 'benchmark' require 'ffi' 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 { - iter.times { FFI::MemoryPointer.new(:int, 1, true) } + 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 { - iter.times { FFI::MemoryPointer.new(4, 1, true) } + 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 { - iter.times { FFI::MemoryPointer.new(size, 1, true) } + i = 0; while i < iter + FFI::MemoryPointer.new(size, 1, true) + i += 1 + end } } end diff --git a/bench/bench_struct.rb b/bench/bench_struct.rb index 8f47e10..4506afe 100644 --- a/bench/bench_struct.rb +++ b/bench/bench_struct.rb @@ -2,7 +2,6 @@ require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper")) require 'benchmark' require 'ffi' -iter = 100_000 module StructBench extend FFI::Library @@ -16,21 +15,33 @@ end class TestStruct < FFI::Struct layout :i, :int, :p, :pointer end -puts "Benchmark FFI call(Struct.alloc_in) performance, #{iter}x" + +puts "Benchmark FFI call(Struct.alloc_in) performance, #{ITER}x" 10.times { puts Benchmark.measure { - iter.times { StructBench.bench_struct_in(TestStruct.alloc_in, 0) } + i = 0; while i < ITER + StructBench.bench_struct_in(TestStruct.alloc_in, 0) + i += 1 + end } } -puts "Benchmark FFI call(Struct.alloc_out) performance, #{iter}x" + +puts "Benchmark FFI call(Struct.alloc_out) performance, #{ITER}x" 10.times { puts Benchmark.measure { - iter.times { StructBench.bench_struct_out(TestStruct.alloc_out, 0) } + i = 0; while i < ITER + StructBench.bench_struct_out(TestStruct.alloc_out, 0) + i += 1 + end } } -puts "Benchmark FFI call(Struct.alloc_inout) performance, #{iter}x" + +puts "Benchmark FFI call(Struct.alloc_inout) performance, #{ITER}x" 10.times { puts Benchmark.measure { - iter.times { StructBench.bench_struct_inout(TestStruct.alloc_inout, 0) } + i = 0; while i < ITER + StructBench.bench_struct_inout(TestStruct.alloc_inout, 0) + i += 1 + end } } diff --git a/bench/bench_struct_field.rb b/bench/bench_struct_field.rb index b74a1bb..0e6faf4 100644 --- a/bench/bench_struct_field.rb +++ b/bench/bench_struct_field.rb @@ -2,7 +2,7 @@ require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper")) require 'benchmark' require 'ffi' -iter = 1000_000 +iter = ITER class TestStruct < FFI::Struct layout :i, :int, :p, :pointer @@ -12,33 +12,49 @@ s = TestStruct.new(FFI::MemoryPointer.new(TestStruct)) puts "Benchmark FFI Struct.get(:int) performance, #{iter}x" 10.times { puts Benchmark.measure { - iter.times { s[:i] } + i = 0; while i < iter + s[:i] + i += 1 + end } } puts "Benchmark FFI Struct.get(:int) using string name performance, #{iter}x" 10.times { puts Benchmark.measure { - iter.times { s[:i] } + i = 0; while i < iter + s['i'] + i += 1 + end } } + puts "Benchmark FFI Struct.put(:int) performance, #{iter}x" 10.times { puts Benchmark.measure { - iter.times { s[:i] = 0x12345678 } + i = 0; while i < iter + s[:i] = 0x12345678 + i += 1 + end } } puts "Benchmark FFI Struct.get(:pointer) performance, #{iter}x" 10.times { puts Benchmark.measure { - iter.times { s[:p] } + i = 0; while i < iter + s[:p] + i += 1 + end } } puts "Benchmark FFI Struct.put(:pointer) performance, #{iter}x" 10.times { p = FFI::MemoryPointer.new :int puts Benchmark.measure { - iter.times { s[:p] = p } + i = 0; while i < iter + s[:p] = p + i += 1 + end } } puts "Benchmark FFI Struct.get(:string) performance, #{iter}x" @@ -51,6 +67,9 @@ end s = StringStruct.new s.pointer.put_pointer(0, mp) puts Benchmark.measure { - iter.times { s[:s] } + i = 0; while i < iter + s[:s] + i += 1 + end } } |