File: bench_PPPrV.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 (114 lines) | stat: -rw-r--r-- 3,321 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
require_relative 'bench_helper'

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


  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"

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

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

    def to_ptr
      @pointer
    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
    }
  }
end