Ruby試行005 dl2遅い
拡張ライブラリ、Win32API、DL2で速度比較
p RUBY_VERSION hwnd = 2231714 # 自分で適当に調べて # 拡張ライブラリ版 require 'extest' # 適当に作る # EXTest.ScreenToClient(hwnd, x, y) # Win32API版 require 'Win32API' module WATest ScreenToClient = Win32API.new("user32", "ScreenToClient", "IP", "I") def self.screentoclient(hwnd, x, y) r=[x, y].pack("ii") ScreenToClient.call(hwnd, r) r.unpack("ii") end end # dl2版 require 'dl/import' require 'dl/types' module DLTest extend DL::Importer dlload "user32.dll" include DL::Win32Types extern "BOOL ScreenToClient(HWND, void *)", :stdcall def self.screentoclient(hwnd, x, y) r=[x, y].pack("ii") ScreenToClient(hwnd, r) r.unpack("ii") end end require 'benchmark' n = 100000 puts Benchmark::CAPTION puts Benchmark.measure { n.times{ EXTest.ScreenToClient(hwnd, 0, 0) } } puts Benchmark.measure { n.times{ WATest.screentoclient(hwnd, 0, 0) } } puts Benchmark.measure { n.times{ DLTest.screentoclient(hwnd, 0, 0) } }↓
上から順に、拡張ライブラリ版、Win32API版、dl2版
"1.8.4" user system total real 0.235000 0.000000 0.235000 ( 0.234000) 1.672000 0.031000 1.703000 ( 1.719000) 7.796000 0.141000 7.937000 ( 7.969000)
"1.9.0" user system total real 0.219000 0.000000 0.219000 ( 0.219000) 1.672000 0.000000 1.672000 ( 1.672000) 5.719000 0.000000 5.719000 ( 5.719000)
dl2遅い
2007年01月31日(水) 04:25:48 Modified by aqualung