File: bench_struct_field.rb

package info (click to toggle)
ruby-ffi 1.12.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,652 kB
  • sloc: ruby: 8,078; ansic: 7,157; xml: 151; sh: 51; makefile: 14
file content (77 lines) | stat: -rw-r--r-- 1,652 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
require_relative 'bench_helper'

module BenchStructField
  iter = ITER

  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
    }
  }

  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.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
    }
  }
end