gmp 0.5.47 → 0.6.7
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +13 -0
- data/README.markdown +10 -31
- data/benchmark/benchmark-results-5.0.5_1.9.3_0.6.7.ods +0 -0
- data/ext/gmp.c +36 -3
- data/ext/gmpbench_timing.c +9 -9
- data/ext/gmpf.c +83 -12
- data/ext/gmprandstate.c +14 -15
- data/ext/gmpz.c +145 -30
- data/ext/mprnd.c +7 -7
- data/ext/ruby_gmp.h +2 -2
- data/lib/gmp.rb +22 -1
- data/manual.pdf +0 -0
- data/manual.tex +4 -108
- data/performance.html +763 -0
- data/performance.md +321 -0
- data/performance.pdf +0 -0
- data/test/gmp_tlcm.rb +67 -0
- data/test/gmp_tprintf.rb +124 -0
- data/test/tc_division.rb +3 -3
- data/test/tc_f_arithmetics_coersion.rb +2 -2
- data/test/tc_f_to_s.rb +72 -0
- data/test/tc_mpfr_integer.rb +20 -0
- data/test/tc_mpfr_random.rb +61 -26
- data/test/test_helper.rb +1 -1
- data/test/unit_tests.rb +3 -0
- metadata +51 -57
- data/benchmark/COPYING +0 -674
- data/benchmark/README +0 -75
- data/benchmark/divide +0 -34
- data/benchmark/gcd +0 -38
- data/benchmark/gexpr.c +0 -359
- data/benchmark/multiply +0 -44
- data/benchmark/multiply.fnl +0 -47
- data/benchmark/multiply.gc +0 -57
- data/benchmark/pi +0 -126
- data/benchmark/rsa +0 -93
- data/benchmark/runbench +0 -147
- data/benchmark/srb.sh +0 -21
- data/benchmark/version +0 -1
data/performance.html
ADDED
@@ -0,0 +1,763 @@
|
|
1
|
+
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
|
2
|
+
<style>
|
3
|
+
body {
|
4
|
+
margin: 0 auto;
|
5
|
+
width: 800px;
|
6
|
+
}
|
7
|
+
|
8
|
+
table {
|
9
|
+
border-collapse: collapse;
|
10
|
+
border-top: solid 1px #000000;
|
11
|
+
border-bottom: solid 1px #000000;
|
12
|
+
border-right: solid 1px #000000;
|
13
|
+
border-left: solid 1px #000000;
|
14
|
+
margin: 0 auto;
|
15
|
+
}
|
16
|
+
|
17
|
+
th {
|
18
|
+
font-weight: normal;
|
19
|
+
border-top: solid 1px #000000;
|
20
|
+
border-bottom: solid 2px #000000;
|
21
|
+
border-right: solid 1px #000000;
|
22
|
+
border-left: solid 1px #000000;
|
23
|
+
}
|
24
|
+
|
25
|
+
td {
|
26
|
+
border-top: solid 1px #CCCCCC;
|
27
|
+
border-bottom: solid 1px #CCCCCC;
|
28
|
+
border-right: solid 1px #666666;
|
29
|
+
border-left: solid 1px #666666;
|
30
|
+
}
|
31
|
+
|
32
|
+
td, th {
|
33
|
+
padding: 2px 4px;
|
34
|
+
}
|
35
|
+
|
36
|
+
pre {
|
37
|
+
background-color: #EEEEEE;
|
38
|
+
padding: 3px 2px;
|
39
|
+
</style>
|
40
|
+
|
41
|
+
<h1 id="gmp-gem-performance">GMP gem Performance</h1>
|
42
|
+
<p>Performance analysis of the GMP gem</p>
|
43
|
+
<p>28 November 2012</p>
|
44
|
+
<p></p>
|
45
|
+
<p>written by Sam Rawlins</p>
|
46
|
+
<p></p>
|
47
|
+
<p></p>
|
48
|
+
<p></p>
|
49
|
+
<h2 id="introduction-to-the-performance-benchmarks">Introduction to the performance benchmarks</h2>
|
50
|
+
<p>The benchmarking system used to test the performance of the gmp gem is inspired by, and uses parts of, gmpbench 0.2. http://gmplib.org/gmpbench.html. gmpbench consists of two parts:</p>
|
51
|
+
<ul>
|
52
|
+
<li><code>multiply</code>, <code>divide</code>, <code>gcd</code>, <code>gcdext</code>, <code>rsa</code>, and <code>pi</code> are 6 small programs that use GMP to measure a specific piece of functionality. <code>multiply</code>, <code>divide</code>, <code>gcd</code>, and <code>gcdext</code> are the "base" benchmarks that test small pieces of functionality. <code>rsa</code> and <code>pi</code> are the "application" benchmarks that measure the performance of a larger concept implemented with GMP.</li>
|
53
|
+
<li><code>runbench</code> is a shell script that coordinates an execution of each of the benchmarking programs, applying a weight to the results of each, and yielding a total score for GMP on the current system.</li>
|
54
|
+
</ul>
|
55
|
+
<p>The benchmarking system in the gmp gem uses Ruby versions of each of the 6 programs (actually, <code>pi</code> is still being ported), attempting to be as identical to their C code siblings. This system also just uses <code>runbench</code> unmodified from the original gmpbench suite.</p>
|
56
|
+
<p>Due to a few issues with Ruby 1.8.7, and the gmp gem itself, there are actually 3x different versions of the benchmark suite that use the gmp gem:</p>
|
57
|
+
<ul>
|
58
|
+
<li><code>benchmark/gmp/bin_op</code> uses binary operators, such as <code>*</code>, on <code>GMP::Z</code> integers. Since <code>a * b</code> creates a new <code>mpz_t</code> that it returns, the benchmark programs are constantly creating new objects, which is not what the GMP benchmark programs do. The real problem that this creates is Ruby 1.8.7 running out of memory.</li>
|
59
|
+
<li><code>benchmark/gmp/gc</code> also uses binary operators, but invokes Ruby's garbage collector every 512 iterations of each test. This allows all of the benchmarks to complete in Ruby 1.8.7, but is still not the best comparison with GMP's benchmark programs.</li>
|
60
|
+
<li><code>benchmark/gmp/functional</code> uses the "functional", <code>GMP::Z</code> singleton methods to perform what would otherwise be binary operations. For example, <code>x * y</code> is replaced with <code>GMP::Z.mul(z,x,y)</code> in order to use <code>z</code> as the "return argument" through each iteration of a benchmark. In this version, <code>z</code> is only created once, before the benchmark begins measuring time.</li>
|
61
|
+
</ul>
|
62
|
+
<h2 id="run-the-benchmarks">Run the Benchmarks</h2>
|
63
|
+
<p>In order to run a set of benchmarks (a directory containing <code>multiply</code>, <code>runbench</code>, etc.), just use the command:</p>
|
64
|
+
<pre><code>./runbench -n</code></pre>
|
65
|
+
<p>Next to each test case, program, and category, a score will be printed, which is iterations per second. For program, category, and overall scores, this represents a weighted geometric mean, and so should just be thought of more like a "score" than an actual real-world metric.</p>
|
66
|
+
<p></p>
|
67
|
+
<h2 id="ruby-benchmarks">Ruby benchmarks</h2>
|
68
|
+
<p>In addition to the above variations of the benchmark suite located in <code>benchmark/gmp</code>, there is one more variation of the benchmark suite that measure's Ruby's Bignum algorithms. This suite is located at <code>benchmark/ruby</code>.</p>
|
69
|
+
<h3 id="new-bignum-methods">New <code>Bignum</code> methods</h3>
|
70
|
+
<p>Several methods provided in <code>GMP::Z</code> are not provided in <code>Bignum</code>, in Ruby's standard library. In order to attempt a vague comparison between <code>Bignum</code> and <code>GMP::Z</code>, a simple and "fast enough" version of the following methods is provided in <code>benchmark/ruby/ruby-enhancements</code>:</p>
|
71
|
+
<ul>
|
72
|
+
<li><code>Bignum.gcdext</code></li>
|
73
|
+
<li><code>Bignum.invert</code></li>
|
74
|
+
<li><code>Bignum.powmod</code></li>
|
75
|
+
<li><code>Bignum#[]=</code></li>
|
76
|
+
<li><code>Bignum#gcd</code></li>
|
77
|
+
</ul>
|
78
|
+
<p><code>Bignum.gcdext</code>, <code>Bignum.invert</code>, and <code>Bignum.powmod</code> are all borrwed from John Nishinaga, available at <a href="https://gist.github.com/2388745"><code class="url">https://gist.github.com/2388745</code></a>.</p>
|
79
|
+
<h3 id="modifications-to-benchmarkruby-benchmarks">Modifications to <code>benchmark/ruby</code> benchmarks</h3>
|
80
|
+
<p>Ruby's <code>Bignum</code> class is not advanced enough to handle several of the benchmark test cases, namely:</p>
|
81
|
+
<ul>
|
82
|
+
<li><code>multiply 16777216 512</code> (Ruby's <code>Bignum</code> cannot raise 2 to a 16777216-bit number.)</li>
|
83
|
+
<li><code>multiply 16777216 262144</code> (Ruby's <code>Bignum</code> cannot raise 2 to a 16777216-bit number.)</li>
|
84
|
+
<li><code>divide 8388608 4194304</code> (Ruby's <code>Bignum</code> cannot raise 2 to a 8388608-bit number.)</li>
|
85
|
+
<li><code>divide 16777216 262144</code> (Ruby's <code>Bignum</code> cannot raise 2 to a 16777216-bit number.)</li>
|
86
|
+
</ul>
|
87
|
+
<p>Ruby can raise 2 to approximately 4,194,000.</p>
|
88
|
+
<p>In the <code>benchmark/ruby</code> suite, these have been removed, so that summary scores can still be produced. In order to compare these summary scores against <code>GMP::Z</code> benchmarks, there is also a <code>benchmark/gmp/reduced</code> suite that uses the same test cases. <code>benchmark/gmp/reduced</code> is the only test suite that should be compared against <code>benchmark/ruby</code> (or, with some work, one can manually calculate the weighted geometric means, using the same method found in <code>runbench</code>.</p>
|
89
|
+
<p></p>
|
90
|
+
<h2 id="results">Results</h2>
|
91
|
+
<p>Raw benchmark results can be found in <code>benchmark/benchmark-results-5.0.5_1.9.3_0.6.7.ods</code>, a LibreOffice spreadsheet. Below I show some interpreted results.</p>
|
92
|
+
<h3 id="ruby-v-ruby">Ruby v Ruby</h3>
|
93
|
+
<p>I benchmarked three different versions of Ruby's Bignum implementation: Ruby 1.8.7, Ruby 1.9.3, and Ruby 2.0.0-preview2 (the latest version of Ruby 2.0 at the time of the tests). These tests only measured Ruby's Bignum, and do not use GMP at all. Ruby 1.9.3 and Ruby 2.0.0-preview2 performed very similarly, within 5% of each other in most cases. The interesting result in this test is Ruby 1.8.7 v Ruby 1.9.3. With the exception of <code>divide</code>, 1.9.3 outperformed 1.8.7, and often dramatically:</p>
|
94
|
+
<table>
|
95
|
+
<thead>
|
96
|
+
<tr class="header">
|
97
|
+
<th align="left">Program</th>
|
98
|
+
<th align="right">Ruby 1.8.7</th>
|
99
|
+
<th align="right">Ruby 1.9.3</th>
|
100
|
+
<th align="right">1.9.3 over 1.8.7*</th>
|
101
|
+
</tr>
|
102
|
+
</thead>
|
103
|
+
<tbody>
|
104
|
+
<tr class="odd">
|
105
|
+
<td align="left">multiply</td>
|
106
|
+
<td align="right">1.98e+03</td>
|
107
|
+
<td align="right">4.89e+03</td>
|
108
|
+
<td align="right">2.47</td>
|
109
|
+
</tr>
|
110
|
+
<tr class="even">
|
111
|
+
<td align="left">divide</td>
|
112
|
+
<td align="right">2.45e+04</td>
|
113
|
+
<td align="right">2.32e+04</td>
|
114
|
+
<td align="right">0.95</td>
|
115
|
+
</tr>
|
116
|
+
<tr class="odd">
|
117
|
+
<td align="left">gcd</td>
|
118
|
+
<td align="right">2.23e+01</td>
|
119
|
+
<td align="right">3.08e+01</td>
|
120
|
+
<td align="right">1.38</td>
|
121
|
+
</tr>
|
122
|
+
<tr class="even">
|
123
|
+
<td align="left">gcdext</td>
|
124
|
+
<td align="right">6.41e+00</td>
|
125
|
+
<td align="right">1.05e+01</td>
|
126
|
+
<td align="right">1.64</td>
|
127
|
+
</tr>
|
128
|
+
<tr class="odd">
|
129
|
+
<td align="left">[base]</td>
|
130
|
+
<td align="right">8.34e+04</td>
|
131
|
+
<td align="right">1.27e+03</td>
|
132
|
+
<td align="right">1.52</td>
|
133
|
+
</tr>
|
134
|
+
<tr class="even">
|
135
|
+
<td align="left">rsa</td>
|
136
|
+
<td align="right">1.17e+02</td>
|
137
|
+
<td align="right">1.45e+02</td>
|
138
|
+
<td align="right">1.24</td>
|
139
|
+
</tr>
|
140
|
+
<tr class="odd">
|
141
|
+
<td align="left">[app]</td>
|
142
|
+
<td align="right">1.17e+02</td>
|
143
|
+
<td align="right">1.45e+02</td>
|
144
|
+
<td align="right">1.24</td>
|
145
|
+
</tr>
|
146
|
+
<tr class="even">
|
147
|
+
<td align="left">[bench]</td>
|
148
|
+
<td align="right">3.12e+02</td>
|
149
|
+
<td align="right">4.29e+02</td>
|
150
|
+
<td align="right">1.37</td>
|
151
|
+
</tr>
|
152
|
+
</tbody>
|
153
|
+
</table>
|
154
|
+
<p>* Calculated as \(\frac{1.9.3 \text{ score}}{1.8.7\text{ score}}\) so that 2.47 means "2.47 times as fast" or equivalently "1.47 times faster."</p>
|
155
|
+
<p>We can look at individual tests to see where 1.9.3 specifically improves over 1.8.7:</p>
|
156
|
+
<ul>
|
157
|
+
<li>Firstly, in the <code>multiply</code> test, 1.9.3 and 1.8.7 are actually neck-and-neck for most of the tests, until we get to multiplying "very large" numbers together. Multiplying a 131072-bit number by a 131072-bit number is ~5 times as fast in 1.9.3 vs 1.8.7. Multiplying two 2,097,152-bit numbers together is 22x as fast!</li>
|
158
|
+
<li>Second, the reverse phenomenon happens with <code>gcd</code> and <code>gcdext</code>, where 1.9.3 outperforms 1.8.7 at 3.4x and 5.1x, respectively, when using 128-bit inputs. With 512-bit inputs and above, however, the speedup fades to nothing. This suggests that the algorithms used in <code>Bignum</code> do not change, but the overhead costs are lower in Ruby 1.9.3. One can understand that when GCDing smaller numbers, the overhead of looping, making method calls, etc. is a larger percentage of the work being done, but when GCDing larger numbers, the overhead dissolves into almost nothing.</li>
|
159
|
+
</ul>
|
160
|
+
<p></p>
|
161
|
+
<h2 id="gmp-gem-binary-operators-v-functional-operators">gmp gem: Binary Operators v Functional Operators</h2>
|
162
|
+
<p>It is beneficial to look at the two different forms of methods sometimes offered: binary operators (such as <code>GMP::Z#+</code> which is used like <code>c = a + b</code>) and "functional" operators (such as <code>GMP::Z.add</code> which is used like <code>GMP::Z.add(c, a, b)</code>). At this time, only the <code>GMP::Z#*</code> binary operator is available as a functional operator (<code>GMP::Z.multiply</code>), which can change gears to a squaring algorithm if it detects that the operands are equal. (Squaring is thus faster than multiplication.) We can look at those results below:</p>
|
163
|
+
<table>
|
164
|
+
<thead>
|
165
|
+
<tr class="header">
|
166
|
+
<th align="left">Test Case</th>
|
167
|
+
<th align="right">Bin Op</th>
|
168
|
+
<th align="right">Functional</th>
|
169
|
+
<th align="right">Functional over Bin Op</th>
|
170
|
+
</tr>
|
171
|
+
</thead>
|
172
|
+
<tbody>
|
173
|
+
<tr class="odd">
|
174
|
+
<td align="left">multiply(128)</td>
|
175
|
+
<td align="right">9.30e+05</td>
|
176
|
+
<td align="right">4.39e+06</td>
|
177
|
+
<td align="right">4.72</td>
|
178
|
+
</tr>
|
179
|
+
<tr class="even">
|
180
|
+
<td align="left">multiply(512)</td>
|
181
|
+
<td align="right">9.19e+05</td>
|
182
|
+
<td align="right">3.10e+06</td>
|
183
|
+
<td align="right">3.37</td>
|
184
|
+
</tr>
|
185
|
+
<tr class="odd">
|
186
|
+
<td align="left">multiply(8192)</td>
|
187
|
+
<td align="right">7.93e+04</td>
|
188
|
+
<td align="right">9.24e+04</td>
|
189
|
+
<td align="right">1.17</td>
|
190
|
+
</tr>
|
191
|
+
<tr class="even">
|
192
|
+
<td align="left">multiply(131072)</td>
|
193
|
+
<td align="right">1.66e+03</td>
|
194
|
+
<td align="right">1.75e+03</td>
|
195
|
+
<td align="right">1.06</td>
|
196
|
+
</tr>
|
197
|
+
<tr class="odd">
|
198
|
+
<td align="left">multiply(2097152)</td>
|
199
|
+
<td align="right">6.24e+01</td>
|
200
|
+
<td align="right">6.20e+01</td>
|
201
|
+
<td align="right">0.99</td>
|
202
|
+
</tr>
|
203
|
+
<tr class="even">
|
204
|
+
<td align="left">multiply(128, 128)</td>
|
205
|
+
<td align="right">9.57e+05</td>
|
206
|
+
<td align="right">4.41e+06</td>
|
207
|
+
<td align="right">4.61</td>
|
208
|
+
</tr>
|
209
|
+
<tr class="odd">
|
210
|
+
<td align="left">multiply(512, 512)</td>
|
211
|
+
<td align="right">8.40e+05</td>
|
212
|
+
<td align="right">2.78e+06</td>
|
213
|
+
<td align="right">3.31</td>
|
214
|
+
</tr>
|
215
|
+
<tr class="even">
|
216
|
+
<td align="left">multiply(8192, 8192)</td>
|
217
|
+
<td align="right">5.44e+03</td>
|
218
|
+
<td align="right">5.92e+04</td>
|
219
|
+
<td align="right">1.09</td>
|
220
|
+
</tr>
|
221
|
+
<tr class="odd">
|
222
|
+
<td align="left">multiply(131072, 131072)</td>
|
223
|
+
<td align="right">1.20e+03</td>
|
224
|
+
<td align="right">1.23e+03</td>
|
225
|
+
<td align="right">1.02</td>
|
226
|
+
</tr>
|
227
|
+
<tr class="even">
|
228
|
+
<td align="left">multiply(2097152, 2097152)</td>
|
229
|
+
<td align="right">4.08e+01</td>
|
230
|
+
<td align="right">4.00e+01</td>
|
231
|
+
<td align="right">0.98</td>
|
232
|
+
</tr>
|
233
|
+
<tr class="odd">
|
234
|
+
<td align="left">multiply(15000, 10000)</td>
|
235
|
+
<td align="right">2.95e+04</td>
|
236
|
+
<td align="right">3.19e+04</td>
|
237
|
+
<td align="right">1.08</td>
|
238
|
+
</tr>
|
239
|
+
<tr class="even">
|
240
|
+
<td align="left">multiply(20000, 10000)</td>
|
241
|
+
<td align="right">2.32e+04</td>
|
242
|
+
<td align="right">2.51e+04</td>
|
243
|
+
<td align="right">1.08</td>
|
244
|
+
</tr>
|
245
|
+
<tr class="odd">
|
246
|
+
<td align="left">multiply(30000, 10000)</td>
|
247
|
+
<td align="right">1.54e+04</td>
|
248
|
+
<td align="right">1.60e+04</td>
|
249
|
+
<td align="right">1.04</td>
|
250
|
+
</tr>
|
251
|
+
</tbody>
|
252
|
+
</table>
|
253
|
+
<p>We can see the effects of allocating new <code>GMP::Z</code> objects every iteration of the benchmark loop. When we are squaring or multiplying "small," 128-bit or 512-bit numbers, allocating objects and garbage collection can slow down the computation by three- or four-fold, if the computation is multiplying numbers (using <code>GMP::Z#*</code>) in a tight loop.</p>
|
254
|
+
<p>Once we get to squaring (or multiplying) 8192-bit numbers, however, the time spent inside GMP becomes great enough, that garbage collection and object allocation fades into the background. Above this size, binary operators can be only 17% slower. When squaring 131072-bit numbers, or multiplying 10000-bit numbers, binary operators are 8%, or less, slower.</p>
|
255
|
+
<p></p>
|
256
|
+
<h3 id="gnu-multiple-precision-arithmetic-library-without-ruby">GNU Multiple Precision Arithmetic Library, without Ruby</h3>
|
257
|
+
<p>Here I present some raw benchmark results of GMP 5.0.5, using the original gmpbench 0.2 software. These tests do not involve the Ruby interpreter in any way.</p>
|
258
|
+
<table>
|
259
|
+
<thead>
|
260
|
+
<tr class="header">
|
261
|
+
<th align="left">Program</th>
|
262
|
+
<th align="right">GMP 5.0.5</th>
|
263
|
+
<th align="right">GMP 5.0.5, reduced</th>
|
264
|
+
</tr>
|
265
|
+
</thead>
|
266
|
+
<tbody>
|
267
|
+
<tr class="odd">
|
268
|
+
<td align="left">multiply(128, 128)</td>
|
269
|
+
<td align="right">4.55e+07</td>
|
270
|
+
<td align="right">4.55e+07</td>
|
271
|
+
</tr>
|
272
|
+
<tr class="even">
|
273
|
+
<td align="left">multiply(2097152, 2097152)</td>
|
274
|
+
<td align="right">4.09e+01</td>
|
275
|
+
<td align="right">4.09e+01</td>
|
276
|
+
</tr>
|
277
|
+
<tr class="odd">
|
278
|
+
<td align="left">multiply(16777216, 262144)</td>
|
279
|
+
<td align="right">9.97e+00</td>
|
280
|
+
<td align="right">n/a</td>
|
281
|
+
</tr>
|
282
|
+
<tr class="even">
|
283
|
+
<td align="left">multiply</td>
|
284
|
+
<td align="right">2.15e+04</td>
|
285
|
+
<td align="right">5.58e+04</td>
|
286
|
+
</tr>
|
287
|
+
<tr class="odd">
|
288
|
+
<td align="left">divide(8192, 32)</td>
|
289
|
+
<td align="right">7.23e+05</td>
|
290
|
+
<td align="right">7.23e+05</td>
|
291
|
+
</tr>
|
292
|
+
<tr class="even">
|
293
|
+
<td align="left">divide(16777216, 262144)</td>
|
294
|
+
<td align="right">4.98e+00</td>
|
295
|
+
<td align="right">n/a</td>
|
296
|
+
</tr>
|
297
|
+
<tr class="odd">
|
298
|
+
<td align="left">divide</td>
|
299
|
+
<td align="right">1.93e+04</td>
|
300
|
+
<td align="right">2.77e+05</td>
|
301
|
+
</tr>
|
302
|
+
<tr class="even">
|
303
|
+
<td align="left">gcd</td>
|
304
|
+
<td align="right">3.68e+03</td>
|
305
|
+
<td align="right">3.68e+03</td>
|
306
|
+
</tr>
|
307
|
+
<tr class="odd">
|
308
|
+
<td align="left">gcdext</td>
|
309
|
+
<td align="right">2.22e+03</td>
|
310
|
+
<td align="right">2.22e+03</td>
|
311
|
+
</tr>
|
312
|
+
<tr class="even">
|
313
|
+
<td align="left">[base]</td>
|
314
|
+
<td align="right">1.06e+04</td>
|
315
|
+
<td align="right">3.53e+04</td>
|
316
|
+
</tr>
|
317
|
+
<tr class="odd">
|
318
|
+
<td align="left">rsa</td>
|
319
|
+
<td align="right">2.68e+03</td>
|
320
|
+
<td align="right">2.68e+03</td>
|
321
|
+
</tr>
|
322
|
+
<tr class="even">
|
323
|
+
<td align="left">[app]</td>
|
324
|
+
<td align="right">2.68e+03</td>
|
325
|
+
<td align="right">2.68e+03</td>
|
326
|
+
</tr>
|
327
|
+
<tr class="odd">
|
328
|
+
<td align="left">[bench]</td>
|
329
|
+
<td align="right">5.33e+03</td>
|
330
|
+
<td align="right">9.73e+03</td>
|
331
|
+
</tr>
|
332
|
+
</tbody>
|
333
|
+
</table>
|
334
|
+
<p>In both columns of results, the <code>pi</code> results have not been presented, as they cannot be compared to anything in Ruby, yet. In the second column, we also reduce the test by not including the <code>multiply</code> and <code>divide</code> tests that Ruby's Bignum algorithms cannot handle.</p>
|
335
|
+
<p>These results have been included to primarily show the results of two tests that Ruby's Bignum is unable to compute: <code>multiply(16777216, 262144)</code> and <code>divide(16777216, 262144)</code>. Whereas GMP can multiply two 128-bit numbers together more than 45 million times per second, and even two 2097152-bit numbers more than 40 times per second, it can only multiply a 16777216-bit and a 262144-bit number about 10 times per second.</p>
|
336
|
+
<p>At the same time, pure GMP works hard to divide one huge number by another: it can divide an 8192-bit by a 32-bit number more than 700,000 times per second, but only divide a 16777216-bit by a 262144-bit number about 5 times per second.</p>
|
337
|
+
<p>One can also get a grasp of how why the <em>geometric</em> mean is important when computing the scores for, say, the <code>multiply</code> or the <code>divide</code> program. Removing the two slowest test cases from the <code>multiply</code> set raises the geometric mean from about 21,500 to about 55,800 multiplications per second. An arithmetic mean would produce scores that might be difficult to compare side-by-side.</p>
|
338
|
+
<p>Ultimately, the reduced test cases change the overall benchmark score from about 5000 to about 10000. This shows why, ultimately, none of the test scores here should be compared with scores from the original, full gmpbench 0.2 suite. All of the scores analyzed in this document can only be used to compare <em>some</em> other scores also analyzed in this document.</p>
|
339
|
+
<p></p>
|
340
|
+
<h3 id="gmp-v-gmp-gem-v-ruby-bignum">GMP v gmp gem v Ruby Bignum</h3>
|
341
|
+
<p>Now that we have all of the required reduced test results, and the known limitations of Ruby's Bignum and the gmp gem's binary operators, we can do a proper comparison between raw GMP, the gmp gem, and Ruby's Bignum. First, a table with some summarized results, and no direct comparisons:</p>
|
342
|
+
<table>
|
343
|
+
<thead>
|
344
|
+
<tr class="header">
|
345
|
+
<th align="left">Program</th>
|
346
|
+
<th align="right">GMP</th>
|
347
|
+
<th align="right">gmp gem</th>
|
348
|
+
<th align="right">Ruby Bignum</th>
|
349
|
+
</tr>
|
350
|
+
</thead>
|
351
|
+
<tbody>
|
352
|
+
<tr class="odd">
|
353
|
+
<td align="left">multiply</td>
|
354
|
+
<td align="right">5.58e+04</td>
|
355
|
+
<td align="right">2.17e+04</td>
|
356
|
+
<td align="right">4.89e+03</td>
|
357
|
+
</tr>
|
358
|
+
<tr class="even">
|
359
|
+
<td align="left">divide</td>
|
360
|
+
<td align="right">2.77e+05</td>
|
361
|
+
<td align="right">1.46e+05</td>
|
362
|
+
<td align="right">2.32e+04</td>
|
363
|
+
</tr>
|
364
|
+
<tr class="odd">
|
365
|
+
<td align="left">gcd</td>
|
366
|
+
<td align="right">3.68e+03</td>
|
367
|
+
<td align="right">2.94e+03</td>
|
368
|
+
<td align="right">3.08e+01</td>
|
369
|
+
</tr>
|
370
|
+
<tr class="even">
|
371
|
+
<td align="left">gcdext</td>
|
372
|
+
<td align="right">2.22e+03</td>
|
373
|
+
<td align="right">1.74e+03</td>
|
374
|
+
<td align="right">1.05e+01</td>
|
375
|
+
</tr>
|
376
|
+
<tr class="odd">
|
377
|
+
<td align="left">[base]</td>
|
378
|
+
<td align="right">3.53e+04</td>
|
379
|
+
<td align="right">1.93e+04</td>
|
380
|
+
<td align="right">1.27e+03</td>
|
381
|
+
</tr>
|
382
|
+
<tr class="even">
|
383
|
+
<td align="left">rsa</td>
|
384
|
+
<td align="right">2.68e+03</td>
|
385
|
+
<td align="right">2.59e+03</td>
|
386
|
+
<td align="right">1.45e+02</td>
|
387
|
+
</tr>
|
388
|
+
<tr class="odd">
|
389
|
+
<td align="left">[app]</td>
|
390
|
+
<td align="right">2.68e+03</td>
|
391
|
+
<td align="right">2.59e+03</td>
|
392
|
+
<td align="right">1.45e+02</td>
|
393
|
+
</tr>
|
394
|
+
<tr class="even">
|
395
|
+
<td align="left">[bench]</td>
|
396
|
+
<td align="right">9.73e+03</td>
|
397
|
+
<td align="right">7.06e+03</td>
|
398
|
+
<td align="right">4.29e+02</td>
|
399
|
+
</tr>
|
400
|
+
</tbody>
|
401
|
+
</table>
|
402
|
+
<p>At a glance, it looks like GMP is consistently faster than the gmp gem, but they are on the same order of magnitude. We can also see that the gmp gem is consistently faster than Ruby's Bignum, by one or two orders of magnitude.</p>
|
403
|
+
<p>Here are the specifics of these tests:</p>
|
404
|
+
<ul>
|
405
|
+
<li>The pure GMP tests used GMP 5.0.5, compiled with Apple's GCC 4.2.1.</li>
|
406
|
+
<li>The gmp gem tests used the master branch of the gmp gem (roughly equivalent to gmp gem version 0.6.7), compiled against GMP 5.0.5, on Ruby 1.9.3, compiled with Apple's GCC 4.2.1.</li>
|
407
|
+
<li>The Ruby Bignum tests used Ruby 1.9.3, compiled with Apple's GCC 4.2.1</li>
|
408
|
+
</ul>
|
409
|
+
<p></p>
|
410
|
+
<h3 id="gmp-gem-v-ruby-bignum">gmp gem v Ruby Bignum</h3>
|
411
|
+
<p>Perhaps the most useful results to come out of the benchmark testing are the comparisons between Ruby's Bignum and the gmp gem. These results show the possible performance gains when refactoring Ruby code to use the gmp gem:</p>
|
412
|
+
<table>
|
413
|
+
<thead>
|
414
|
+
<tr class="header">
|
415
|
+
<th align="left">Test Case</th>
|
416
|
+
<th align="right">Ruby Bignum</th>
|
417
|
+
<th align="right">gmp gem</th>
|
418
|
+
<th align="right">Bignum over gmp gem</th>
|
419
|
+
</tr>
|
420
|
+
</thead>
|
421
|
+
<tbody>
|
422
|
+
<tr class="odd">
|
423
|
+
<td align="left">multiply(128)</td>
|
424
|
+
<td align="right">1.81e+06</td>
|
425
|
+
<td align="right">1.06e+06</td>
|
426
|
+
<td align="right">0.59</td>
|
427
|
+
</tr>
|
428
|
+
<tr class="even">
|
429
|
+
<td align="left">multiply(512)</td>
|
430
|
+
<td align="right">8.48e+05</td>
|
431
|
+
<td align="right">9.13e+05</td>
|
432
|
+
<td align="right">1.08</td>
|
433
|
+
</tr>
|
434
|
+
<tr class="odd">
|
435
|
+
<td align="left">multiply(2097152)</td>
|
436
|
+
<td align="right">2.85e+00</td>
|
437
|
+
<td align="right">6.24e+01</td>
|
438
|
+
<td align="right">21.95</td>
|
439
|
+
</tr>
|
440
|
+
<tr class="even">
|
441
|
+
<td align="left">multiply(128,128)</td>
|
442
|
+
<td align="right">1.73e+06</td>
|
443
|
+
<td align="right">1.07e+06</td>
|
444
|
+
<td align="right">0.62</td>
|
445
|
+
</tr>
|
446
|
+
<tr class="odd">
|
447
|
+
<td align="left">multiply(512,512)</td>
|
448
|
+
<td align="right">8.25e+05</td>
|
449
|
+
<td align="right">8.77e+05</td>
|
450
|
+
<td align="right">1.06</td>
|
451
|
+
</tr>
|
452
|
+
<tr class="even">
|
453
|
+
<td align="left">multiply(2097152,2097152)</td>
|
454
|
+
<td align="right">2.29e+00</td>
|
455
|
+
<td align="right">3.99e+01</td>
|
456
|
+
<td align="right">17.48</td>
|
457
|
+
</tr>
|
458
|
+
<tr class="odd">
|
459
|
+
<td align="left">multiply</td>
|
460
|
+
<td align="right">4.89e+03</td>
|
461
|
+
<td align="right">2.17e+04</td>
|
462
|
+
<td align="right">4.43</td>
|
463
|
+
</tr>
|
464
|
+
<tr class="even">
|
465
|
+
<td align="left">divide(8192,32)</td>
|
466
|
+
<td align="right">1.98e+05</td>
|
467
|
+
<td align="right">3.90e+05</td>
|
468
|
+
<td align="right">1.97</td>
|
469
|
+
</tr>
|
470
|
+
<tr class="odd">
|
471
|
+
<td align="left">divide(8192,4096)</td>
|
472
|
+
<td align="right">1.61e+04</td>
|
473
|
+
<td align="right">1.41e+05</td>
|
474
|
+
<td align="right">8.81</td>
|
475
|
+
</tr>
|
476
|
+
<tr class="even">
|
477
|
+
<td align="left">divide(131072,65536)</td>
|
478
|
+
<td align="right">6.60e+01</td>
|
479
|
+
<td align="right">1.62e+03</td>
|
480
|
+
<td align="right">24.47</td>
|
481
|
+
</tr>
|
482
|
+
<tr class="odd">
|
483
|
+
<td align="left">divide</td>
|
484
|
+
<td align="right">2.32e+04</td>
|
485
|
+
<td align="right">1.46e+05</td>
|
486
|
+
<td align="right">6.29</td>
|
487
|
+
</tr>
|
488
|
+
<tr class="even">
|
489
|
+
<td align="left">gcd(128,128)</td>
|
490
|
+
<td align="right">3.58e+04</td>
|
491
|
+
<td align="right">7.53e+05</td>
|
492
|
+
<td align="right">21.05</td>
|
493
|
+
</tr>
|
494
|
+
<tr class="odd">
|
495
|
+
<td align="left">gcd(8192,8192)</td>
|
496
|
+
<td align="right">6.65e+01</td>
|
497
|
+
<td align="right">5.11e+03</td>
|
498
|
+
<td align="right">76.83</td>
|
499
|
+
</tr>
|
500
|
+
<tr class="even">
|
501
|
+
<td align="left">gcd(1048576,1048576)</td>
|
502
|
+
<td align="right">8.17e-03</td>
|
503
|
+
<td align="right">4.36e+00</td>
|
504
|
+
<td align="right">532.98</td>
|
505
|
+
</tr>
|
506
|
+
<tr class="odd">
|
507
|
+
<td align="left">gcd</td>
|
508
|
+
<td align="right">3.08e+01</td>
|
509
|
+
<td align="right">2.94e+03</td>
|
510
|
+
<td align="right">95.38</td>
|
511
|
+
</tr>
|
512
|
+
<tr class="even">
|
513
|
+
<td align="left">gcdext(128,128)</td>
|
514
|
+
<td align="right">1.32e+04</td>
|
515
|
+
<td align="right">3.44e+05</td>
|
516
|
+
<td align="right">26.03</td>
|
517
|
+
</tr>
|
518
|
+
<tr class="odd">
|
519
|
+
<td align="left">gcdext(8192,8192)</td>
|
520
|
+
<td align="right">2.07e+01</td>
|
521
|
+
<td align="right">3.16e+03</td>
|
522
|
+
<td align="right">152.61</td>
|
523
|
+
</tr>
|
524
|
+
<tr class="even">
|
525
|
+
<td align="left">gcdext(1048576,1048576)</td>
|
526
|
+
<td align="right">3.23e-03</td>
|
527
|
+
<td align="right">2.83e+00</td>
|
528
|
+
<td align="right">876.51</td>
|
529
|
+
</tr>
|
530
|
+
<tr class="odd">
|
531
|
+
<td align="left">gcdext</td>
|
532
|
+
<td align="right">1.05e+01</td>
|
533
|
+
<td align="right">1.74e+03</td>
|
534
|
+
<td align="right">165.59</td>
|
535
|
+
</tr>
|
536
|
+
<tr class="even">
|
537
|
+
<td align="left">[base]</td>
|
538
|
+
<td align="right">1.27e+03</td>
|
539
|
+
<td align="right">1.93e+04</td>
|
540
|
+
<td align="right">15.20</td>
|
541
|
+
</tr>
|
542
|
+
<tr class="odd">
|
543
|
+
<td align="left">rsa(512)</td>
|
544
|
+
<td align="right">5.52e+02</td>
|
545
|
+
<td align="right">1.37e+04</td>
|
546
|
+
<td align="right">24.88</td>
|
547
|
+
</tr>
|
548
|
+
<tr class="even">
|
549
|
+
<td align="left">rsa(2048)</td>
|
550
|
+
<td align="right">3.48e+01</td>
|
551
|
+
<td align="right">4.46e+02</td>
|
552
|
+
<td align="right">12.80</td>
|
553
|
+
</tr>
|
554
|
+
<tr class="odd">
|
555
|
+
<td align="left">rsa</td>
|
556
|
+
<td align="right">1.45e+02</td>
|
557
|
+
<td align="right">2.59e+03</td>
|
558
|
+
<td align="right">17.84</td>
|
559
|
+
</tr>
|
560
|
+
<tr class="even">
|
561
|
+
<td align="left">[app]</td>
|
562
|
+
<td align="right">1.45e+02</td>
|
563
|
+
<td align="right">2.59e+03</td>
|
564
|
+
<td align="right">17.84</td>
|
565
|
+
</tr>
|
566
|
+
<tr class="odd">
|
567
|
+
<td align="left">[bench]</td>
|
568
|
+
<td align="right">4.29e+02</td>
|
569
|
+
<td align="right">7.06e+03</td>
|
570
|
+
<td align="right">16.47</td>
|
571
|
+
</tr>
|
572
|
+
</tbody>
|
573
|
+
</table>
|
574
|
+
<p>Let's analyze the multiplication results first. We can see that below a threshold of squaring (or multiplying together) 512-bit numbers, Ruby's Bignum implementation is actually faster than using the gmp gem. Beyond this threshold however, the gmp gem gets continually faster. The greatest improvement measured in multiplication is the case of squaring a 2097152-bit number, where the gmp gem is approximately 22 times as fast as Ruby's Bignum. The (geometric) average improvement is 4.43x. The reason for an improved speedup with larger numbers is of course attributable to asymptotically faster algorithms used in GMP.</p>
|
575
|
+
<p>The division results show much the same thing. When dividing an 8192-bit by a 32-bit number, the gmp outperforms Ruby's Bignum at abouttwice as fast. Beyond that, the gmp grows to be up to 25 times as fast as Ruby's Bignum. This growing gap is again attributable to asymptotically faster algorithms in GMP.</p>
|
576
|
+
<p>The GCD and GCD Extended cases show that the gmp gem is dramatically faster than Ruby's Bignum. However, this test is not actually benchmarking any GCD algorithms written into the Bignum C extension; it is using the GCD algorithms that were written in Ruby, for the benchmark tests. It is likely that a faster algorithm could be implemented in Ruby in a few hours, or that a faster implementation could be written in C, as a Bignum C extension function, in a few dozen hours. This is something that should be examined in the future.</p>
|
577
|
+
<p>The RSA test results show the reverse phenomenon as all of the previous results: The gmp gem is an order of magnitude faster than pure Ruby in every test, but the Ruby Bignum implementation appears to be <em>catching up</em> to the gmp gem. This is currently not understood.</p>
|
578
|
+
<p></p>
|
579
|
+
<h3 id="pure-gmp-vs-gmp-gem">Pure GMP vs gmp gem</h3>
|
580
|
+
<p>The other question that scientific computation experts will want answered is this: what is the cost of refactoring a GMP application into Ruby and the gmp gem? Let's compare those two:</p>
|
581
|
+
<table>
|
582
|
+
<thead>
|
583
|
+
<tr class="header">
|
584
|
+
<th align="left">Test Case</th>
|
585
|
+
<th align="right">gmp gem</th>
|
586
|
+
<th align="right">Pure GMP</th>
|
587
|
+
<th align="right">GMP over gmp gem</th>
|
588
|
+
</tr>
|
589
|
+
</thead>
|
590
|
+
<tbody>
|
591
|
+
<tr class="odd">
|
592
|
+
<td align="left">multiply(128)</td>
|
593
|
+
<td align="right">1.06e+06</td>
|
594
|
+
<td align="right">4.56e+07</td>
|
595
|
+
<td align="right">43.05</td>
|
596
|
+
</tr>
|
597
|
+
<tr class="even">
|
598
|
+
<td align="left">multiply(512)</td>
|
599
|
+
<td align="right">9.13e+05</td>
|
600
|
+
<td align="right">8.79e+06</td>
|
601
|
+
<td align="right">9.63</td>
|
602
|
+
</tr>
|
603
|
+
<tr class="odd">
|
604
|
+
<td align="left">multiply(2097152)</td>
|
605
|
+
<td align="right">6.24e+01</td>
|
606
|
+
<td align="right">6.38e+01</td>
|
607
|
+
<td align="right">1.02</td>
|
608
|
+
</tr>
|
609
|
+
<tr class="even">
|
610
|
+
<td align="left">multiply(128,128)</td>
|
611
|
+
<td align="right">1.07e+06</td>
|
612
|
+
<td align="right">4.55e+07</td>
|
613
|
+
<td align="right">42.50</td>
|
614
|
+
</tr>
|
615
|
+
<tr class="odd">
|
616
|
+
<td align="left">multiply(512,512)</td>
|
617
|
+
<td align="right">8.77e+05</td>
|
618
|
+
<td align="right">6.39e+06</td>
|
619
|
+
<td align="right">7.29</td>
|
620
|
+
</tr>
|
621
|
+
<tr class="even">
|
622
|
+
<td align="left">multiply(2097152,2097152)</td>
|
623
|
+
<td align="right">3.99e+01</td>
|
624
|
+
<td align="right">4.09e+01</td>
|
625
|
+
<td align="right">1.02</td>
|
626
|
+
</tr>
|
627
|
+
<tr class="odd">
|
628
|
+
<td align="left">multiply</td>
|
629
|
+
<td align="right">2.17e+04</td>
|
630
|
+
<td align="right">5.58e+04</td>
|
631
|
+
<td align="right">2.57</td>
|
632
|
+
</tr>
|
633
|
+
<tr class="even">
|
634
|
+
<td align="left">divide(8192,32)</td>
|
635
|
+
<td align="right">3.90e+05</td>
|
636
|
+
<td align="right">7.23e+05</td>
|
637
|
+
<td align="right">1.86</td>
|
638
|
+
</tr>
|
639
|
+
<tr class="odd">
|
640
|
+
<td align="left">divide(8192,4096)</td>
|
641
|
+
<td align="right">1.41e+05</td>
|
642
|
+
<td align="right">1.72e+05</td>
|
643
|
+
<td align="right">1.22</td>
|
644
|
+
</tr>
|
645
|
+
<tr class="even">
|
646
|
+
<td align="left">divide(131072,65536)</td>
|
647
|
+
<td align="right">1.62e+03</td>
|
648
|
+
<td align="right">1.64e+03</td>
|
649
|
+
<td align="right">1.01</td>
|
650
|
+
</tr>
|
651
|
+
<tr class="odd">
|
652
|
+
<td align="left">divide</td>
|
653
|
+
<td align="right">1.46e+05</td>
|
654
|
+
<td align="right">2.77e+05</td>
|
655
|
+
<td align="right">1.90</td>
|
656
|
+
</tr>
|
657
|
+
<tr class="even">
|
658
|
+
<td align="left">gcd(128,128)</td>
|
659
|
+
<td align="right">7.53e+05</td>
|
660
|
+
<td align="right">1.82e+06</td>
|
661
|
+
<td align="right">2.42</td>
|
662
|
+
</tr>
|
663
|
+
<tr class="odd">
|
664
|
+
<td align="left">gcd(8192,8192)</td>
|
665
|
+
<td align="right">5.11e+03</td>
|
666
|
+
<td align="right">5.16e+03</td>
|
667
|
+
<td align="right">1.01</td>
|
668
|
+
</tr>
|
669
|
+
<tr class="even">
|
670
|
+
<td align="left">gcd(1048576,1048576)</td>
|
671
|
+
<td align="right">4.36e+00</td>
|
672
|
+
<td align="right">4.37e+00</td>
|
673
|
+
<td align="right">1.00</td>
|
674
|
+
</tr>
|
675
|
+
<tr class="odd">
|
676
|
+
<td align="left">gcd</td>
|
677
|
+
<td align="right">2.94e+03</td>
|
678
|
+
<td align="right">3.68e+03</td>
|
679
|
+
<td align="right">1.25</td>
|
680
|
+
</tr>
|
681
|
+
<tr class="even">
|
682
|
+
<td align="left">gcdext(128,128)</td>
|
683
|
+
<td align="right">3.44e+05</td>
|
684
|
+
<td align="right">8.40e+05</td>
|
685
|
+
<td align="right">2.44</td>
|
686
|
+
</tr>
|
687
|
+
<tr class="odd">
|
688
|
+
<td align="left">gcdext(8192,8192)</td>
|
689
|
+
<td align="right">3.16e+03</td>
|
690
|
+
<td align="right">3.20e+03</td>
|
691
|
+
<td align="right">1.01</td>
|
692
|
+
</tr>
|
693
|
+
<tr class="even">
|
694
|
+
<td align="left">gcdext(1048576,1048576)</td>
|
695
|
+
<td align="right">2.83e+00</td>
|
696
|
+
<td align="right">2.85e+00</td>
|
697
|
+
<td align="right">1.01</td>
|
698
|
+
</tr>
|
699
|
+
<tr class="odd">
|
700
|
+
<td align="left">gcdext</td>
|
701
|
+
<td align="right">1.74e+03</td>
|
702
|
+
<td align="right">2.22e+03</td>
|
703
|
+
<td align="right">1.28</td>
|
704
|
+
</tr>
|
705
|
+
<tr class="even">
|
706
|
+
<td align="left">[base]</td>
|
707
|
+
<td align="right">1.93e+04</td>
|
708
|
+
<td align="right">3.53e+04</td>
|
709
|
+
<td align="right">1.83</td>
|
710
|
+
</tr>
|
711
|
+
<tr class="odd">
|
712
|
+
<td align="left">rsa(512)</td>
|
713
|
+
<td align="right">1.37e+04</td>
|
714
|
+
<td align="right">1.49e+04</td>
|
715
|
+
<td align="right">1.08</td>
|
716
|
+
</tr>
|
717
|
+
<tr class="even">
|
718
|
+
<td align="left">rsa(2048)</td>
|
719
|
+
<td align="right">4.46e+02</td>
|
720
|
+
<td align="right">4.48e+02</td>
|
721
|
+
<td align="right">1.00</td>
|
722
|
+
</tr>
|
723
|
+
<tr class="odd">
|
724
|
+
<td align="left">rsa</td>
|
725
|
+
<td align="right">2.59e+03</td>
|
726
|
+
<td align="right">2.68e+03</td>
|
727
|
+
<td align="right">1.04</td>
|
728
|
+
</tr>
|
729
|
+
<tr class="even">
|
730
|
+
<td align="left">[app]</td>
|
731
|
+
<td align="right">2.59e+03</td>
|
732
|
+
<td align="right">2.68e+03</td>
|
733
|
+
<td align="right">1.04</td>
|
734
|
+
</tr>
|
735
|
+
<tr class="odd">
|
736
|
+
<td align="left">[bench]</td>
|
737
|
+
<td align="right">7.06e+03</td>
|
738
|
+
<td align="right">9.73e+03</td>
|
739
|
+
<td align="right">1.38</td>
|
740
|
+
</tr>
|
741
|
+
</tbody>
|
742
|
+
</table>
|
743
|
+
<p>These results are all very exciting for potential users of the gmp gem. All of the tests show the same trend: as the operand size grows, the performance of the gmp gem gets asymptotically closer to the GMP library itself. For example, when multiplying two 128-bit numbers together, the GMP library by itself is more than 40 times as fast as the gmp gem, but this gap shrinks to just 7x when multiplying two 512-bit numbers, and shrinks all the way to 1.02x when multiplying two 2097152-bit numbers.</p>
|
744
|
+
<p>None of the other programs start off with such a gap between GMP performance and gmp gem performance as the multiplication tests. For example in calculating the GCD between two 128-bit numbers, GMP itself is only 2.4 times as fast as the gmp gem.</p>
|
745
|
+
<p>These can all be easily explained as Ruby overhead. The Ruby VM and the Ruby garbage collector and all of the dynamic calls are responsible for the gap between GMP and the gmp gem. As the operands get larger, and more CPU time is spent inside the GMP algorithms, the overhead shrinks to almost nothing.</p>
|
746
|
+
<p>The goal, of course, in future releases of the gmp gem, is to shrink that gap even more. Even though the gap when multiplying two 2097152-bit numbers is negligible, that does not help the developer who is multiplying two 128-bit numbers. Theoretically, improvements in a number of different arenas can help shrink the gap:</p>
|
747
|
+
<ul>
|
748
|
+
<li>Improvements in "Matz's Ruby Interpreter" may reduce the overhead.</li>
|
749
|
+
<li>A different Ruby VM, such as Rubinius and JRuby, may reduce the overhead.</li>
|
750
|
+
<li>Compiling Ruby and the gmp gem with an improved C compiler (such as a "modern" GCC, as opposed to GCC 4.2.1, or LLVM) may reduce the overhead.</li>
|
751
|
+
<li>Coding improvements in the gmp gem may reduce the overhead. This could include reordered type-checking, and complete bindings for functional forms of theGMP methods.</li>
|
752
|
+
</ul>
|
753
|
+
<h2 id="future-plans">Future Plans</h2>
|
754
|
+
<p>There is a lot of work to be done in comparing pure GMP, the gmp gem, and Ruby's Bignum. These plans are not listed in any particular order:</p>
|
755
|
+
<ul>
|
756
|
+
<li>The <code>pi</code> program (benchmark test) needs to be written, in order to compare more closely the gmp gem with GMP.</li>
|
757
|
+
<li>Various Bignum methods need to be written more seriously, namely <code>gcd</code> and <code>gcdext</code>. These can use faster alrogithms, but still exist as Ruby code (see <a href="http://gmplib.org/manual/Greatest-Common-Divisor-Algorithms.html"><code class="url">http://gmplib.org/manual/Greatest-Common-Divisor-Algorithms.html</code></a>), or be reimplemented as Ruby C extensions. Also, <code>Bignum#[]=</code> should probably be reimplemented as a C extension. All of these would be candidates to contribute back to Ruby Core.</li>
|
758
|
+
<li>The <code>rsa</code> results between Ruby's Bignum and gmp gem need to be understood.</li>
|
759
|
+
<li>All of the tests represented in this report used software compiled with Apple's GCC 4.2.1, which is notoriously a bad choice to compile GMP with. Smoke tests should be conducted against a more modern GCC, such as GCC 4.6.x or GCC 4.7.x. Alternatively, LLVM should compile GMP and Ruby without much difficulty these days.</li>
|
760
|
+
<li>There are new releases of both Ruby (2.0.0) and GMP (5.1.0) on the horizon. As previews, betas, and release candidates are made available, some benchmarking should be performed.</li>
|
761
|
+
<li>Most (all?) alternative Ruby VMs in the wild today support Ruby C Extensions. These include: JRuby 1.6+, Rubinius 1.1+, MacRuby 0.7+, and MagLev. JRuby and Rubinius, at a minimum, have the real possibility of outperforming MRI, with their different garbage collectors and JIT compilers.</li>
|
762
|
+
<li>The results listed in this report were all conducted on Mac OS X 10.6.8. While they should certainly translate <em>roughly</em> to other platforms, an effort should be made to test the gmp gem on other platforms. I don't expect any surprises on BSD or Linux, but coupling GMP, Ruby, and Windows together yield something different. Additionally, I think that testing GMP and Ruby on ARM (on Android, for example) sounds incredibly fun.</li>
|
763
|
+
</ul>
|