File: bench_gettimeofday.rb

package info (click to toggle)
ruby-ffi 1.0.11debian-5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,488 kB
  • sloc: ansic: 6,608; ruby: 6,167; xml: 151; sh: 74; makefile: 12
file content (45 lines) | stat: -rw-r--r-- 1,235 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
require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper"))

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

iter = ITER
puts "Benchmark FFI gettimeofday(2) (nil, nil) performance, #{iter}x"

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.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) (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 }
  }
}