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
|
require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper"))
module Posix
extend FFI::Library
ffi_lib 'c'
attach_function 'umask', [ :int ], :int
end
module NativeFile
extend FFI::Library
ffi_lib 'c'
# Attaching the function to this module is about 10% faster than calling Posix.umask
if FFI::Platform.windows?
attach_function :_umask, '_umask', [ :int ], :int
else
attach_function :_umask, 'umask', [ :int ], :int
end
def self.umask(mask = nil)
if mask
_umask(mask)
else
old = _umask(0)
_umask(old)
old
end
end
end
puts "FFI umask=#{NativeFile.umask} File.umask=#{File.umask}"
puts "Benchmark File.umask(0777) performance, #{ITER}x"
10.times {
puts Benchmark.measure {
ITER.times { File.umask(0777) }
}
}
puts "Benchmark FFI File.umask(0777) performance, #{ITER}x"
10.times {
puts Benchmark.measure {
ITER.times { NativeFile.umask(0777) }
}
}
puts "Benchmark FFI Posix.umask(0777) performance, #{ITER}x"
10.times {
puts Benchmark.measure {
ITER.times { Posix.umask(0777) }
}
}
puts "Benchmark File.umask() performance, #{ITER}x"
10.times {
puts Benchmark.measure {
ITER.times { File.umask }
}
}
puts "Benchmark FFI File.umask() performance, #{ITER}x"
10.times {
puts Benchmark.measure {
ITER.times { NativeFile.umask }
}
}
|