summaryrefslogtreecommitdiff
path: root/bench/bench_struct_field.rb
diff options
context:
space:
mode:
Diffstat (limited to 'bench/bench_struct_field.rb')
-rw-r--r--bench/bench_struct_field.rb33
1 files changed, 26 insertions, 7 deletions
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
}
}