bin_utils 0.0.4-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ module BinUtils
2
+ VERSION = "0.0.4"
3
+ end
data/lib/bin_utils.rb ADDED
@@ -0,0 +1,19 @@
1
+ require "bin_utils/version"
2
+
3
+ module BinUtils
4
+ begin
5
+ if RUBY_ENGINE == 'jruby'
6
+ require 'bin_utils/bin_utils.jar'
7
+ else
8
+ require 'bin_utils/bin_utils'
9
+ end
10
+ extend ::BinUtils::Native
11
+ rescue LoadError
12
+ raise
13
+ require 'bin_utils/pure_ruby'
14
+ extend PureRuby
15
+ if RUBY_ENGINE == 'ruby' || RUBY_ENGINE == 'jruby'
16
+ $stderr.puts "Attention: pure ruby version of BinUtils is used"
17
+ end
18
+ end
19
+ end
data/test/benchmark.rb ADDED
@@ -0,0 +1,176 @@
1
+ require 'benchmark'
2
+ require 'bin_utils'
3
+
4
+ N = (ARGV[0] || 1_000_000).to_i
5
+ a = 'asdfzxcvqwerasdfzxcvqwer1234'.force_encoding('binary').freeze
6
+
7
+ EMPTY = ''.freeze
8
+ _V = 'V'.freeze
9
+ _Vv = 'Vv'.freeze
10
+ _Vvw = 'Vvw'.freeze
11
+ _VvwQ = 'VvwQ'.freeze
12
+ _VV = 'VV'.freeze
13
+ _VVv = 'VVv'.freeze
14
+ _vVV = 'vVV'.freeze
15
+ _VVn = 'VVn'.freeze
16
+ _nVw = 'nVw'.freeze
17
+ _w = 'w*'.freeze
18
+
19
+ Benchmark.bmbm(6) do |x|
20
+ x.report('get i32le') { N.times{|_|
21
+ i = ::BinUtils.get_int32_le(a)
22
+ } }
23
+ x.report('get i32le i32le') { N.times{|_|
24
+ i = ::BinUtils.get_int32_le(a); i2 = ::BinUtils.get_int32_le(a, 4)
25
+ } }
26
+ x.report('get i32le i32le i16be') { N.times{|_|
27
+ i = ::BinUtils.get_int32_le(a); i2 = ::BinUtils.get_int32_le(a, 4)
28
+ i3 = ::BinUtils.get_int16_be(a, 9)
29
+ } }
30
+ x.report('get i16be i32le ber') { N.times{|_|
31
+ i = ::BinUtils.get_int16_be(a); i2 = ::BinUtils.get_int32_le(a, 2)
32
+ i3 = ::BinUtils.get_ber(a, 6)
33
+ } }
34
+ x.report('unpack i32le') { N.times{|_| i, = a.unpack(_V)} }
35
+ x.report('unpack i32le`') { N.times{|_| i = a.unpack(_V)[0]} }
36
+ x.report('unpack i32le i32le') { N.times{|_| i, i2 = a.unpack(_VV)} }
37
+ x.report('unpack i32le i32le i16be') { N.times{|_| i, i2, i3 = a.unpack(_VVn)} }
38
+ x.report('unpack i16be i32le ber') { N.times{|_| i, i2, i3 = a.unpack(_nVw)} }
39
+ x.report('slice i32le') { N.times{|_|
40
+ b = a.dup
41
+ i = ::BinUtils.slice_int32_le!(b)
42
+ } }
43
+ x.report('slice i32le i16le') { N.times{|_|
44
+ b = a.dup
45
+ i = ::BinUtils.slice_int32_le!(b)
46
+ i2 = ::BinUtils.slice_int16_le!(b)
47
+ } }
48
+ x.report('slice i32le i16le ber') { N.times{|_|
49
+ b = a.dup
50
+ i = ::BinUtils.slice_int32_le!(b)
51
+ i2 = ::BinUtils.slice_int16_le!(b)
52
+ i3 = ::BinUtils.slice_ber!(b)
53
+ } }
54
+ x.report('slice i32le i16le ber i64le') { N.times{|_|
55
+ b = a.dup
56
+ i = ::BinUtils.slice_int32_le!(b)
57
+ i2 = ::BinUtils.slice_int16_le!(b)
58
+ i3 = ::BinUtils.slice_ber!(b)
59
+ i4 = ::BinUtils.slice_int64_le!(b)
60
+ } }
61
+ x.report('cut unpack i32le') { N.times{|_|
62
+ b = a.dup
63
+ i, = b.unpack(_V)
64
+ b[0, 4] = EMPTY
65
+ } }
66
+ x.report('cut unpack i32le i16le') { N.times{|_|
67
+ b = a.dup
68
+ i, i2 = b.unpack(_Vv)
69
+ b[0, 6] = EMPTY
70
+ } }
71
+ x.report('cut unpack i32le i16le') { N.times{|_|
72
+ b = a.dup
73
+ i, i2 = b.unpack(_Vv)
74
+ b.replace b[6..-1]
75
+ } }
76
+ x.report('cut unpack i32le i16le ber') { N.times{|_|
77
+ b = a.dup
78
+ i, i2, i3 = b.unpack(_Vvw)
79
+ if i3 < 128
80
+ b[0, 7] = EMPTY
81
+ else
82
+ b[0, 8] = EMPTY # simplify ber size only to two cases
83
+ end
84
+ } }
85
+ x.report('cut unpack i32le i16le ber i64le') { N.times{|_|
86
+ b = a.dup
87
+ i, i2, i3, i4 = b.unpack(_VvwQ)
88
+ if i3 < 128
89
+ b[0, 15] = EMPTY
90
+ else
91
+ b[0, 16] = EMPTY # simplify ber size only to two cases
92
+ end
93
+ } }
94
+ x.report('create i32le') { N.times{|i|
95
+ b = ::BinUtils.append_int32_le!(nil, i)
96
+ } }
97
+ x.report('create i32le i32le') { N.times{|i|
98
+ b = ::BinUtils.append_int32_le!(nil, i, i+1)
99
+ } }
100
+ x.report('create i32le i32le i16le') { N.times{|i|
101
+ b = ::BinUtils.append_int32_le!(nil, i, i+1)
102
+ ::BinUtils.append_int16_le!(b, i+2)
103
+ } }
104
+ x.report('create i16le i32le i32le') { N.times{|i|
105
+ b = ::BinUtils.append_int16_int32_le!(nil, i, i+1, i+2)
106
+ } }
107
+ x.report('create i16be i32le ber') { N.times{|i|
108
+ b = ::BinUtils.append_int16_be!(nil, i)
109
+ ::BinUtils.append_int32_le!(b, i)
110
+ ::BinUtils.append_ber!(b, i+2)
111
+ } }
112
+ x.report('append i32le') { N.times{|i|
113
+ b = a.dup
114
+ ::BinUtils.append_int32_le!(b, i)
115
+ } }
116
+ x.report('append i32le i32le') { N.times{|i|
117
+ b = a.dup
118
+ ::BinUtils.append_int32_le!(b, i, i+1)
119
+ } }
120
+ x.report('append i32le i32le i16le') { N.times{|i|
121
+ b = a.dup
122
+ ::BinUtils.append_int32_le!(b, i, i+1)
123
+ ::BinUtils.append_int16_le!(b, i+2)
124
+ } }
125
+ x.report('append i16le i32le i32le') { N.times{|i|
126
+ b = a.dup
127
+ ::BinUtils.append_int16_int32_le!(b, i, i+1, i+2)
128
+ } }
129
+ x.report('append i16be i32le ber') { N.times{|i|
130
+ b = a.dup
131
+ ::BinUtils.append_int16_be!(b, i)
132
+ ::BinUtils.append_int32_le!(b, i)
133
+ ::BinUtils.append_ber!(b, i+2)
134
+ } }
135
+ x.report('pack i32le') { N.times{|i|
136
+ b = [i].pack(_V)
137
+ } }
138
+ x.report('pack i32le i32le') { N.times{|i|
139
+ b = [i, i+1].pack(_VV)
140
+ } }
141
+ x.report('pack i32le i32le i16le') { N.times{|i|
142
+ b = [i, i+1, i+2].pack(_VVv)
143
+ } }
144
+ x.report('pack i16le i32le i32le') { N.times{|i|
145
+ b = [i, i+1, i+2].pack(_vVV)
146
+ } }
147
+ x.report('pack i16be i32le ber') { N.times{|i|
148
+ b = [i, i+1, i+2].pack(_nVw)
149
+ } }
150
+ x.report('pack append i32le') { N.times{|i|
151
+ b = a.dup
152
+ b << [i].pack(_V)
153
+ } }
154
+ x.report('pack append i32le i32le') { N.times{|i|
155
+ b = a.dup
156
+ b << [i, i+1].pack(_VV)
157
+ } }
158
+ x.report('pack append i32le i32le i16le') { N.times{|i|
159
+ b = a.dup
160
+ b << [i, i+1, i+2].pack(_VVv)
161
+ } }
162
+ x.report('pack append i16le i32le i32le') { N.times{|i|
163
+ b = a.dup
164
+ b << [i, i+1, i+2].pack(_vVV)
165
+ } }
166
+ x.report('pack append i16be i32le ber') { N.times{|i|
167
+ b = a.dup
168
+ b << [i, i+1, i+2].pack(_nVw)
169
+ } }
170
+ x.report('create ber array') { arr = [1,10,100,1000,10000,100000,1000000]; N.times {|_|
171
+ ::BinUtils.append_ber!(nil, arr)
172
+ } }
173
+ x.report('pack ber array') { arr = [1,10,100,1000,10000,100000,1000000]; N.times {|_|
174
+ arr.pack(_w)
175
+ } }
176
+ end