-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibgo_example_3.rb
42 lines (35 loc) · 1022 Bytes
/
libgo_example_3.rb
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
require 'ffi'
require 'benchmark'
require 'http_accept_language/parser'
module LibGo
class GoString < FFI::Struct
layout :p, :pointer,
:n, :int
end
extend FFI::Library
ffi_lib './libgo_example_3.so'
attach_function :preferredLanguageFrom, [GoString.by_ref], GoString.by_ref
attach_function :preferredLanguageFromUseCString, [:string], :string
end
@lang = "en;q=0.9,zh;q=0.8,en-US;q=0.6,en-GB;q=0.2"
@availables = %w(en ja zh-CN zh-TW )
def use_go
x = LibGo::GoString.new
x[:p] = FFI::MemoryPointer.from_string(@lang)
x[:n] = @lang.length
result = LibGo.preferredLanguageFrom( x )
result[:p].get_string(0, result[:n])
end
def use_go_cstring
LibGo.preferredLanguageFromUseCString( @lang )
end
def use_ruby
parser = HttpAcceptLanguage::Parser.new(@lang)
parser.preferred_language_from( @availables )
end
n=100000
Benchmark.bmbm do |x|
x.report("Ruby"){ n.times{ use_ruby() }}
x.report("GoString"){ n.times{ use_go() }}
x.report("CString"){ n.times{ use_go_cstring() }}
end