From 18cb2cf0c307c1141d4a358c4741b15ce31bfc1f Mon Sep 17 00:00:00 2001 From: Lars Kanis Date: Wed, 8 Jan 2020 13:28:34 +0100 Subject: [PATCH] Namespace all benchmarks since they are loaded into one process This is a follow-up to the previous commit. The benchmarks also made use of the redefinition feature of struct layouts. Namespacing avoids redefinition, but uses separate classes for all structs. --- bench/bench_FFFrV.rb | 36 +++---- bench/bench_FrV.rb | 60 ++++++------ bench/bench_IIIIIIrV.rb | 47 ++++----- bench/bench_IIIrI.rb | 22 +++-- bench/bench_IIIrV.rb | 46 ++++----- bench/bench_IrV.rb | 58 +++++------ bench/bench_LLLrV.rb | 29 +++--- bench/bench_PPPrV.rb | 182 +++++++++++++++++----------------- bench/bench_PPrV.rb | 98 +++++++++---------- bench/bench_PrV.rb | 174 ++++++++++++++++----------------- bench/bench_SrV.rb | 24 ++--- bench/bench_VrI.rb | 62 ++++++------ bench/bench_VrV.rb | 71 +++++++------- bench/bench_autoptr.rb | 56 ++++++----- bench/bench_buffer.rb | 184 ++++++++++++++++++----------------- bench/bench_buffer_alloc.rb | 68 ++++++------- bench/bench_buffer_fill.rb | 62 ++++++------ bench/bench_chmod.rb | 48 ++++----- bench/bench_closure_IIIrV.rb | 100 +++++++++---------- bench/bench_closure_IrV.rb | 60 ++++++------ bench/bench_closure_VrV.rb | 82 ++++++++-------- bench/bench_enum.rb | 68 ++++++------- bench/bench_enum_i.rb | 28 +++--- bench/bench_getlogin.rb | 40 ++++---- bench/bench_getpid.rb | 54 +++++----- bench/bench_gettimeofday.rb | 82 ++++++++-------- bench/bench_getuid.rb | 36 +++---- bench/bench_math.rb | 50 +++++----- bench/bench_memptr_alloc.rb | 100 +++++++++---------- bench/bench_memptr_fill.rb | 62 ++++++------ bench/bench_strlen.rb | 40 ++++---- bench/bench_struct.rb | 100 +++++++++---------- bench/bench_struct_field.rb | 132 ++++++++++++------------- bench/bench_struct_size.rb | 43 ++++---- bench/bench_time.rb | 66 +++++++------ bench/bench_umask.rb | 94 +++++++++--------- 36 files changed, 1314 insertions(+), 1250 deletions(-) diff --git a/bench/bench_FFFrV.rb b/bench/bench_FFFrV.rb index 89fe79a67..81207f7e6 100644 --- a/bench/bench_FFFrV.rb +++ b/bench/bench_FFFrV.rb @@ -1,24 +1,26 @@ require_relative 'bench_helper' -module LibTest - extend FFI::Library - ffi_lib LIBTEST_PATH - attach_function :bench_f32f32f32_v, [ :float, :float, :float ], :void - def self.rb_bench(a0, a1, a2); nil; end -end +module BenchFFFrV + module LibTest + extend FFI::Library + ffi_lib LIBTEST_PATH + attach_function :bench_f32f32f32_v, [ :float, :float, :float ], :void + def self.rb_bench(a0, a1, a2); nil; end + end -puts "Benchmark [ :float, :float, :float ], :void performance, #{ITER}x calls" -f = 1.0 -10.times { - puts Benchmark.measure { - ITER.times { LibTest.bench_f32f32f32_v(f, f, f) } + puts "Benchmark [ :float, :float, :float ], :void performance, #{ITER}x calls" + f = 1.0 + 10.times { + puts Benchmark.measure { + ITER.times { LibTest.bench_f32f32f32_v(f, f, f) } + } } -} -puts "Benchmark ruby method(1 arg) performance, #{ITER}x calls" -10.times { - puts Benchmark.measure { - ITER.times { LibTest.rb_bench(f, f, f) } + puts "Benchmark ruby method(1 arg) performance, #{ITER}x calls" + 10.times { + puts Benchmark.measure { + ITER.times { LibTest.rb_bench(f, f, f) } + } } -} +end diff --git a/bench/bench_FrV.rb b/bench/bench_FrV.rb index d1aa3d7cf..cc2e7917f 100644 --- a/bench/bench_FrV.rb +++ b/bench/bench_FrV.rb @@ -1,36 +1,38 @@ require_relative 'bench_helper' -module LibTest - extend FFI::Library - ffi_lib LIBTEST_PATH - attach_function :bench_f32_v, [ :float ], :void - def self.rb_bench(i0); nil; end -end +module BenchFrV + module LibTest + extend FFI::Library + ffi_lib LIBTEST_PATH + attach_function :bench_f32_v, [ :float ], :void + def self.rb_bench(i0); nil; end + end -puts "Benchmark [ :float ], :void performance, #{ITER}x calls" -f = 1.0 -10.times { - puts Benchmark.measure { - 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 [ :float ], :void performance, #{ITER}x calls" + f = 1.0 + 10.times { + puts Benchmark.measure { + 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 { - i = 0; while i < ITER - LibTest.rb_bench(f) - LibTest.rb_bench(f) - LibTest.rb_bench(f) - LibTest.rb_bench(f) - i += 4 - end + puts "Benchmark ruby method(1 arg) performance, #{ITER}x calls" + 10.times { + puts Benchmark.measure { + i = 0; while i < ITER + LibTest.rb_bench(f) + LibTest.rb_bench(f) + LibTest.rb_bench(f) + LibTest.rb_bench(f) + i += 4 + end + } } -} +end diff --git a/bench/bench_IIIIIIrV.rb b/bench/bench_IIIIIIrV.rb index 3849204af..671be9dd9 100644 --- a/bench/bench_IIIIIIrV.rb +++ b/bench/bench_IIIIIIrV.rb @@ -1,29 +1,30 @@ require_relative 'bench_helper' -module LibTest - extend FFI::Library - ffi_lib LIBTEST_PATH - attach_function :ffi_bench, 'bench_s32s32s32s32s32s32_v', [ :int, :int, :int, :int, :int, :int ], :void, :save_errno => false - def self.rb_bench(i0, i1, i2, i3, i4, i5); nil; end -end +module BenchIIIIIIrV + module LibTest + extend FFI::Library + ffi_lib LIBTEST_PATH + attach_function :ffi_bench, 'bench_s32s32s32s32s32s32_v', [ :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 { - i = 0; while i < ITER - LibTest.ffi_bench(1, 2, 3, 4, 5, 6) - i += 1 - end + puts "Benchmark [ :int, :int, :int, :int, :int, :int ], :void performance, #{ITER}x calls" + 10.times { + puts Benchmark.measure { + 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 { - i = 0; while i < ITER - LibTest.rb_bench(0, 1, 2, 3, 4, 5) - i += 1 - end + puts "Benchmark ruby method(6 arg) performance, #{ITER}x calls" + 10.times { + puts Benchmark.measure { + i = 0; while i < ITER + LibTest.rb_bench(0, 1, 2, 3, 4, 5) + i += 1 + end + } } -} - +end diff --git a/bench/bench_IIIrI.rb b/bench/bench_IIIrI.rb index cf8e4dedf..0bb30efc4 100644 --- a/bench/bench_IIIrI.rb +++ b/bench/bench_IIIrI.rb @@ -1,15 +1,17 @@ require_relative 'bench_helper' -module LibTest - extend FFI::Library - ffi_lib LIBTEST_PATH - attach_function :bench, :bench_s32s32s32_v, [ :int, :int, :int ], :int -end +module BenchIIIrI + module LibTest + extend FFI::Library + ffi_lib LIBTEST_PATH + attach_function :bench, :bench_s32s32s32_v, [ :int, :int, :int ], :int + end -puts "Benchmark [ :int, :int, :int ], :int performance, #{ITER}x calls" -10.times { - puts Benchmark.measure { - ITER.times { LibTest.bench(0, 1, 2) } + puts "Benchmark [ :int, :int, :int ], :int performance, #{ITER}x calls" + 10.times { + puts Benchmark.measure { + ITER.times { LibTest.bench(0, 1, 2) } + } } -} +end diff --git a/bench/bench_IIIrV.rb b/bench/bench_IIIrV.rb index 1dfa5a09d..f1a8bef76 100644 --- a/bench/bench_IIIrV.rb +++ b/bench/bench_IIIrV.rb @@ -1,28 +1,30 @@ require_relative 'bench_helper' -module LibTest - extend FFI::Library - ffi_lib LIBTEST_PATH - attach_function :ffi_bench, :bench_s32s32s32_v, [ :int, :int, :int ], :void, :save_errno => false - def self.rb_bench(i0, i1, i2); nil; end -end +module BenchIIIrV + module LibTest + extend FFI::Library + ffi_lib LIBTEST_PATH + attach_function :ffi_bench, :bench_s32s32s32_v, [ :int, :int, :int ], :void, :save_errno => false + def self.rb_bench(i0, i1, i2); nil; end + end -puts "Benchmark [ :int, :int, :int ], :void performance, #{ITER}x calls" -10.times { - puts Benchmark.measure { - i = 0; while i < ITER - LibTest.ffi_bench(0, 1, 2) - i += 1 - end + puts "Benchmark [ :int, :int, :int ], :void performance, #{ITER}x calls" + 10.times { + puts Benchmark.measure { + 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 { - i = 0; while i < ITER - LibTest.rb_bench(0, 1, 2) - i += 1 - end + puts "Benchmark ruby method(3 arg) performance, #{ITER}x calls" + 10.times { + puts Benchmark.measure { + i = 0; while i < ITER + LibTest.rb_bench(0, 1, 2) + i += 1 + end + } } -} +end diff --git a/bench/bench_IrV.rb b/bench/bench_IrV.rb index 0a9447da4..1bd2a27c2 100644 --- a/bench/bench_IrV.rb +++ b/bench/bench_IrV.rb @@ -1,34 +1,36 @@ require_relative 'bench_helper' -module LibTest - extend FFI::Library - ffi_lib LIBTEST_PATH - attach_function :ffi_bench, :bench_s32_v, [ :int ], :void, :save_errno => false - def self.rb_bench(i0); nil; end -end +module BenchIrV + module LibTest + extend FFI::Library + ffi_lib LIBTEST_PATH + attach_function :ffi_bench, :bench_s32_v, [ :int ], :void, :save_errno => false + def self.rb_bench(i0); nil; end + end -puts "Benchmark [ :int ], :void performance, #{ITER}x calls" -10.times { - puts Benchmark.measure { - i = 0; while i < ITER - LibTest.ffi_bench(0) - LibTest.ffi_bench(0) - LibTest.ffi_bench(0) - LibTest.ffi_bench(0) - i += 4 - end + puts "Benchmark [ :int ], :void performance, #{ITER}x calls" + 10.times { + puts Benchmark.measure { + i = 0; while i < ITER + LibTest.ffi_bench(0) + LibTest.ffi_bench(0) + LibTest.ffi_bench(0) + LibTest.ffi_bench(0) + i += 4 + end + } } -} -puts "Benchmark ruby method(1 arg) performance, #{ITER}x calls" -10.times { - puts Benchmark.measure { - i = 0; while i < ITER - LibTest.rb_bench(0) - LibTest.rb_bench(0) - LibTest.rb_bench(0) - LibTest.rb_bench(0) - i += 4 - end + puts "Benchmark ruby method(1 arg) performance, #{ITER}x calls" + 10.times { + puts Benchmark.measure { + 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 diff --git a/bench/bench_LLLrV.rb b/bench/bench_LLLrV.rb index 067fe06bf..b633483cd 100644 --- a/bench/bench_LLLrV.rb +++ b/bench/bench_LLLrV.rb @@ -1,20 +1,21 @@ require_relative 'bench_helper' -module LibTest - extend FFI::Library - ffi_lib LIBTEST_PATH - attach_function :bench_s64s64s64_v, [ :long_long, :long_long, :long_long ], :void, :save_errno => false -end +module BenchLLLrV + module LibTest + extend FFI::Library + ffi_lib LIBTEST_PATH + attach_function :bench_s64s64s64_v, [ :long_long, :long_long, :long_long ], :void, :save_errno => false + end -puts "Benchmark [ :long_long, :long_long, :long_long ], :void performance, #{ITER}x calls" + puts "Benchmark [ :long_long, :long_long, :long_long ], :void performance, #{ITER}x calls" -10.times { - puts Benchmark.measure { - i = 0; while i < ITER - LibTest.bench_s64s64s64_v(0, 1, 2) - i += 1 - end + 10.times { + puts Benchmark.measure { + i = 0; while i < ITER + LibTest.bench_s64s64s64_v(0, 1, 2) + i += 1 + end + } } -} - +end diff --git a/bench/bench_PPPrV.rb b/bench/bench_PPPrV.rb index 6764ea1fe..edd7ff47f 100644 --- a/bench/bench_PPPrV.rb +++ b/bench/bench_PPPrV.rb @@ -1,112 +1,114 @@ require_relative 'bench_helper' -module LibTest - extend FFI::Library - ffi_lib LIBTEST_PATH +module BenchPPPrV + module LibTest + extend FFI::Library + ffi_lib LIBTEST_PATH - attach_function :bench_ptr, :bench_PPP_v, [ :pointer, :pointer, :pointer ], :void, :save_errno => false - attach_function :bench_buffer, :bench_PPP_v, [ :pointer, :pointer, :pointer ], :void, :save_errno => false - attach_function :bench_struct, :bench_PPP_v, [ :pointer, :pointer, :pointer ], :void, :save_errno => false - attach_function :bench_nil, :bench_PPP_v, [ :pointer, :pointer, :pointer ], :void, :save_errno => false - attach_function :bench_conv, :bench_PPP_v, [ :pointer, :pointer, :pointer ], :void, :save_errno => false -end + attach_function :bench_ptr, :bench_PPP_v, [ :pointer, :pointer, :pointer ], :void, :save_errno => false + attach_function :bench_buffer, :bench_PPP_v, [ :pointer, :pointer, :pointer ], :void, :save_errno => false + attach_function :bench_struct, :bench_PPP_v, [ :pointer, :pointer, :pointer ], :void, :save_errno => false + attach_function :bench_nil, :bench_PPP_v, [ :pointer, :pointer, :pointer ], :void, :save_errno => false + attach_function :bench_conv, :bench_PPP_v, [ :pointer, :pointer, :pointer ], :void, :save_errno => false + end -puts "Benchmark [ :pointer, :pointer, :pointer ], :void performance, #{ITER}x calls" -ptr = FFI::MemoryPointer.new :int -10.times { - puts Benchmark.measure { - i = 0; while i < ITER - LibTest.bench_ptr(ptr, ptr, ptr) - LibTest.bench_ptr(ptr, ptr, ptr) - LibTest.bench_ptr(ptr, ptr, ptr) - LibTest.bench_ptr(ptr, ptr, ptr) - i += 4 - end + puts "Benchmark [ :pointer, :pointer, :pointer ], :void performance, #{ITER}x calls" + ptr = FFI::MemoryPointer.new :int + 10.times { + puts Benchmark.measure { + i = 0; while i < ITER + LibTest.bench_ptr(ptr, ptr, ptr) + LibTest.bench_ptr(ptr, ptr, ptr) + LibTest.bench_ptr(ptr, ptr, ptr) + LibTest.bench_ptr(ptr, ptr, ptr) + i += 4 + end + } } -} -puts "Benchmark [ :pointer, :pointer, :pointer ], :void with Struct parameters performance #{ITER}x calls" + puts "Benchmark [ :pointer, :pointer, :pointer ], :void with Struct parameters performance #{ITER}x calls" -class TestStruct < FFI::Struct - layout :i, :int -end + class TestStruct < FFI::Struct + layout :i, :int + end -s = TestStruct.new(FFI::MemoryPointer.new(TestStruct)); -10.times { - puts Benchmark.measure { - i = 0; while i < ITER - LibTest.bench_struct(s, s, s) - LibTest.bench_struct(s, s, s) - LibTest.bench_struct(s, s, s) - LibTest.bench_struct(s, s, s) - i += 4 - end + s = TestStruct.new(FFI::MemoryPointer.new(TestStruct)); + 10.times { + puts Benchmark.measure { + i = 0; while i < ITER + LibTest.bench_struct(s, s, s) + LibTest.bench_struct(s, s, s) + LibTest.bench_struct(s, s, s) + LibTest.bench_struct(s, s, s) + i += 4 + end + } } -} -puts "Benchmark [ :pointer, :pointer, :pointer ], :void with Buffer parameters performance, #{ITER}x calls" -ptr = FFI::Buffer.new(:int) -10.times { - puts Benchmark.measure { - i = 0; while i < ITER - LibTest.bench_buffer(ptr, ptr, ptr) - LibTest.bench_buffer(ptr, ptr, ptr) - LibTest.bench_buffer(ptr, ptr, ptr) - LibTest.bench_buffer(ptr, ptr, ptr) - i += 4 - end + puts "Benchmark [ :pointer, :pointer, :pointer ], :void with Buffer parameters performance, #{ITER}x calls" + ptr = FFI::Buffer.new(:int) + 10.times { + puts Benchmark.measure { + i = 0; while i < ITER + LibTest.bench_buffer(ptr, ptr, ptr) + LibTest.bench_buffer(ptr, ptr, ptr) + LibTest.bench_buffer(ptr, ptr, ptr) + LibTest.bench_buffer(ptr, ptr, ptr) + i += 4 + end + } } -} -puts "Benchmark [ :pointer, :pointer, :pointer ], :void with nil parameters performance, #{ITER}x calls" -10.times { - puts Benchmark.measure { - i = 0; while i < ITER - LibTest.bench_nil(nil, nil, nil) - LibTest.bench_nil(nil, nil, nil) - LibTest.bench_nil(nil, nil, nil) - LibTest.bench_nil(nil, nil, nil) - i += 4 - end + puts "Benchmark [ :pointer, :pointer, :pointer ], :void with nil parameters performance, #{ITER}x calls" + 10.times { + puts Benchmark.measure { + i = 0; while i < ITER + LibTest.bench_nil(nil, nil, nil) + LibTest.bench_nil(nil, nil, nil) + LibTest.bench_nil(nil, nil, nil) + LibTest.bench_nil(nil, nil, nil) + i += 4 + end + } } -} -puts "Benchmark [ :pointer, :pointer, :pointer ], :void with string parameters performance, #{ITER}x calls" -ptr = 0.chr * 4 -10.times { - puts Benchmark.measure { - i = 0; while i < ITER - LibTest.bench_ptr(ptr, ptr, ptr) - LibTest.bench_ptr(ptr, ptr, ptr) - LibTest.bench_ptr(ptr, ptr, ptr) - LibTest.bench_ptr(ptr, ptr, ptr) - i += 4 - end + puts "Benchmark [ :pointer, :pointer, :pointer ], :void with string parameters performance, #{ITER}x calls" + ptr = 0.chr * 4 + 10.times { + puts Benchmark.measure { + i = 0; while i < ITER + LibTest.bench_ptr(ptr, ptr, ptr) + LibTest.bench_ptr(ptr, ptr, ptr) + LibTest.bench_ptr(ptr, ptr, ptr) + LibTest.bench_ptr(ptr, ptr, ptr) + i += 4 + end + } } -} -class PointerType - def initialize(ptr) - @pointer = ptr - end + class PointerType + def initialize(ptr) + @pointer = ptr + end - def to_ptr - @pointer + def to_ptr + @pointer + end end -end -puts "Benchmark [ :pointer, :pointer, :pointer ], :void with to_ptr converting parameters performance, #{ITER}x calls" -ptr = PointerType.new(FFI::MemoryPointer.new(:int)) -10.times { - puts Benchmark.measure { - i = 0; while i < ITER - LibTest.bench_conv(ptr, ptr, ptr) - LibTest.bench_conv(ptr, ptr, ptr) - LibTest.bench_conv(ptr, ptr, ptr) - LibTest.bench_conv(ptr, ptr, ptr) - i += 4 - end + puts "Benchmark [ :pointer, :pointer, :pointer ], :void with to_ptr converting parameters performance, #{ITER}x calls" + ptr = PointerType.new(FFI::MemoryPointer.new(:int)) + 10.times { + puts Benchmark.measure { + i = 0; while i < ITER + LibTest.bench_conv(ptr, ptr, ptr) + LibTest.bench_conv(ptr, ptr, ptr) + LibTest.bench_conv(ptr, ptr, ptr) + LibTest.bench_conv(ptr, ptr, ptr) + i += 4 + end + } } -} +end diff --git a/bench/bench_PPrV.rb b/bench/bench_PPrV.rb index f4e5afb7e..6842110e1 100644 --- a/bench/bench_PPrV.rb +++ b/bench/bench_PPrV.rb @@ -1,69 +1,69 @@ require_relative 'bench_helper' -module LibTest - extend FFI::Library - ffi_lib LIBTEST_PATH +module BenchPPrV + module LibTest + extend FFI::Library + ffi_lib LIBTEST_PATH - attach_function :bench, :bench_PP_v, [ :buffer_in, :buffer_in ], :void -end + attach_function :bench, :bench_PP_v, [ :buffer_in, :buffer_in ], :void + end -puts "Benchmark [ :buffer_in, :pointer ], :void performance, #{ITER}x calls" -ptr = FFI::MemoryPointer.new :int -10.times { - puts Benchmark.measure { - ITER.times { LibTest.bench(ptr, ptr) } + puts "Benchmark [ :buffer_in, :pointer ], :void performance, #{ITER}x calls" + ptr = FFI::MemoryPointer.new :int + 10.times { + puts Benchmark.measure { + ITER.times { LibTest.bench(ptr, ptr) } + } } -} -puts "Benchmark [ :buffer_in, :buffer_in ], :void with Struct parameters performance #{ITER}x calls" + puts "Benchmark [ :buffer_in, :buffer_in ], :void with Struct parameters performance #{ITER}x calls" -class TestStruct < FFI::Struct - layout :i, :int -end + class TestStruct < FFI::Struct + layout :i, :int + end -s = TestStruct.new(FFI::MemoryPointer.new(TestStruct)); -10.times { - puts Benchmark.measure { - ITER.times { LibTest.bench(s, s) } + s = TestStruct.new(FFI::MemoryPointer.new(TestStruct)); + 10.times { + puts Benchmark.measure { + ITER.times { LibTest.bench(s, s) } + } } -} -puts "Benchmark [ :buffer_in, :buffer_in ], :void with nil parameters performance, #{ITER}x calls" -10.times { - puts Benchmark.measure { - ITER.times { LibTest.bench(nil, nil) } + puts "Benchmark [ :buffer_in, :buffer_in ], :void with nil parameters performance, #{ITER}x calls" + 10.times { + puts Benchmark.measure { + ITER.times { LibTest.bench(nil, nil) } + } } -} -puts "Benchmark [ :buffer_in, :buffer_in ], :void with Buffer parameters performance, #{ITER}x calls" -ptr = FFI::Buffer.new(:int) -10.times { - puts Benchmark.measure { - ITER.times { LibTest.bench(ptr, ptr) } + puts "Benchmark [ :buffer_in, :buffer_in ], :void with Buffer parameters performance, #{ITER}x calls" + ptr = FFI::Buffer.new(:int) + 10.times { + puts Benchmark.measure { + ITER.times { LibTest.bench(ptr, ptr) } + } } -} -puts "Benchmark [ :buffer_in, :buffer_in ], :void with one nil, one Buffer parameter performance, #{ITER}x calls" -ptr = FFI::Buffer.new(:int) -10.times { - puts Benchmark.measure { - ITER.times { LibTest.bench(nil, ptr) } + puts "Benchmark [ :buffer_in, :buffer_in ], :void with one nil, one Buffer parameter performance, #{ITER}x calls" + ptr = FFI::Buffer.new(:int) + 10.times { + puts Benchmark.measure { + ITER.times { LibTest.bench(nil, ptr) } + } } -} -puts "Benchmark [ :buffer_in, :buffer_in ], :void with loop-allocated Buffer parameters performance, #{ITER}x calls" -10.times { - puts Benchmark.measure { - ITER.times { LibTest.bench(FFI::Buffer.new(:int), FFI::Buffer.new(:int)) } + puts "Benchmark [ :buffer_in, :buffer_in ], :void with loop-allocated Buffer parameters performance, #{ITER}x calls" + 10.times { + puts Benchmark.measure { + ITER.times { LibTest.bench(FFI::Buffer.new(:int), FFI::Buffer.new(:int)) } + } } -} -puts "Benchmark [ :buffer_in, :buffer_in ], :void with loop-allocated MemoryPointer parameters performance, #{ITER}x calls" -10.times { - puts Benchmark.measure { - ITER.times { LibTest.bench(FFI::MemoryPointer.new(:int), FFI::MemoryPointer.new(:int)) } + puts "Benchmark [ :buffer_in, :buffer_in ], :void with loop-allocated MemoryPointer parameters performance, #{ITER}x calls" + 10.times { + puts Benchmark.measure { + ITER.times { LibTest.bench(FFI::MemoryPointer.new(:int), FFI::MemoryPointer.new(:int)) } + } } -} - - +end diff --git a/bench/bench_PrV.rb b/bench/bench_PrV.rb index 7e3f401ad..baf1935e7 100644 --- a/bench/bench_PrV.rb +++ b/bench/bench_PrV.rb @@ -1,101 +1,101 @@ require_relative 'bench_helper' -module LibTest - extend FFI::Library - ffi_lib LIBTEST_PATH +module BenchPrV + module LibTest + extend FFI::Library + ffi_lib LIBTEST_PATH - attach_function :bench, :bench_P_v, [ :buffer_in ], :void, :save_errno => false -end + attach_function :bench, :bench_P_v, [ :buffer_in ], :void, :save_errno => false + end -puts "Benchmark [ :buffer_in ], :void performance (pre allocated pointer), #{ITER}x calls" -ptr = FFI::MemoryPointer.new :int -10.times { - puts Benchmark.measure { - 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 (pre allocated pointer), #{ITER}x calls" + ptr = FFI::MemoryPointer.new :int + 10.times { + puts Benchmark.measure { + 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 { - 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 (nil param), #{ITER}x calls" + 10.times { + puts Benchmark.measure { + 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 (pre allocated Buffer param), #{ITER}x calls" -ptr = FFI::Buffer.new :int -10.times { - puts Benchmark.measure { - 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 (pre allocated Buffer param), #{ITER}x calls" + ptr = FFI::Buffer.new :int + 10.times { + puts Benchmark.measure { + 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" -ptr = 'test' -10.times { - puts Benchmark.measure { - 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" + ptr = 'test' + 10.times { + puts Benchmark.measure { + 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 { - 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 Buffer param), #{ITER}x calls" + 10.times { + puts Benchmark.measure { + 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 { - 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 String param), #{ITER}x calls" + 10.times { + puts Benchmark.measure { + 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 { - 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 + puts "Benchmark [ :buffer_in ], :void performance (loop-allocated MemoryPointer param), #{ITER}x calls" + 10.times { + puts Benchmark.measure { + 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 + } } -} - - +end diff --git a/bench/bench_SrV.rb b/bench/bench_SrV.rb index 04128d540..49c41082f 100644 --- a/bench/bench_SrV.rb +++ b/bench/bench_SrV.rb @@ -1,16 +1,18 @@ require_relative 'bench_helper' -module LibTest - extend FFI::Library - ffi_lib LIBTEST_PATH - attach_function :bench_S_v, [ :string ], :void -end +module BenchSrV + module LibTest + extend FFI::Library + ffi_lib LIBTEST_PATH + attach_function :bench_S_v, [ :string ], :void + end -puts "Benchmark [ :string ], :void performance, #{ITER}x calls" + puts "Benchmark [ :string ], :void performance, #{ITER}x calls" -s = 'a' * 1000 -10.times { - puts Benchmark.measure { - ITER.times { LibTest.bench_S_v(s) } + s = 'a' * 1000 + 10.times { + puts Benchmark.measure { + ITER.times { LibTest.bench_S_v(s) } + } } -} +end diff --git a/bench/bench_VrI.rb b/bench/bench_VrI.rb index 82850e94b..8ee08bb52 100644 --- a/bench/bench_VrI.rb +++ b/bench/bench_VrI.rb @@ -1,36 +1,38 @@ require_relative 'bench_helper' -module LibTest - extend FFI::Library - ffi_lib LIBTEST_PATH - attach_function :ffi_bench, :returnInt, [ ], :int, :save_errno => false - def self.rb_bench; 0xdeadbeef; end -end +module BenchVrI + module LibTest + extend FFI::Library + ffi_lib LIBTEST_PATH + attach_function :ffi_bench, :returnInt, [ ], :int, :save_errno => false + def self.rb_bench; 0xdeadbeef; end + end -puts "Benchmark [ ], :int performance, #{ITER}x calls" -10.times { - puts Benchmark.measure { - i = 0; max = ITER / 4 - while i < max - LibTest.ffi_bench - LibTest.ffi_bench - LibTest.ffi_bench - LibTest.ffi_bench - i += 1 - end + puts "Benchmark [ ], :int performance, #{ITER}x calls" + 10.times { + puts Benchmark.measure { + i = 0; max = ITER / 4 + while i < max + LibTest.ffi_bench + LibTest.ffi_bench + LibTest.ffi_bench + LibTest.ffi_bench + i += 1 + end + } } -} -puts "Benchmark ruby method(), nil performance, #{ITER}x calls" -10.times { - puts Benchmark.measure { - 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(), nil performance, #{ITER}x calls" + 10.times { + puts Benchmark.measure { + i = 0; max = ITER / 4 + while i < max + LibTest.rb_bench + LibTest.rb_bench + LibTest.rb_bench + LibTest.rb_bench + i += 1 + end + } } -} +end diff --git a/bench/bench_VrV.rb b/bench/bench_VrV.rb index 7ef9c07a4..0210ae3ca 100644 --- a/bench/bench_VrV.rb +++ b/bench/bench_VrV.rb @@ -1,43 +1,44 @@ require_relative 'bench_helper' -module LibTest - extend FFI::Library - ffi_lib LIBTEST_PATH - attach_function :ffi_bench, :returnVoid, [ ], :void - attach_function :ffi_bench_noerrno, :returnVoid, [ ], :void, :save_errno => false - def self.rb_bench; nil; end -end +module BenchVrV + module LibTest + extend FFI::Library + ffi_lib LIBTEST_PATH + attach_function :ffi_bench, :returnVoid, [ ], :void + attach_function :ffi_bench_noerrno, :returnVoid, [ ], :void, :save_errno => false + def self.rb_bench; nil; 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 + puts "Benchmark [ ], :void performance, #{ITER}x calls" + 10.times { + puts Benchmark.measure { + i = 0 + while i < ITER + LibTest.ffi_bench + i += 1 + end + } } -} -puts "Benchmark [ ], :void no-errno performance, #{ITER}x calls" -10.times { - puts Benchmark.measure { - i = 0 - while i < ITER - LibTest.ffi_bench_noerrno - i += 1 - end + puts "Benchmark [ ], :void no-errno performance, #{ITER}x calls" + 10.times { + puts Benchmark.measure { + i = 0 + while i < ITER + LibTest.ffi_bench_noerrno + 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 + 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 + } } -} - +end diff --git a/bench/bench_autoptr.rb b/bench/bench_autoptr.rb index 3c8a635b0..f8946d105 100644 --- a/bench/bench_autoptr.rb +++ b/bench/bench_autoptr.rb @@ -1,39 +1,41 @@ require_relative 'bench_helper' -iter = ITER +module BenchAutoptr + iter = ITER -class Ptr < FFI::AutoPointer - def self.release(ptr) - LibC.free(ptr); + class Ptr < FFI::AutoPointer + def self.release(ptr) + LibC.free(ptr); + end end -end -module LibC - extend FFI::Library - ffi_lib FFI::Library::LIBC - attach_function :malloc, [ :long ], Ptr, :ignore_error => true - attach_function :malloc2, :malloc, [ :long ], :pointer, :ignore_error => true - attach_function :free, [ :pointer ], :void, :ignore_error => true - def self.finalizer(ptr) - proc { LibC.free(ptr) } + module LibC + extend FFI::Library + ffi_lib FFI::Library::LIBC + attach_function :malloc, [ :long ], Ptr, :ignore_error => true + attach_function :malloc2, :malloc, [ :long ], :pointer, :ignore_error => true + attach_function :free, [ :pointer ], :void, :ignore_error => true + def self.finalizer(ptr) + proc { LibC.free(ptr) } + end end -end -puts "Benchmark AutoPointer.new performance, #{iter}x" -10.times { - puts Benchmark.measure { - iter.times { LibC.malloc(4) } + puts "Benchmark AutoPointer.new performance, #{iter}x" + 10.times { + puts Benchmark.measure { + iter.times { LibC.malloc(4) } + } } -} -puts "Benchmark ObjectSpace finalizer performance, #{iter}x" -10.times { - puts Benchmark.measure { - iter.times { - ptr = LibC.malloc2(4) - ptr2 = FFI::Pointer.new(ptr) - ObjectSpace.define_finalizer(ptr2, LibC.finalizer(ptr)) + puts "Benchmark ObjectSpace finalizer performance, #{iter}x" + 10.times { + puts Benchmark.measure { + iter.times { + ptr = LibC.malloc2(4) + ptr2 = FFI::Pointer.new(ptr) + ObjectSpace.define_finalizer(ptr2, LibC.finalizer(ptr)) + } } } -} +end diff --git a/bench/bench_buffer.rb b/bench/bench_buffer.rb index 8f030ce61..4c2322d65 100644 --- a/bench/bench_buffer.rb +++ b/bench/bench_buffer.rb @@ -1,109 +1,111 @@ require_relative 'bench_helper' -iter = ITER +module BenchBuffer + iter = ITER -module BufferBench - extend FFI::Library - extend FFI::Library - ffi_lib LIBTEST_PATH - attach_function :bench_s32_v, [ :int ], :void - begin - attach_function :bench_buffer_in, :ptr_ret_int32_t, [ :buffer_in, :int ], :void - rescue FFI::NotFoundError - # NetBSD uses #define instead of typedef for these - attach_function :bench_buffer_in, :ptr_ret___int32_t, [ :buffer_in, :int ], :void + module BufferBench + extend FFI::Library + extend FFI::Library + ffi_lib LIBTEST_PATH + attach_function :bench_s32_v, [ :int ], :void + begin + attach_function :bench_buffer_in, :ptr_ret_int32_t, [ :buffer_in, :int ], :void + rescue FFI::NotFoundError + # NetBSD uses #define instead of typedef for these + attach_function :bench_buffer_in, :ptr_ret___int32_t, [ :buffer_in, :int ], :void + end + begin + attach_function :bench_buffer_inout, :ptr_ret_int32_t, [ :buffer_inout, :int ], :void + rescue FFI::NotFoundError + # NetBSD uses #define instead of typedef for these + attach_function :bench_buffer_inout, :ptr_ret___int32_t, [ :buffer_inout, :int ], :void + end + begin + attach_function :bench_buffer_out, :ptr_ret_int32_t, [ :buffer_out, :int ], :void + rescue FFI::NotFoundError + # NetBSD uses #define instead of typedef for these + attach_function :bench_buffer_out, :ptr_ret___int32_t, [ :buffer_out, :int ], :void + end end - begin - attach_function :bench_buffer_inout, :ptr_ret_int32_t, [ :buffer_inout, :int ], :void - rescue FFI::NotFoundError - # NetBSD uses #define instead of typedef for these - attach_function :bench_buffer_inout, :ptr_ret___int32_t, [ :buffer_inout, :int ], :void + class IntStruct < FFI::Struct + layout :i, :int end - begin - attach_function :bench_buffer_out, :ptr_ret_int32_t, [ :buffer_out, :int ], :void - rescue FFI::NotFoundError - # NetBSD uses #define instead of typedef for these - attach_function :bench_buffer_out, :ptr_ret___int32_t, [ :buffer_out, :int ], :void - end -end -class IntStruct < FFI::Struct - layout :i, :int -end -puts "Benchmark FFI call(MemoryPointer.new(:int, 1, true)) performance, #{iter}x" -10.times { - puts Benchmark.measure { - iter.times { BufferBench.bench_buffer_inout(FFI::MemoryPointer.new(:int, 1, true), 0) } + puts "Benchmark FFI call(MemoryPointer.new(:int, 1, true)) performance, #{iter}x" + 10.times { + puts Benchmark.measure { + iter.times { BufferBench.bench_buffer_inout(FFI::MemoryPointer.new(:int, 1, true), 0) } + } } -} -puts "Benchmark FFI call(MemoryPointer.new(4, 1, true)) performance, #{iter}x" -10.times { - puts Benchmark.measure { - iter.times { BufferBench.bench_buffer_inout(FFI::MemoryPointer.new(4, 1, true), 0) } + puts "Benchmark FFI call(MemoryPointer.new(4, 1, true)) performance, #{iter}x" + 10.times { + puts Benchmark.measure { + iter.times { BufferBench.bench_buffer_inout(FFI::MemoryPointer.new(4, 1, true), 0) } + } } -} -puts "Benchmark FFI call(MemoryPointer.new(IntStruct, 1, true)) performance, #{iter}x" -10.times { - puts Benchmark.measure { - iter.times { BufferBench.bench_buffer_inout(FFI::MemoryPointer.new(IntStruct, 1, true), 0) } + puts "Benchmark FFI call(MemoryPointer.new(IntStruct, 1, true)) performance, #{iter}x" + 10.times { + puts Benchmark.measure { + iter.times { BufferBench.bench_buffer_inout(FFI::MemoryPointer.new(IntStruct, 1, true), 0) } + } } -} -puts "Benchmark FFI call(0.chr * 4) performance, #{iter}x" -10.times { - puts Benchmark.measure { - iter.times { BufferBench.bench_buffer_inout(0.chr * 4, 0) } + puts "Benchmark FFI call(0.chr * 4) performance, #{iter}x" + 10.times { + puts Benchmark.measure { + iter.times { BufferBench.bench_buffer_inout(0.chr * 4, 0) } + } } -} -puts "Benchmark FFI call(Buffer.alloc_inout(:int, 1, true)) performance, #{iter}x" -10.times { - puts Benchmark.measure { - iter.times { BufferBench.bench_buffer_inout(FFI::Buffer.alloc_inout(:int, 1, true), 0) } + puts "Benchmark FFI call(Buffer.alloc_inout(:int, 1, true)) performance, #{iter}x" + 10.times { + puts Benchmark.measure { + iter.times { BufferBench.bench_buffer_inout(FFI::Buffer.alloc_inout(:int, 1, true), 0) } + } } -} -puts "Benchmark FFI call(Buffer.alloc_inout(IntStruct, 1, true)) performance, #{iter}x" -10.times { - puts Benchmark.measure { - iter.times { BufferBench.bench_buffer_inout(FFI::Buffer.alloc_inout(IntStruct, 1, true), 0) } + puts "Benchmark FFI call(Buffer.alloc_inout(IntStruct, 1, true)) performance, #{iter}x" + 10.times { + puts Benchmark.measure { + iter.times { BufferBench.bench_buffer_inout(FFI::Buffer.alloc_inout(IntStruct, 1, true), 0) } + } } -} -puts "Benchmark FFI call(Buffer.alloc_inout(4, 1, true)) performance, #{iter}x" -10.times { - puts Benchmark.measure { - iter.times { BufferBench.bench_buffer_inout(FFI::Buffer.alloc_inout(4, 1, true), 0) } + puts "Benchmark FFI call(Buffer.alloc_inout(4, 1, true)) performance, #{iter}x" + 10.times { + puts Benchmark.measure { + iter.times { BufferBench.bench_buffer_inout(FFI::Buffer.alloc_inout(4, 1, true), 0) } + } } -} -puts "Benchmark FFI call(Buffer.alloc_in(:int, 1, true)) performance, #{iter}x" -10.times { - puts Benchmark.measure { - iter.times { BufferBench.bench_buffer_in(FFI::Buffer.alloc_in(:int, 1, true), 0) } + puts "Benchmark FFI call(Buffer.alloc_in(:int, 1, true)) performance, #{iter}x" + 10.times { + puts Benchmark.measure { + iter.times { BufferBench.bench_buffer_in(FFI::Buffer.alloc_in(:int, 1, true), 0) } + } } -} -puts "Benchmark FFI call(Buffer.alloc_in(4, 1, true)) performance, #{iter}x" -10.times { - puts Benchmark.measure { - iter.times { BufferBench.bench_buffer_in(FFI::Buffer.alloc_in(4, 1, true), 0) } + puts "Benchmark FFI call(Buffer.alloc_in(4, 1, true)) performance, #{iter}x" + 10.times { + puts Benchmark.measure { + iter.times { BufferBench.bench_buffer_in(FFI::Buffer.alloc_in(4, 1, true), 0) } + } } -} -puts "Benchmark FFI call(Buffer.alloc_in(IntStruct, 1, true)) performance, #{iter}x" -10.times { - puts Benchmark.measure { - iter.times { BufferBench.bench_buffer_in(FFI::Buffer.alloc_in(IntStruct, 1, true), 0) } + puts "Benchmark FFI call(Buffer.alloc_in(IntStruct, 1, true)) performance, #{iter}x" + 10.times { + puts Benchmark.measure { + iter.times { BufferBench.bench_buffer_in(FFI::Buffer.alloc_in(IntStruct, 1, true), 0) } + } } -} -puts "Benchmark FFI call(Buffer.alloc_out(:int, 1, true)) performance, #{iter}x" -10.times { - puts Benchmark.measure { - iter.times { BufferBench.bench_buffer_out(FFI::Buffer.alloc_out(:int, 1, true), 0) } + puts "Benchmark FFI call(Buffer.alloc_out(:int, 1, true)) performance, #{iter}x" + 10.times { + puts Benchmark.measure { + iter.times { BufferBench.bench_buffer_out(FFI::Buffer.alloc_out(:int, 1, true), 0) } + } } -} -puts "Benchmark FFI call(Buffer.alloc_out(IntStruct, 1, true)) performance, #{iter}x" -10.times { - puts Benchmark.measure { - iter.times { BufferBench.bench_buffer_out(FFI::Buffer.alloc_out(IntStruct, 1, true), 0) } + puts "Benchmark FFI call(Buffer.alloc_out(IntStruct, 1, true)) performance, #{iter}x" + 10.times { + puts Benchmark.measure { + iter.times { BufferBench.bench_buffer_out(FFI::Buffer.alloc_out(IntStruct, 1, true), 0) } + } } -} -puts "Benchmark FFI call(Buffer.alloc_out(4, 1, true)) performance, #{iter}x" -10.times { - puts Benchmark.measure { - iter.times { BufferBench.bench_buffer_out(FFI::Buffer.alloc_out(4, 1, true), 0) } + puts "Benchmark FFI call(Buffer.alloc_out(4, 1, true)) performance, #{iter}x" + 10.times { + puts Benchmark.measure { + iter.times { BufferBench.bench_buffer_out(FFI::Buffer.alloc_out(4, 1, true), 0) } + } } -} +end diff --git a/bench/bench_buffer_alloc.rb b/bench/bench_buffer_alloc.rb index 0f223ffe0..6314b6328 100644 --- a/bench/bench_buffer_alloc.rb +++ b/bench/bench_buffer_alloc.rb @@ -1,43 +1,45 @@ require_relative 'bench_helper' -iter = ITER +module BenchBufferAlloc + iter = ITER -puts "Benchmark Buffer.new(:int, 1, true)) performance, #{iter}x" -10.times { - puts Benchmark.measure { - i = 0; while i < iter - FFI::Buffer.new(:int, 1, true) - i += 1 - end + puts "Benchmark Buffer.new(:int, 1, true)) performance, #{iter}x" + 10.times { + puts Benchmark.measure { + 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.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 { - i = 0; while i < iter - FFI::Buffer.new(4, 1, true) - i += 1 - end + puts "Benchmark Buffer.new(4, 1, true)) performance, #{iter}x" + 10.times { + puts Benchmark.measure { + 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 { - i = 0; while i < iter - FFI::Buffer.new(256, 1, true) - i += 1 - end + puts "Benchmark Buffer.new(256, 1, true)) performance, #{iter}x" + 10.times { + puts Benchmark.measure { + i = 0; while i < iter + FFI::Buffer.new(256, 1, true) + i += 1 + end + } } -} +end diff --git a/bench/bench_buffer_fill.rb b/bench/bench_buffer_fill.rb index 66e6efd35..259a6ae90 100644 --- a/bench/bench_buffer_fill.rb +++ b/bench/bench_buffer_fill.rb @@ -1,43 +1,45 @@ require_relative 'bench_helper' -iter = ITER +module BenchBufferFill + iter = ITER -puts "Benchmark Buffer#put_array_of_float performance, #{iter}x" + puts "Benchmark Buffer#put_array_of_float performance, #{iter}x" -5.times { - ptr = FFI::Buffer.new(:float, 8, false) - puts Benchmark.measure { - iter.times { - ptr.put_array_of_float(0, [ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8 ]) + 5.times { + ptr = FFI::Buffer.new(:float, 8, false) + puts Benchmark.measure { + iter.times { + ptr.put_array_of_float(0, [ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8 ]) + } } } -} -puts "Benchmark Buffer.new(:float, 8, false)).put_array_of_float performance, #{iter}x" -5.times { - puts Benchmark.measure { - iter.times { - ptr = FFI::Buffer.new(:float, 8, false) - ptr.put_array_of_float(0, [ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8 ]) + puts "Benchmark Buffer.new(:float, 8, false)).put_array_of_float performance, #{iter}x" + 5.times { + puts Benchmark.measure { + iter.times { + ptr = FFI::Buffer.new(:float, 8, false) + ptr.put_array_of_float(0, [ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8 ]) + } } } -} -module LibTest - extend FFI::Library - ffi_lib LIBTEST_PATH - - attach_function :bench, :bench_P_v, [ :buffer_in ], :void -end - -puts "Benchmark Buffer alloc+fill+call performance, #{iter}x" -5.times { - puts Benchmark.measure { - iter.times { - ptr = FFI::Buffer.new(:float, 8, false) - ptr.put_array_of_float(0, [ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8 ]) - LibTest.bench(ptr) + module LibTest + extend FFI::Library + ffi_lib LIBTEST_PATH + + attach_function :bench, :bench_P_v, [ :buffer_in ], :void + end + + puts "Benchmark Buffer alloc+fill+call performance, #{iter}x" + 5.times { + puts Benchmark.measure { + iter.times { + ptr = FFI::Buffer.new(:float, 8, false) + ptr.put_array_of_float(0, [ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8 ]) + LibTest.bench(ptr) + } } } -} +end diff --git a/bench/bench_chmod.rb b/bench/bench_chmod.rb index c0b683845..71b3576d3 100644 --- a/bench/bench_chmod.rb +++ b/bench/bench_chmod.rb @@ -1,34 +1,36 @@ require_relative 'bench_helper' -iter = ITER -file = "README" +module BenchChmod + iter = ITER + file = "README" -module Posix - extend FFI::Library - ffi_lib FFI::Library::LIBC + module Posix + extend FFI::Library + ffi_lib FFI::Library::LIBC - def self.chmod(mode, path) - if self._chmod(path, mode) != 0 + def self.chmod(mode, path) + if self._chmod(path, mode) != 0 + end + end + if FFI::Platform.windows? + attach_function :_chmod, :_chmod, [ :string, :int ], :int + else + attach_function :_chmod, :chmod, [ :string, :int ], :int end end - if FFI::Platform.windows? - attach_function :_chmod, :_chmod, [ :string, :int ], :int - else - attach_function :_chmod, :chmod, [ :string, :int ], :int - end -end -puts "Benchmark FFI chmod performance, #{iter}x changing mode" -10.times { - puts Benchmark.measure { - iter.times { Posix.chmod(0622, __FILE__) } + puts "Benchmark FFI chmod performance, #{iter}x changing mode" + 10.times { + puts Benchmark.measure { + iter.times { Posix.chmod(0622, __FILE__) } + } } -} -puts "Benchmark Ruby File.chmod performance, #{iter}x changing mode" -10.times { - puts Benchmark.measure { - iter.times { File.chmod(0622, __FILE__) } + puts "Benchmark Ruby File.chmod performance, #{iter}x changing mode" + 10.times { + puts Benchmark.measure { + iter.times { File.chmod(0622, __FILE__) } + } } -} +end diff --git a/bench/bench_closure_IIIrV.rb b/bench/bench_closure_IIIrV.rb index 1c164f279..1ecb3f339 100644 --- a/bench/bench_closure_IIIrV.rb +++ b/bench/bench_closure_IIIrV.rb @@ -1,67 +1,69 @@ require_relative 'bench_helper' -module LibTest - extend FFI::Library - ffi_lib LIBTEST_PATH - callback :closureIIIrV, [ :int, :int, :int ], :void - attach_function :ffi_bench, :testClosureIIIrV, [ :closureIIIrV, :int, :int, :int ], :void - def self.rb_bench(a, b, c, &block); yield(a, b, c); end -end +module BenchClosureIIIrV + module LibTest + extend FFI::Library + ffi_lib LIBTEST_PATH + callback :closureIIIrV, [ :int, :int, :int ], :void + attach_function :ffi_bench, :testClosureIIIrV, [ :closureIIIrV, :int, :int, :int ], :void + def self.rb_bench(a, b, c, &block); yield(a, b, c); end + end -class Foo - def call(a, b, c); nil; end -end + class Foo + def call(a, b, c); nil; end + end -puts "Benchmark [ ], :void closure block, #{ITER}x calls" -10.times { - puts Benchmark.measure { - ITER.times { LibTest.ffi_bench(1, 2, 3) { } } + puts "Benchmark [ ], :void closure block, #{ITER}x calls" + 10.times { + puts Benchmark.measure { + ITER.times { LibTest.ffi_bench(1, 2, 3) { } } + } } -} -puts "Benchmark [ ], :void closure callable, #{ITER}x calls" -10.times { - puts Benchmark.measure { - ITER.times { LibTest.ffi_bench(Foo.new, 1, 2, 3) } + puts "Benchmark [ ], :void closure callable, #{ITER}x calls" + 10.times { + puts Benchmark.measure { + ITER.times { LibTest.ffi_bench(Foo.new, 1, 2, 3) } + } } -} -puts "Benchmark [ ], :void pre-allocated function with block, #{ITER}x calls" -10.times { - fn = FFI::Function.new(:void, [ :int, :int, :int ]) {} - puts Benchmark.measure { - ITER.times { LibTest.ffi_bench(fn, 1, 2, 3) } + puts "Benchmark [ ], :void pre-allocated function with block, #{ITER}x calls" + 10.times { + fn = FFI::Function.new(:void, [ :int, :int, :int ]) {} + puts Benchmark.measure { + ITER.times { LibTest.ffi_bench(fn, 1, 2, 3) } + } } -} -puts "Benchmark [ ], :void pre-allocated function with callable, #{ITER}x calls" -10.times { - fn = FFI::Function.new(:void, [ :int, :int, :int ], Foo.new) - puts Benchmark.measure { - ITER.times { LibTest.ffi_bench(fn, 1, 2, 3) } + puts "Benchmark [ ], :void pre-allocated function with callable, #{ITER}x calls" + 10.times { + fn = FFI::Function.new(:void, [ :int, :int, :int ], Foo.new) + puts Benchmark.measure { + ITER.times { LibTest.ffi_bench(fn, 1, 2, 3) } + } } -} -puts "Benchmark [ ], :void pre-allocated proc, #{ITER}x calls" -10.times { - proc = lambda { |a,b,c| } - puts Benchmark.measure { - ITER.times { LibTest.ffi_bench(proc, 1, 2, 3) } + puts "Benchmark [ ], :void pre-allocated proc, #{ITER}x calls" + 10.times { + proc = lambda { |a,b,c| } + puts Benchmark.measure { + ITER.times { LibTest.ffi_bench(proc, 1, 2, 3) } + } } -} -puts "Benchmark [ ], :void pre-allocated callable, #{ITER}x calls" -10.times { - proc = Foo.new - puts Benchmark.measure { - ITER.times { LibTest.ffi_bench(proc, 1, 2, 3) } + puts "Benchmark [ ], :void pre-allocated callable, #{ITER}x calls" + 10.times { + proc = Foo.new + puts Benchmark.measure { + ITER.times { LibTest.ffi_bench(proc, 1, 2, 3) } + } } -} -puts "Benchmark ruby method(3 arg), #{ITER}x calls" -10.times { - puts Benchmark.measure { - ITER.times { LibTest.rb_bench(1, 2, 3) {} } + puts "Benchmark ruby method(3 arg), #{ITER}x calls" + 10.times { + puts Benchmark.measure { + ITER.times { LibTest.rb_bench(1, 2, 3) {} } + } } -} + end diff --git a/bench/bench_closure_IrV.rb b/bench/bench_closure_IrV.rb index 0e4021eab..33fb06482 100644 --- a/bench/bench_closure_IrV.rb +++ b/bench/bench_closure_IrV.rb @@ -1,39 +1,41 @@ require_relative 'bench_helper' -module LibTest - extend FFI::Library - ffi_lib LIBTEST_PATH - callback :closureVrV, [ ], :void - attach_function :ffi_bench, :testClosureIrV, [ :closureVrV, :int ], :void - def self.rb_bench(i, &block); nil; end -end +module BenchClosureIrV + module LibTest + extend FFI::Library + ffi_lib LIBTEST_PATH + callback :closureVrV, [ ], :void + attach_function :ffi_bench, :testClosureIrV, [ :closureVrV, :int ], :void + def self.rb_bench(i, &block); nil; end + end -puts "Benchmark [ ], :void closure block performance, #{ITER}x calls" -10.times { - puts Benchmark.measure { - ITER.times { LibTest.ffi_bench(1) { |i| } } + puts "Benchmark [ ], :void closure block performance, #{ITER}x calls" + 10.times { + puts Benchmark.measure { + ITER.times { LibTest.ffi_bench(1) { |i| } } + } } -} -puts "Benchmark [ ], :void pre-allocated function performance, #{ITER}x calls" -10.times { - fn = FFI::Function.new(:void, [ :int ]) { |i| } - puts Benchmark.measure { - ITER.times { LibTest.ffi_bench(fn, 2) } + puts "Benchmark [ ], :void pre-allocated function performance, #{ITER}x calls" + 10.times { + fn = FFI::Function.new(:void, [ :int ]) { |i| } + puts Benchmark.measure { + ITER.times { LibTest.ffi_bench(fn, 2) } + } } -} -puts "Benchmark [ ], :void pre-allocated callable performance, #{ITER}x calls" -10.times { - fn = Proc.new { |i| } - puts Benchmark.measure { - ITER.times { LibTest.ffi_bench(fn, 2) } + puts "Benchmark [ ], :void pre-allocated callable performance, #{ITER}x calls" + 10.times { + fn = Proc.new { |i| } + puts Benchmark.measure { + ITER.times { LibTest.ffi_bench(fn, 2) } + } } -} -puts "Benchmark ruby method(1 arg) performance, #{ITER}x calls" -10.times { - puts Benchmark.measure { - ITER.times { LibTest.rb_bench(1) {} } + puts "Benchmark ruby method(1 arg) performance, #{ITER}x calls" + 10.times { + puts Benchmark.measure { + ITER.times { LibTest.rb_bench(1) {} } + } } -} + end diff --git a/bench/bench_closure_VrV.rb b/bench/bench_closure_VrV.rb index c8239f992..eb2992e43 100644 --- a/bench/bench_closure_VrV.rb +++ b/bench/bench_closure_VrV.rb @@ -1,53 +1,55 @@ require_relative 'bench_helper' -module LibTest - extend FFI::Library - ffi_lib LIBTEST_PATH - callback :closureVrV, [ ], :void - - attach_function :ffi_bench, :testClosureVrV, [ :closureVrV ], :void - @blocking = true - attach_function :threaded_bench, :testThreadedClosureVrV, [ :closureVrV, :int ], :void - - def self.rb_bench(&block) - yield +module BenchClosureVrV + module LibTest + extend FFI::Library + ffi_lib LIBTEST_PATH + callback :closureVrV, [ ], :void + + attach_function :ffi_bench, :testClosureVrV, [ :closureVrV ], :void + @blocking = true + attach_function :threaded_bench, :testThreadedClosureVrV, [ :closureVrV, :int ], :void + + def self.rb_bench(&block) + yield + end end -end -puts "Benchmark [ ], :void closure block performance, #{ITER}x calls" -10.times { - puts Benchmark.measure { - ITER.times { LibTest.ffi_bench { } } + puts "Benchmark [ ], :void closure block performance, #{ITER}x calls" + 10.times { + puts Benchmark.measure { + ITER.times { LibTest.ffi_bench { } } + } } -} -puts "Benchmark [ ], :void pre-allocated function pointer performance, #{ITER}x calls" -10.times { - fn = FFI::Function.new(:void, []) {} - puts Benchmark.measure { - ITER.times { LibTest.ffi_bench fn } + puts "Benchmark [ ], :void pre-allocated function pointer performance, #{ITER}x calls" + 10.times { + fn = FFI::Function.new(:void, []) {} + puts Benchmark.measure { + ITER.times { LibTest.ffi_bench fn } + } } -} -puts "Benchmark [ ], :void pre-allocated closure performance, #{ITER}x calls" -10.times { - proc = lambda {} - puts Benchmark.measure { - ITER.times { LibTest.ffi_bench proc } + puts "Benchmark [ ], :void pre-allocated closure performance, #{ITER}x calls" + 10.times { + proc = lambda {} + puts Benchmark.measure { + ITER.times { LibTest.ffi_bench proc } + } } -} -puts "Benchmark [ ], :void non-ruby thread closure performance, #{ITER}x calls" -10.times { - fn = FFI::Function.new(:void, []) {} - puts Benchmark.measure { - LibTest.threaded_bench(fn, ITER) + puts "Benchmark [ ], :void non-ruby thread closure performance, #{ITER}x calls" + 10.times { + fn = FFI::Function.new(:void, []) {} + puts Benchmark.measure { + LibTest.threaded_bench(fn, ITER) + } } -} -puts "Benchmark ruby method(no arg) performance, #{ITER}x calls" -10.times { - puts Benchmark.measure { - ITER.times { LibTest.rb_bench {} } + puts "Benchmark ruby method(no arg) performance, #{ITER}x calls" + 10.times { + puts Benchmark.measure { + ITER.times { LibTest.rb_bench {} } + } } -} +end diff --git a/bench/bench_enum.rb b/bench/bench_enum.rb index e4d217d8f..21b4de0c9 100644 --- a/bench/bench_enum.rb +++ b/bench/bench_enum.rb @@ -1,45 +1,47 @@ require_relative 'bench_helper' -module LibTest - extend FFI::Library - ffi_lib LIBTEST_PATH - enum :foo, [ :a, :b, :c ] - attach_function :ffi_bench, :bench_s32_v, [ :foo ], :void, :save_errno => false - attach_function :ffi_bench_i, :bench_s32_v, [ :int ], :void, :save_errno => false - def self.rb_bench(i0); nil; end -end +module BenchEnum + module LibTest + extend FFI::Library + ffi_lib LIBTEST_PATH + enum :foo, [ :a, :b, :c ] + attach_function :ffi_bench, :bench_s32_v, [ :foo ], :void, :save_errno => false + attach_function :ffi_bench_i, :bench_s32_v, [ :int ], :void, :save_errno => false + def self.rb_bench(i0); nil; end + end -puts "Benchmark [ enum ], :void performance, #{ITER}x calls" -10.times { - puts Benchmark.measure { - ITER.times { LibTest.ffi_bench(:a) } + puts "Benchmark [ enum ], :void performance, #{ITER}x calls" + 10.times { + puts Benchmark.measure { + ITER.times { LibTest.ffi_bench(:a) } + } } -} -puts "Benchmark [ enum ], :void with int arg performance, #{ITER}x calls" -10.times { - puts Benchmark.measure { - ITER.times { LibTest.ffi_bench(1) } + puts "Benchmark [ enum ], :void with int arg performance, #{ITER}x calls" + 10.times { + puts Benchmark.measure { + ITER.times { LibTest.ffi_bench(1) } + } } -} -puts "Benchmark [ :int ], :void performance, #{ITER}x calls" -10.times { - puts Benchmark.measure { - ITER.times { LibTest.ffi_bench_i(1) } + puts "Benchmark [ :int ], :void performance, #{ITER}x calls" + 10.times { + puts Benchmark.measure { + ITER.times { LibTest.ffi_bench_i(1) } + } } -} -puts "Benchmark [ :int ], :void with enum arg performance, #{ITER}x calls" -10.times { - puts Benchmark.measure { - ITER.times { LibTest.ffi_bench_i(:a) } + puts "Benchmark [ :int ], :void with enum arg performance, #{ITER}x calls" + 10.times { + puts Benchmark.measure { + ITER.times { LibTest.ffi_bench_i(:a) } + } } -} -puts "Benchmark ruby method(1 arg) performance, #{ITER}x calls" -10.times { - puts Benchmark.measure { - ITER.times { LibTest.rb_bench(:a) } + puts "Benchmark ruby method(1 arg) performance, #{ITER}x calls" + 10.times { + puts Benchmark.measure { + ITER.times { LibTest.rb_bench(:a) } + } } -} +end diff --git a/bench/bench_enum_i.rb b/bench/bench_enum_i.rb index a249274df..8a5b6da54 100644 --- a/bench/bench_enum_i.rb +++ b/bench/bench_enum_i.rb @@ -1,17 +1,19 @@ require_relative 'bench_helper' -module LibTest - extend FFI::Library - ffi_lib LIBTEST_PATH - enum :foo, [ :a, :b, :c ] - attach_function :ffi_bench, :bench_s32_v, [ :foo ], :void, :save_errno => true - attach_function :ffi_bench_i, :bench_s32_v, [ :int ], :void, :save_errno => true - def self.rb_bench(i0); nil; end -end +module BenchEnumI + module LibTest + extend FFI::Library + ffi_lib LIBTEST_PATH + enum :foo, [ :a, :b, :c ] + attach_function :ffi_bench, :bench_s32_v, [ :foo ], :void, :save_errno => true + attach_function :ffi_bench_i, :bench_s32_v, [ :int ], :void, :save_errno => true + def self.rb_bench(i0); nil; end + end -puts "Benchmark [ :int ], :void performance, #{ITER}x calls" -10.times { - puts Benchmark.measure { - ITER.times { LibTest.ffi_bench_i(1) } + puts "Benchmark [ :int ], :void performance, #{ITER}x calls" + 10.times { + puts Benchmark.measure { + ITER.times { LibTest.ffi_bench_i(1) } + } } -} +end diff --git a/bench/bench_getlogin.rb b/bench/bench_getlogin.rb index ca2815162..c5824f4e1 100644 --- a/bench/bench_getlogin.rb +++ b/bench/bench_getlogin.rb @@ -1,28 +1,30 @@ require_relative 'bench_helper' require 'etc' -iter = ITER +module BenchGetlogin + iter = ITER -module Posix - extend FFI::Library - ffi_lib FFI::Library::LIBC - attach_function :getlogin, [], :string -end -if Posix.getlogin != Etc.getlogin - raise ArgumentError, "FFI getlogin returned incorrect value" -end + module Posix + extend FFI::Library + ffi_lib FFI::Library::LIBC + attach_function :getlogin, [], :string + end + if Posix.getlogin != Etc.getlogin + raise ArgumentError, "FFI getlogin returned incorrect value" + end -puts "Benchmark FFI getlogin(2) performance, #{ITER}x" + puts "Benchmark FFI getlogin(2) performance, #{ITER}x" -10.times { - puts Benchmark.measure { - iter.times { Posix.getlogin } + 10.times { + puts Benchmark.measure { + iter.times { Posix.getlogin } + } } -} -puts "Benchmark Etc.getlogin performance, #{ITER}x" -10.times { - puts Benchmark.measure { - iter.times { Etc.getlogin } + puts "Benchmark Etc.getlogin performance, #{ITER}x" + 10.times { + puts Benchmark.measure { + iter.times { Etc.getlogin } + } } -} +end diff --git a/bench/bench_getpid.rb b/bench/bench_getpid.rb index e2f468cd0..3cb391e46 100644 --- a/bench/bench_getpid.rb +++ b/bench/bench_getpid.rb @@ -1,34 +1,36 @@ require_relative 'bench_helper' -iter = ITER +module BenchGetpid + iter = ITER -module Posix - extend FFI::Library - ffi_lib 'c' - if FFI::Platform.windows? - attach_function :getpid, :_getpid, [], :uint, :save_errno => false - else - attach_function :getpid, [], :uint, :save_errno => false + module Posix + extend FFI::Library + ffi_lib 'c' + if FFI::Platform.windows? + attach_function :getpid, :_getpid, [], :uint, :save_errno => false + else + attach_function :getpid, [], :uint, :save_errno => false + end end -end -puts "pid=#{Process.pid} Foo.getpid=#{Posix.getpid}" -puts "Benchmark FFI getpid performance, #{iter}x calls" -10.times { - puts Benchmark.measure { - i = 0; while i < iter - Posix.getpid - i += 1 - end + puts "pid=#{Process.pid} Foo.getpid=#{Posix.getpid}" + puts "Benchmark FFI getpid performance, #{iter}x calls" + 10.times { + puts Benchmark.measure { + i = 0; while i < iter + Posix.getpid + i += 1 + end + } } -} -puts "Benchmark Process.pid performance, #{iter}x calls" -10.times { - puts Benchmark.measure { - i = 0; while i < iter - Process.pid - i += 1 - end + puts "Benchmark Process.pid performance, #{iter}x calls" + 10.times { + puts Benchmark.measure { + i = 0; while i < iter + Process.pid + i += 1 + end + } } -} +end diff --git a/bench/bench_gettimeofday.rb b/bench/bench_gettimeofday.rb index 69f801ba8..5884d76c3 100644 --- a/bench/bench_gettimeofday.rb +++ b/bench/bench_gettimeofday.rb @@ -1,51 +1,53 @@ require_relative 'bench_helper' -module Posix - extend FFI::Library - ffi_lib FFI::Library::LIBC +module BenchGettimeofday + module Posix + extend FFI::Library + ffi_lib FFI::Library::LIBC - attach_function :gettimeofday, [ :buffer_out, :pointer ], :int -end -class Timeval < FFI::Struct - layout :tv_sec, :ulong, :tv_nsec, :ulong -end + attach_function :gettimeofday, [ :buffer_out, :pointer ], :int + end + class Timeval < FFI::Struct + layout :tv_sec, :ulong, :tv_nsec, :ulong + end -iter = ITER -puts "Benchmark FFI gettimeofday(2) (nil, nil) performance, #{iter}x" + iter = ITER + puts "Benchmark FFI gettimeofday(2) (nil, nil) performance, #{iter}x" -10.times { - puts Benchmark.measure { - iter.times { Posix.gettimeofday(nil, nil) } + 10.times { + puts Benchmark.measure { + iter.times { Posix.gettimeofday(nil, nil) } + } } -} -puts "Benchmark FFI gettimeofday(2) (Timeval.alloc_out, nil) performance, #{iter}x" -10.times { - puts Benchmark.measure { - iter.times { Posix.gettimeofday(Timeval.alloc_out, nil) } + puts "Benchmark FFI gettimeofday(2) (Timeval.alloc_out, nil) performance, #{iter}x" + 10.times { + puts Benchmark.measure { + iter.times { Posix.gettimeofday(Timeval.alloc_out, nil) } + } } -} -puts "Benchmark FFI gettimeofday(2) (Timeval.new(FFI::MemoryPointer.new), nil) performance, #{iter}x" -10.times { - puts Benchmark.measure { - iter.times { Posix.gettimeofday(Timeval.new(FFI::MemoryPointer.new(Timeval)), nil) } + puts "Benchmark FFI gettimeofday(2) (Timeval.new(FFI::MemoryPointer.new), nil) performance, #{iter}x" + 10.times { + puts Benchmark.measure { + iter.times { Posix.gettimeofday(Timeval.new(FFI::MemoryPointer.new(Timeval)), nil) } + } } -} -puts "Benchmark FFI gettimeofday(2) (Timeval.new(FFI::Buffer.new), nil) performance, #{iter}x" -10.times { - puts Benchmark.measure { - iter.times { Posix.gettimeofday(Timeval.new(FFI::Buffer.new(Timeval)), nil) } + puts "Benchmark FFI gettimeofday(2) (Timeval.new(FFI::Buffer.new), nil) performance, #{iter}x" + 10.times { + puts Benchmark.measure { + iter.times { Posix.gettimeofday(Timeval.new(FFI::Buffer.new(Timeval)), nil) } + } } -} -puts "Benchmark FFI gettimeofday(2) (pre allocated pointer, nil) performance, #{iter}x" -10.times { - t = Timeval.new FFI::MemoryPointer.new(Timeval) - puts Benchmark.measure { - iter.times { Posix.gettimeofday(t, nil) } + puts "Benchmark FFI gettimeofday(2) (pre allocated pointer, nil) performance, #{iter}x" + 10.times { + t = Timeval.new FFI::MemoryPointer.new(Timeval) + puts Benchmark.measure { + iter.times { Posix.gettimeofday(t, nil) } + } } -} -puts "Benchmark Time.now performance, #{iter}x" -10.times { - puts Benchmark.measure { - iter.times { Time.now } + puts "Benchmark Time.now performance, #{iter}x" + 10.times { + puts Benchmark.measure { + iter.times { Time.now } + } } -} +end diff --git a/bench/bench_getuid.rb b/bench/bench_getuid.rb index 8a5719a08..25aec5398 100644 --- a/bench/bench_getuid.rb +++ b/bench/bench_getuid.rb @@ -1,25 +1,27 @@ require_relative 'bench_helper' -iter = ITER +module BenchGetuid + iter = ITER -module Posix - extend FFI::Library - ffi_lib FFI::Library::LIBC - attach_function :getuid, [], :uint -end + module Posix + extend FFI::Library + ffi_lib FFI::Library::LIBC + attach_function :getuid, [], :uint + end -puts "uid=#{Process.pid} Posix.getuid=#{Posix.getuid}" -puts "Benchmark FFI getuid performance, #{iter}x calls" + puts "uid=#{Process.pid} Posix.getuid=#{Posix.getuid}" + puts "Benchmark FFI getuid performance, #{iter}x calls" -10.times { - puts Benchmark.measure { - iter.times { Posix.getuid } + 10.times { + puts Benchmark.measure { + iter.times { Posix.getuid } + } } -} -puts "Benchmark Process.uid performance, #{iter}x calls" -10.times { - puts Benchmark.measure { - iter.times { Process.uid } + puts "Benchmark Process.uid performance, #{iter}x calls" + 10.times { + puts Benchmark.measure { + iter.times { Process.uid } + } } -} +end diff --git a/bench/bench_math.rb b/bench/bench_math.rb index 64796b8f2..bea286840 100644 --- a/bench/bench_math.rb +++ b/bench/bench_math.rb @@ -1,30 +1,32 @@ require_relative 'bench_helper' -module FFIMath - extend FFI::Library - ffi_lib 'm' - attach_function :cos, [ :double ], :double - attach_function :cosf, [ :float ], :float -end -if FFIMath.cos(0) != 1 - raise ArgumentError, "FFI.cos returned incorrect value" -end -puts "Benchmark FFI cos(0) performance, #{ITER}x" -10.times { - puts Benchmark.measure { - ITER.times { FFIMath.cos(0) } +module BenchMath + module FFIMath + extend FFI::Library + ffi_lib 'm' + attach_function :cos, [ :double ], :double + attach_function :cosf, [ :float ], :float + end + if FFIMath.cos(0) != 1 + raise ArgumentError, "FFI.cos returned incorrect value" + end + puts "Benchmark FFI cos(0) performance, #{ITER}x" + 10.times { + puts Benchmark.measure { + ITER.times { FFIMath.cos(0) } + } } -} -puts "Benchmark FFI cosf(0) performance, #{ITER}x" -10.times { - puts Benchmark.measure { - ITER.times { FFIMath.cosf(0) } + puts "Benchmark FFI cosf(0) performance, #{ITER}x" + 10.times { + puts Benchmark.measure { + ITER.times { FFIMath.cosf(0) } + } } -} -puts "Benchmark Math.cos(0) performance, #{ITER}x" -10.times { - puts Benchmark.measure { - ITER.times { Math.cos(0) } + puts "Benchmark Math.cos(0) performance, #{ITER}x" + 10.times { + puts Benchmark.measure { + ITER.times { Math.cos(0) } + } } -} +end diff --git a/bench/bench_memptr_alloc.rb b/bench/bench_memptr_alloc.rb index a6002abf6..446773350 100644 --- a/bench/bench_memptr_alloc.rb +++ b/bench/bench_memptr_alloc.rb @@ -1,59 +1,61 @@ require_relative 'bench_helper' -iter = ITER +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 + 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 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(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 + 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 + } } -} -[ 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 + 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 + } } -} -end - -if defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby" - require 'java' - puts "calling java gc" + [ 8, 16, 32, 64, 128, 256 ].each do |size| + puts "Benchmark MemoryPointer.new(#{size}, 1, true)) performance, #{iter}x" 10.times { - java.lang.System.gc - sleep 1 + 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 diff --git a/bench/bench_memptr_fill.rb b/bench/bench_memptr_fill.rb index 74a940aaa..f80f9d3fc 100644 --- a/bench/bench_memptr_fill.rb +++ b/bench/bench_memptr_fill.rb @@ -1,43 +1,45 @@ require_relative 'bench_helper' -iter = ITER +module BenchMemptrFill + iter = ITER -puts "Benchmark MemoryPointer#put_array_of_float performance, #{iter}x" + puts "Benchmark MemoryPointer#put_array_of_float performance, #{iter}x" -5.times { - ptr = FFI::MemoryPointer.new(:float, 8, false) - puts Benchmark.measure { - iter.times { - ptr.put_array_of_float(0, [ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8 ]) + 5.times { + ptr = FFI::MemoryPointer.new(:float, 8, false) + puts Benchmark.measure { + iter.times { + ptr.put_array_of_float(0, [ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8 ]) + } } } -} -puts "Benchmark MemoryPointer.new(:float, 8, false)).put_array_of_float performance, #{iter}x" -5.times { - puts Benchmark.measure { - iter.times { - ptr = FFI::MemoryPointer.new(:float, 8, false) - ptr.put_array_of_float(0, [ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8 ]) + puts "Benchmark MemoryPointer.new(:float, 8, false)).put_array_of_float performance, #{iter}x" + 5.times { + puts Benchmark.measure { + iter.times { + ptr = FFI::MemoryPointer.new(:float, 8, false) + ptr.put_array_of_float(0, [ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8 ]) + } } } -} -module LibTest - extend FFI::Library - ffi_lib LIBTEST_PATH - - attach_function :bench, :bench_P_v, [ :buffer_in ], :void -end - -puts "Benchmark MemoryPointer alloc+fill+call performance, #{iter}x" -5.times { - puts Benchmark.measure { - iter.times { - ptr = FFI::MemoryPointer.new(:float, 8, false) - ptr.put_array_of_float(0, [ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8 ]) - LibTest.bench(ptr) + module LibTest + extend FFI::Library + ffi_lib LIBTEST_PATH + + attach_function :bench, :bench_P_v, [ :buffer_in ], :void + end + + puts "Benchmark MemoryPointer alloc+fill+call performance, #{iter}x" + 5.times { + puts Benchmark.measure { + iter.times { + ptr = FFI::MemoryPointer.new(:float, 8, false) + ptr.put_array_of_float(0, [ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8 ]) + LibTest.bench(ptr) + } } } -} +end diff --git a/bench/bench_strlen.rb b/bench/bench_strlen.rb index 3c8a6488b..9f0264fe4 100644 --- a/bench/bench_strlen.rb +++ b/bench/bench_strlen.rb @@ -1,26 +1,28 @@ require_relative 'bench_helper' -module LibC - extend FFI::Library - ffi_lib 'c' - attach_function :strlen, [ :string ], :int -end +module BenchStrlen + module LibC + extend FFI::Library + ffi_lib 'c' + attach_function :strlen, [ :string ], :int + end -if LibC.strlen("test") != 4 - raise ArgumentError, "FFI.strlen returned incorrect value" -end + if LibC.strlen("test") != 4 + raise ArgumentError, "FFI.strlen returned incorrect value" + end -puts "Benchmark FFI api strlen(3) performance, #{ITER}x" -str = 'test' * 10 -10.times { - puts Benchmark.measure { - ITER.times { LibC.strlen(str) } + puts "Benchmark FFI api strlen(3) performance, #{ITER}x" + str = 'test' * 10 + 10.times { + puts Benchmark.measure { + ITER.times { LibC.strlen(str) } + } } -} -puts "Benchmark FFI api strlen(3), with new string performance, #{ITER}x" -10.times { - puts Benchmark.measure { - ITER.times { LibC.strlen('test' * 10) } + puts "Benchmark FFI api strlen(3), with new string performance, #{ITER}x" + 10.times { + puts Benchmark.measure { + ITER.times { LibC.strlen('test' * 10) } + } } -} +end diff --git a/bench/bench_struct.rb b/bench/bench_struct.rb index 67bde3289..2d5f7b92c 100644 --- a/bench/bench_struct.rb +++ b/bench/bench_struct.rb @@ -1,59 +1,61 @@ require_relative 'bench_helper' -module StructBench - extend FFI::Library - extend FFI::Library - ffi_lib LIBTEST_PATH - attach_function :bench_s32_v, [ :int ], :void - begin - attach_function :bench_struct_in, :ptr_ret_int32_t, [ :buffer_in, :int ], :void - rescue FFI::NotFoundError - # NetBSD uses #define instead of typedef for these - attach_function :bench_struct_in, :ptr_ret___int32_t, [ :buffer_in, :int ], :void - end - begin - attach_function :bench_struct_out, :ptr_ret_int32_t, [ :buffer_out, :int ], :void - rescue FFI::NotFoundError - # NetBSD uses #define instead of typedef for these - attach_function :bench_struct_out, :ptr_ret___int32_t, [ :buffer_out, :int ], :void +module BenchStruct + module StructBench + extend FFI::Library + extend FFI::Library + ffi_lib LIBTEST_PATH + attach_function :bench_s32_v, [ :int ], :void + begin + attach_function :bench_struct_in, :ptr_ret_int32_t, [ :buffer_in, :int ], :void + rescue FFI::NotFoundError + # NetBSD uses #define instead of typedef for these + attach_function :bench_struct_in, :ptr_ret___int32_t, [ :buffer_in, :int ], :void + end + begin + attach_function :bench_struct_out, :ptr_ret_int32_t, [ :buffer_out, :int ], :void + rescue FFI::NotFoundError + # NetBSD uses #define instead of typedef for these + attach_function :bench_struct_out, :ptr_ret___int32_t, [ :buffer_out, :int ], :void + end + begin + attach_function :bench_struct_inout, :ptr_ret_int32_t, [ :buffer_inout, :int ], :void + rescue FFI::NotFoundError + # NetBSD uses #define instead of typedef for these + attach_function :bench_struct_inout, :ptr_ret___int32_t, [ :buffer_inout, :int ], :void + end end - begin - attach_function :bench_struct_inout, :ptr_ret_int32_t, [ :buffer_inout, :int ], :void - rescue FFI::NotFoundError - # NetBSD uses #define instead of typedef for these - attach_function :bench_struct_inout, :ptr_ret___int32_t, [ :buffer_inout, :int ], :void + class TestStruct < FFI::Struct + layout :i, :int, :p, :pointer end -end -class TestStruct < FFI::Struct - layout :i, :int, :p, :pointer -end -puts "Benchmark FFI call(Struct.alloc_in) performance, #{ITER}x" -10.times { - puts Benchmark.measure { - i = 0; while i < ITER - StructBench.bench_struct_in(TestStruct.alloc_in, 0) - i += 1 - end + puts "Benchmark FFI call(Struct.alloc_in) performance, #{ITER}x" + 10.times { + puts Benchmark.measure { + 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" -10.times { - puts Benchmark.measure { - i = 0; while i < ITER - StructBench.bench_struct_out(TestStruct.alloc_out, 0) - i += 1 - end + puts "Benchmark FFI call(Struct.alloc_out) performance, #{ITER}x" + 10.times { + puts Benchmark.measure { + 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" -10.times { - puts Benchmark.measure { - i = 0; while i < ITER - StructBench.bench_struct_inout(TestStruct.alloc_inout, 0) - i += 1 - end + puts "Benchmark FFI call(Struct.alloc_inout) performance, #{ITER}x" + 10.times { + puts Benchmark.measure { + i = 0; while i < ITER + StructBench.bench_struct_inout(TestStruct.alloc_inout, 0) + i += 1 + end + } } -} +end diff --git a/bench/bench_struct_field.rb b/bench/bench_struct_field.rb index 739718c77..d428c4a6e 100644 --- a/bench/bench_struct_field.rb +++ b/bench/bench_struct_field.rb @@ -1,75 +1,77 @@ require_relative 'bench_helper' -iter = ITER +module BenchStructField + iter = ITER -class TestStruct < FFI::Struct - layout :i, :int, :p, :pointer -end + class TestStruct < FFI::Struct + layout :i, :int, :p, :pointer + end -s = TestStruct.new(FFI::MemoryPointer.new(TestStruct)) -puts "Benchmark FFI Struct.get(:int) performance, #{iter}x" -10.times { - puts Benchmark.measure { - i = 0; max = iter / 4; while i < max - s[:i] - s[:i] - s[:i] - s[:i] - i += 1 - end + s = TestStruct.new(FFI::MemoryPointer.new(TestStruct)) + puts "Benchmark FFI Struct.get(:int) performance, #{iter}x" + 10.times { + puts Benchmark.measure { + i = 0; max = iter / 4; while i < max + s[:i] + s[:i] + s[:i] + s[:i] + i += 1 + end + } } -} -puts "Benchmark FFI Struct.put(:int) performance, #{iter}x" -10.times { - puts Benchmark.measure { - i = 0; max = iter / 4; while i < max - s[:i] = 0x12345678 - s[:i] = 0x12345678 - s[:i] = 0x12345678 - s[:i] = 0x12345678 - i += 1 - end + puts "Benchmark FFI Struct.put(:int) performance, #{iter}x" + 10.times { + puts Benchmark.measure { + i = 0; max = iter / 4; while i < max + s[:i] = 0x12345678 + s[:i] = 0x12345678 + s[:i] = 0x12345678 + s[:i] = 0x12345678 + i += 1 + end + } } -} -puts "Benchmark FFI Struct.get(:pointer) performance, #{iter}x" -10.times { - puts Benchmark.measure { - i = 0; max = iter / 4; while i < max - s[:p] - s[:p] - s[:p] - s[:p] - i += 1 - end + puts "Benchmark FFI Struct.get(:pointer) performance, #{iter}x" + 10.times { + puts Benchmark.measure { + i = 0; max = iter / 4; while i < max + s[:p] + s[:p] + s[:p] + s[:p] + i += 1 + end + } } -} -puts "Benchmark FFI Struct.put(:pointer) performance, #{iter}x" -10.times { - p = FFI::MemoryPointer.new :int - puts Benchmark.measure { - i = 0; max = iter / 4; while i < max - s[:p] = p - s[:p] = p - s[:p] = p - s[:p] = p - i += 1 - end + puts "Benchmark FFI Struct.put(:pointer) performance, #{iter}x" + 10.times { + p = FFI::MemoryPointer.new :int + puts Benchmark.measure { + i = 0; max = iter / 4; while i < max + s[:p] = p + s[:p] = p + s[:p] = p + s[:p] = p + i += 1 + end + } } -} -puts "Benchmark FFI Struct.get(:string) performance, #{iter}x" -class StringStruct < FFI::Struct - layout :s, :string -end -10.times { - mp = FFI::MemoryPointer.new 1024 - mp.put_string(0, "Hello, World") - s = StringStruct.new - s.pointer.put_pointer(0, mp) - puts Benchmark.measure { - i = 0; while i < iter - s[:s] - i += 1 - end + puts "Benchmark FFI Struct.get(:string) performance, #{iter}x" + class StringStruct < FFI::Struct + layout :s, :string + end + 10.times { + mp = FFI::MemoryPointer.new 1024 + mp.put_string(0, "Hello, World") + s = StringStruct.new + s.pointer.put_pointer(0, mp) + puts Benchmark.measure { + i = 0; while i < iter + s[:s] + i += 1 + end + } } -} +end diff --git a/bench/bench_struct_size.rb b/bench/bench_struct_size.rb index bcc2c8d25..e099c6537 100644 --- a/bench/bench_struct_size.rb +++ b/bench/bench_struct_size.rb @@ -1,30 +1,31 @@ require_relative 'bench_helper' -iter = ITER +module BenchStructSize + iter = ITER -class TestStruct < FFI::Struct - layout :i, :int, :p, :pointer -end + layout = class TestStruct < FFI::Struct + layout :i, :int, :p, :pointer + end -puts "Benchmark FFI Struct class size performance, #{iter}x" -10.times { - puts Benchmark.measure { - iter.times { TestStruct.size } + puts "Benchmark FFI Struct class size performance, #{iter}x" + 10.times { + puts Benchmark.measure { + iter.times { TestStruct.size } + } } -} -s = TestStruct.new(FFI::MemoryPointer.new(TestStruct)) -puts "Benchmark FFI Struct instance size performance, #{iter}x" -10.times { - puts Benchmark.measure { - iter.times { s.size } + s = TestStruct.new(FFI::MemoryPointer.new(TestStruct)) + puts "Benchmark FFI Struct instance size performance, #{iter}x" + 10.times { + puts Benchmark.measure { + iter.times { s.size } + } } -} -puts "Benchmark FFI Struct layout size performance, #{iter}x" -layout = TestStruct.layout -10.times { - puts Benchmark.measure { - iter.times { layout.size } + puts "Benchmark FFI Struct layout size performance, #{iter}x" + 10.times { + puts Benchmark.measure { + iter.times { layout.size } + } } -} +end diff --git a/bench/bench_time.rb b/bench/bench_time.rb index 8d3aceb6a..8a06dc943 100644 --- a/bench/bench_time.rb +++ b/bench/bench_time.rb @@ -1,45 +1,47 @@ require_relative 'bench_helper' -module Posix - extend FFI::Library - ffi_lib FFI::Library::LIBC - attach_function :time, [ :buffer_out ], :ulong, :ignore_error => true -end +module BenchTime + module Posix + extend FFI::Library + ffi_lib FFI::Library::LIBC + attach_function :time, [ :buffer_out ], :ulong, :ignore_error => true + end -puts "Benchmark FFI time(3) with nil argument performance, #{ITER}x" -10.times { - puts Benchmark.measure { - ITER.times { Posix.time(nil) } + puts "Benchmark FFI time(3) with nil argument performance, #{ITER}x" + 10.times { + puts Benchmark.measure { + ITER.times { Posix.time(nil) } + } } -} -puts "Benchmark FFI time(3) with pre-allocated buffer performance, #{ITER}x" -buf = FFI::Buffer.new(:time_t) -10.times { - puts Benchmark.measure { - ITER.times { Posix.time(buf) } + puts "Benchmark FFI time(3) with pre-allocated buffer performance, #{ITER}x" + buf = FFI::Buffer.new(:time_t) + 10.times { + puts Benchmark.measure { + ITER.times { Posix.time(buf) } + } } -} -puts "Benchmark FFI time(3) with loop-allocated buffer performance, #{ITER}x" -10.times { - puts Benchmark.measure { - ITER.times { Posix.time(FFI::Buffer.new(:time_t)) } + puts "Benchmark FFI time(3) with loop-allocated buffer performance, #{ITER}x" + 10.times { + puts Benchmark.measure { + ITER.times { Posix.time(FFI::Buffer.new(:time_t)) } + } } -} -puts "Benchmark FFI time(3) with pre-allocated pointer performance, #{ITER}x" -buf = FFI::MemoryPointer.new(:time_t) -10.times { - puts Benchmark.measure { - ITER.times { Posix.time(buf) } + puts "Benchmark FFI time(3) with pre-allocated pointer performance, #{ITER}x" + buf = FFI::MemoryPointer.new(:time_t) + 10.times { + puts Benchmark.measure { + ITER.times { Posix.time(buf) } + } } -} -puts "Benchmark Time.now performance, #{ITER}x" -10.times { - puts Benchmark.measure { - ITER.times { Time.now } + puts "Benchmark Time.now performance, #{ITER}x" + 10.times { + puts Benchmark.measure { + ITER.times { Time.now } + } } -} +end diff --git a/bench/bench_umask.rb b/bench/bench_umask.rb index 2ec3fcd6b..3692c35c7 100644 --- a/bench/bench_umask.rb +++ b/bench/bench_umask.rb @@ -1,60 +1,62 @@ require_relative 'bench_helper' -module Posix - extend FFI::Library - ffi_lib 'c' - attach_function 'umask', [ :int ], :int -end -module NativeFile - extend FFI::Library - ffi_lib 'c' - # Attaching the function to this module is about 10% faster than calling Posix.umask - if FFI::Platform.windows? - attach_function :_umask, '_umask', [ :int ], :int - else - attach_function :_umask, 'umask', [ :int ], :int +module BenchUmask + module Posix + extend FFI::Library + ffi_lib 'c' + attach_function 'umask', [ :int ], :int end - def self.umask(mask = nil) - if mask - _umask(mask) + module NativeFile + extend FFI::Library + ffi_lib 'c' + # Attaching the function to this module is about 10% faster than calling Posix.umask + if FFI::Platform.windows? + attach_function :_umask, '_umask', [ :int ], :int else - old = _umask(0) - _umask(old) - old + attach_function :_umask, 'umask', [ :int ], :int + end + def self.umask(mask = nil) + if mask + _umask(mask) + else + old = _umask(0) + _umask(old) + old + end end end -end -puts "FFI umask=#{NativeFile.umask} File.umask=#{File.umask}" -puts "Benchmark File.umask(0777) performance, #{ITER}x" -10.times { - puts Benchmark.measure { - ITER.times { File.umask(0777) } + puts "FFI umask=#{NativeFile.umask} File.umask=#{File.umask}" + puts "Benchmark File.umask(0777) performance, #{ITER}x" + 10.times { + puts Benchmark.measure { + ITER.times { File.umask(0777) } + } } -} -puts "Benchmark FFI File.umask(0777) performance, #{ITER}x" + puts "Benchmark FFI File.umask(0777) performance, #{ITER}x" -10.times { - puts Benchmark.measure { - ITER.times { NativeFile.umask(0777) } + 10.times { + puts Benchmark.measure { + ITER.times { NativeFile.umask(0777) } + } } -} -puts "Benchmark FFI Posix.umask(0777) performance, #{ITER}x" + puts "Benchmark FFI Posix.umask(0777) performance, #{ITER}x" -10.times { - puts Benchmark.measure { - ITER.times { Posix.umask(0777) } + 10.times { + puts Benchmark.measure { + ITER.times { Posix.umask(0777) } + } } -} -puts "Benchmark File.umask() performance, #{ITER}x" -10.times { - puts Benchmark.measure { - ITER.times { File.umask } + puts "Benchmark File.umask() performance, #{ITER}x" + 10.times { + puts Benchmark.measure { + ITER.times { File.umask } + } } -} -puts "Benchmark FFI File.umask() performance, #{ITER}x" + puts "Benchmark FFI File.umask() performance, #{ITER}x" -10.times { - puts Benchmark.measure { - ITER.times { NativeFile.umask } + 10.times { + puts Benchmark.measure { + ITER.times { NativeFile.umask } + } } -} +end