File: bench_memptr_alloc.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 (61 lines) | stat: -rw-r--r-- 1,386 bytes parent folder | download
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
require_relative 'bench_helper'

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

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

  if defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby"
    require 'java'
    puts "calling java gc"
    10.times {
      java.lang.System.gc
      sleep 1
    }
  end
end