-
Notifications
You must be signed in to change notification settings - Fork 4
/
performance.2012.html
763 lines (757 loc) · 33.6 KB
/
performance.2012.html
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<style>
body {
margin: 0 auto;
width: 800px;
}
table {
border-collapse: collapse;
border-top: solid 1px #000000;
border-bottom: solid 1px #000000;
border-right: solid 1px #000000;
border-left: solid 1px #000000;
margin: 0 auto;
}
th {
font-weight: normal;
border-top: solid 1px #000000;
border-bottom: solid 2px #000000;
border-right: solid 1px #000000;
border-left: solid 1px #000000;
}
td {
border-top: solid 1px #CCCCCC;
border-bottom: solid 1px #CCCCCC;
border-right: solid 1px #666666;
border-left: solid 1px #666666;
}
td, th {
padding: 2px 4px;
}
pre {
background-color: #EEEEEE;
padding: 3px 2px;
</style>
<h1 id="gmp-gem-performance">GMP gem Performance</h1>
<p>Performance analysis of the GMP gem</p>
<p>28 November 2012</p>
<p></p>
<p>written by Sam Rawlins</p>
<p></p>
<p></p>
<p></p>
<h2 id="introduction-to-the-performance-benchmarks">Introduction to the performance benchmarks</h2>
<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>
<ul>
<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>
<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>
</ul>
<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>
<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>
<ul>
<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>
<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>
<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>
</ul>
<h2 id="run-the-benchmarks">Run the Benchmarks</h2>
<p>In order to run a set of benchmarks (a directory containing <code>multiply</code>, <code>runbench</code>, etc.), just use the command:</p>
<pre><code>./runbench -n</code></pre>
<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>
<p></p>
<h2 id="ruby-benchmarks">Ruby benchmarks</h2>
<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>
<h3 id="new-bignum-methods">New <code>Bignum</code> methods</h3>
<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>
<ul>
<li><code>Bignum.gcdext</code></li>
<li><code>Bignum.invert</code></li>
<li><code>Bignum.powmod</code></li>
<li><code>Bignum#[]=</code></li>
<li><code>Bignum#gcd</code></li>
</ul>
<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>
<h3 id="modifications-to-benchmarkruby-benchmarks">Modifications to <code>benchmark/ruby</code> benchmarks</h3>
<p>Ruby's <code>Bignum</code> class is not advanced enough to handle several of the benchmark test cases, namely:</p>
<ul>
<li><code>multiply 16777216 512</code> (Ruby's <code>Bignum</code> cannot raise 2 to a 16777216-bit number.)</li>
<li><code>multiply 16777216 262144</code> (Ruby's <code>Bignum</code> cannot raise 2 to a 16777216-bit number.)</li>
<li><code>divide 8388608 4194304</code> (Ruby's <code>Bignum</code> cannot raise 2 to a 8388608-bit number.)</li>
<li><code>divide 16777216 262144</code> (Ruby's <code>Bignum</code> cannot raise 2 to a 16777216-bit number.)</li>
</ul>
<p>Ruby can raise 2 to approximately 4,194,000.</p>
<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>
<p></p>
<h2 id="results">Results</h2>
<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>
<h3 id="ruby-v-ruby">Ruby v Ruby</h3>
<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>
<table>
<thead>
<tr class="header">
<th align="left">Program</th>
<th align="right">Ruby 1.8.7</th>
<th align="right">Ruby 1.9.3</th>
<th align="right">1.9.3 over 1.8.7*</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">multiply</td>
<td align="right">1.98e+03</td>
<td align="right">4.89e+03</td>
<td align="right">2.47</td>
</tr>
<tr class="even">
<td align="left">divide</td>
<td align="right">2.45e+04</td>
<td align="right">2.32e+04</td>
<td align="right">0.95</td>
</tr>
<tr class="odd">
<td align="left">gcd</td>
<td align="right">2.23e+01</td>
<td align="right">3.08e+01</td>
<td align="right">1.38</td>
</tr>
<tr class="even">
<td align="left">gcdext</td>
<td align="right">6.41e+00</td>
<td align="right">1.05e+01</td>
<td align="right">1.64</td>
</tr>
<tr class="odd">
<td align="left">[base]</td>
<td align="right">8.34e+04</td>
<td align="right">1.27e+03</td>
<td align="right">1.52</td>
</tr>
<tr class="even">
<td align="left">rsa</td>
<td align="right">1.17e+02</td>
<td align="right">1.45e+02</td>
<td align="right">1.24</td>
</tr>
<tr class="odd">
<td align="left">[app]</td>
<td align="right">1.17e+02</td>
<td align="right">1.45e+02</td>
<td align="right">1.24</td>
</tr>
<tr class="even">
<td align="left">[bench]</td>
<td align="right">3.12e+02</td>
<td align="right">4.29e+02</td>
<td align="right">1.37</td>
</tr>
</tbody>
</table>
<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>
<p>We can look at individual tests to see where 1.9.3 specifically improves over 1.8.7:</p>
<ul>
<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>
<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>
</ul>
<p></p>
<h2 id="gmp-gem-binary-operators-v-functional-operators">gmp gem: Binary Operators v Functional Operators</h2>
<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>
<table>
<thead>
<tr class="header">
<th align="left">Test Case</th>
<th align="right">Bin Op</th>
<th align="right">Functional</th>
<th align="right">Functional over Bin Op</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">multiply(128)</td>
<td align="right">9.30e+05</td>
<td align="right">4.39e+06</td>
<td align="right">4.72</td>
</tr>
<tr class="even">
<td align="left">multiply(512)</td>
<td align="right">9.19e+05</td>
<td align="right">3.10e+06</td>
<td align="right">3.37</td>
</tr>
<tr class="odd">
<td align="left">multiply(8192)</td>
<td align="right">7.93e+04</td>
<td align="right">9.24e+04</td>
<td align="right">1.17</td>
</tr>
<tr class="even">
<td align="left">multiply(131072)</td>
<td align="right">1.66e+03</td>
<td align="right">1.75e+03</td>
<td align="right">1.06</td>
</tr>
<tr class="odd">
<td align="left">multiply(2097152)</td>
<td align="right">6.24e+01</td>
<td align="right">6.20e+01</td>
<td align="right">0.99</td>
</tr>
<tr class="even">
<td align="left">multiply(128, 128)</td>
<td align="right">9.57e+05</td>
<td align="right">4.41e+06</td>
<td align="right">4.61</td>
</tr>
<tr class="odd">
<td align="left">multiply(512, 512)</td>
<td align="right">8.40e+05</td>
<td align="right">2.78e+06</td>
<td align="right">3.31</td>
</tr>
<tr class="even">
<td align="left">multiply(8192, 8192)</td>
<td align="right">5.44e+03</td>
<td align="right">5.92e+04</td>
<td align="right">1.09</td>
</tr>
<tr class="odd">
<td align="left">multiply(131072, 131072)</td>
<td align="right">1.20e+03</td>
<td align="right">1.23e+03</td>
<td align="right">1.02</td>
</tr>
<tr class="even">
<td align="left">multiply(2097152, 2097152)</td>
<td align="right">4.08e+01</td>
<td align="right">4.00e+01</td>
<td align="right">0.98</td>
</tr>
<tr class="odd">
<td align="left">multiply(15000, 10000)</td>
<td align="right">2.95e+04</td>
<td align="right">3.19e+04</td>
<td align="right">1.08</td>
</tr>
<tr class="even">
<td align="left">multiply(20000, 10000)</td>
<td align="right">2.32e+04</td>
<td align="right">2.51e+04</td>
<td align="right">1.08</td>
</tr>
<tr class="odd">
<td align="left">multiply(30000, 10000)</td>
<td align="right">1.54e+04</td>
<td align="right">1.60e+04</td>
<td align="right">1.04</td>
</tr>
</tbody>
</table>
<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>
<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>
<p></p>
<h3 id="gnu-multiple-precision-arithmetic-library-without-ruby">GNU Multiple Precision Arithmetic Library, without Ruby</h3>
<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>
<table>
<thead>
<tr class="header">
<th align="left">Program</th>
<th align="right">GMP 5.0.5</th>
<th align="right">GMP 5.0.5, reduced</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">multiply(128, 128)</td>
<td align="right">4.55e+07</td>
<td align="right">4.55e+07</td>
</tr>
<tr class="even">
<td align="left">multiply(2097152, 2097152)</td>
<td align="right">4.09e+01</td>
<td align="right">4.09e+01</td>
</tr>
<tr class="odd">
<td align="left">multiply(16777216, 262144)</td>
<td align="right">9.97e+00</td>
<td align="right">n/a</td>
</tr>
<tr class="even">
<td align="left">multiply</td>
<td align="right">2.15e+04</td>
<td align="right">5.58e+04</td>
</tr>
<tr class="odd">
<td align="left">divide(8192, 32)</td>
<td align="right">7.23e+05</td>
<td align="right">7.23e+05</td>
</tr>
<tr class="even">
<td align="left">divide(16777216, 262144)</td>
<td align="right">4.98e+00</td>
<td align="right">n/a</td>
</tr>
<tr class="odd">
<td align="left">divide</td>
<td align="right">1.93e+04</td>
<td align="right">2.77e+05</td>
</tr>
<tr class="even">
<td align="left">gcd</td>
<td align="right">3.68e+03</td>
<td align="right">3.68e+03</td>
</tr>
<tr class="odd">
<td align="left">gcdext</td>
<td align="right">2.22e+03</td>
<td align="right">2.22e+03</td>
</tr>
<tr class="even">
<td align="left">[base]</td>
<td align="right">1.06e+04</td>
<td align="right">3.53e+04</td>
</tr>
<tr class="odd">
<td align="left">rsa</td>
<td align="right">2.68e+03</td>
<td align="right">2.68e+03</td>
</tr>
<tr class="even">
<td align="left">[app]</td>
<td align="right">2.68e+03</td>
<td align="right">2.68e+03</td>
</tr>
<tr class="odd">
<td align="left">[bench]</td>
<td align="right">5.33e+03</td>
<td align="right">9.73e+03</td>
</tr>
</tbody>
</table>
<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>
<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>
<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>
<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>
<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>
<p></p>
<h3 id="gmp-v-gmp-gem-v-ruby-bignum">GMP v gmp gem v Ruby Bignum</h3>
<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>
<table>
<thead>
<tr class="header">
<th align="left">Program</th>
<th align="right">GMP</th>
<th align="right">gmp gem</th>
<th align="right">Ruby Bignum</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">multiply</td>
<td align="right">5.58e+04</td>
<td align="right">2.17e+04</td>
<td align="right">4.89e+03</td>
</tr>
<tr class="even">
<td align="left">divide</td>
<td align="right">2.77e+05</td>
<td align="right">1.46e+05</td>
<td align="right">2.32e+04</td>
</tr>
<tr class="odd">
<td align="left">gcd</td>
<td align="right">3.68e+03</td>
<td align="right">2.94e+03</td>
<td align="right">3.08e+01</td>
</tr>
<tr class="even">
<td align="left">gcdext</td>
<td align="right">2.22e+03</td>
<td align="right">1.74e+03</td>
<td align="right">1.05e+01</td>
</tr>
<tr class="odd">
<td align="left">[base]</td>
<td align="right">3.53e+04</td>
<td align="right">1.93e+04</td>
<td align="right">1.27e+03</td>
</tr>
<tr class="even">
<td align="left">rsa</td>
<td align="right">2.68e+03</td>
<td align="right">2.59e+03</td>
<td align="right">1.45e+02</td>
</tr>
<tr class="odd">
<td align="left">[app]</td>
<td align="right">2.68e+03</td>
<td align="right">2.59e+03</td>
<td align="right">1.45e+02</td>
</tr>
<tr class="even">
<td align="left">[bench]</td>
<td align="right">9.73e+03</td>
<td align="right">7.06e+03</td>
<td align="right">4.29e+02</td>
</tr>
</tbody>
</table>
<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>
<p>Here are the specifics of these tests:</p>
<ul>
<li>The pure GMP tests used GMP 5.0.5, compiled with Apple's GCC 4.2.1.</li>
<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>
<li>The Ruby Bignum tests used Ruby 1.9.3, compiled with Apple's GCC 4.2.1</li>
</ul>
<p></p>
<h3 id="gmp-gem-v-ruby-bignum">gmp gem v Ruby Bignum</h3>
<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>
<table>
<thead>
<tr class="header">
<th align="left">Test Case</th>
<th align="right">Ruby Bignum</th>
<th align="right">gmp gem</th>
<th align="right">Bignum over gmp gem</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">multiply(128)</td>
<td align="right">1.81e+06</td>
<td align="right">1.06e+06</td>
<td align="right">0.59</td>
</tr>
<tr class="even">
<td align="left">multiply(512)</td>
<td align="right">8.48e+05</td>
<td align="right">9.13e+05</td>
<td align="right">1.08</td>
</tr>
<tr class="odd">
<td align="left">multiply(2097152)</td>
<td align="right">2.85e+00</td>
<td align="right">6.24e+01</td>
<td align="right">21.95</td>
</tr>
<tr class="even">
<td align="left">multiply(128,128)</td>
<td align="right">1.73e+06</td>
<td align="right">1.07e+06</td>
<td align="right">0.62</td>
</tr>
<tr class="odd">
<td align="left">multiply(512,512)</td>
<td align="right">8.25e+05</td>
<td align="right">8.77e+05</td>
<td align="right">1.06</td>
</tr>
<tr class="even">
<td align="left">multiply(2097152,2097152)</td>
<td align="right">2.29e+00</td>
<td align="right">3.99e+01</td>
<td align="right">17.48</td>
</tr>
<tr class="odd">
<td align="left">multiply</td>
<td align="right">4.89e+03</td>
<td align="right">2.17e+04</td>
<td align="right">4.43</td>
</tr>
<tr class="even">
<td align="left">divide(8192,32)</td>
<td align="right">1.98e+05</td>
<td align="right">3.90e+05</td>
<td align="right">1.97</td>
</tr>
<tr class="odd">
<td align="left">divide(8192,4096)</td>
<td align="right">1.61e+04</td>
<td align="right">1.41e+05</td>
<td align="right">8.81</td>
</tr>
<tr class="even">
<td align="left">divide(131072,65536)</td>
<td align="right">6.60e+01</td>
<td align="right">1.62e+03</td>
<td align="right">24.47</td>
</tr>
<tr class="odd">
<td align="left">divide</td>
<td align="right">2.32e+04</td>
<td align="right">1.46e+05</td>
<td align="right">6.29</td>
</tr>
<tr class="even">
<td align="left">gcd(128,128)</td>
<td align="right">3.58e+04</td>
<td align="right">7.53e+05</td>
<td align="right">21.05</td>
</tr>
<tr class="odd">
<td align="left">gcd(8192,8192)</td>
<td align="right">6.65e+01</td>
<td align="right">5.11e+03</td>
<td align="right">76.83</td>
</tr>
<tr class="even">
<td align="left">gcd(1048576,1048576)</td>
<td align="right">8.17e-03</td>
<td align="right">4.36e+00</td>
<td align="right">532.98</td>
</tr>
<tr class="odd">
<td align="left">gcd</td>
<td align="right">3.08e+01</td>
<td align="right">2.94e+03</td>
<td align="right">95.38</td>
</tr>
<tr class="even">
<td align="left">gcdext(128,128)</td>
<td align="right">1.32e+04</td>
<td align="right">3.44e+05</td>
<td align="right">26.03</td>
</tr>
<tr class="odd">
<td align="left">gcdext(8192,8192)</td>
<td align="right">2.07e+01</td>
<td align="right">3.16e+03</td>
<td align="right">152.61</td>
</tr>
<tr class="even">
<td align="left">gcdext(1048576,1048576)</td>
<td align="right">3.23e-03</td>
<td align="right">2.83e+00</td>
<td align="right">876.51</td>
</tr>
<tr class="odd">
<td align="left">gcdext</td>
<td align="right">1.05e+01</td>
<td align="right">1.74e+03</td>
<td align="right">165.59</td>
</tr>
<tr class="even">
<td align="left">[base]</td>
<td align="right">1.27e+03</td>
<td align="right">1.93e+04</td>
<td align="right">15.20</td>
</tr>
<tr class="odd">
<td align="left">rsa(512)</td>
<td align="right">5.52e+02</td>
<td align="right">1.37e+04</td>
<td align="right">24.88</td>
</tr>
<tr class="even">
<td align="left">rsa(2048)</td>
<td align="right">3.48e+01</td>
<td align="right">4.46e+02</td>
<td align="right">12.80</td>
</tr>
<tr class="odd">
<td align="left">rsa</td>
<td align="right">1.45e+02</td>
<td align="right">2.59e+03</td>
<td align="right">17.84</td>
</tr>
<tr class="even">
<td align="left">[app]</td>
<td align="right">1.45e+02</td>
<td align="right">2.59e+03</td>
<td align="right">17.84</td>
</tr>
<tr class="odd">
<td align="left">[bench]</td>
<td align="right">4.29e+02</td>
<td align="right">7.06e+03</td>
<td align="right">16.47</td>
</tr>
</tbody>
</table>
<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>
<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>
<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>
<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>
<p></p>
<h3 id="pure-gmp-vs-gmp-gem">Pure GMP vs gmp gem</h3>
<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>
<table>
<thead>
<tr class="header">
<th align="left">Test Case</th>
<th align="right">gmp gem</th>
<th align="right">Pure GMP</th>
<th align="right">GMP over gmp gem</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">multiply(128)</td>
<td align="right">1.06e+06</td>
<td align="right">4.56e+07</td>
<td align="right">43.05</td>
</tr>
<tr class="even">
<td align="left">multiply(512)</td>
<td align="right">9.13e+05</td>
<td align="right">8.79e+06</td>
<td align="right">9.63</td>
</tr>
<tr class="odd">
<td align="left">multiply(2097152)</td>
<td align="right">6.24e+01</td>
<td align="right">6.38e+01</td>
<td align="right">1.02</td>
</tr>
<tr class="even">
<td align="left">multiply(128,128)</td>
<td align="right">1.07e+06</td>
<td align="right">4.55e+07</td>
<td align="right">42.50</td>
</tr>
<tr class="odd">
<td align="left">multiply(512,512)</td>
<td align="right">8.77e+05</td>
<td align="right">6.39e+06</td>
<td align="right">7.29</td>
</tr>
<tr class="even">
<td align="left">multiply(2097152,2097152)</td>
<td align="right">3.99e+01</td>
<td align="right">4.09e+01</td>
<td align="right">1.02</td>
</tr>
<tr class="odd">
<td align="left">multiply</td>
<td align="right">2.17e+04</td>
<td align="right">5.58e+04</td>
<td align="right">2.57</td>
</tr>
<tr class="even">
<td align="left">divide(8192,32)</td>
<td align="right">3.90e+05</td>
<td align="right">7.23e+05</td>
<td align="right">1.86</td>
</tr>
<tr class="odd">
<td align="left">divide(8192,4096)</td>
<td align="right">1.41e+05</td>
<td align="right">1.72e+05</td>
<td align="right">1.22</td>
</tr>
<tr class="even">
<td align="left">divide(131072,65536)</td>
<td align="right">1.62e+03</td>
<td align="right">1.64e+03</td>
<td align="right">1.01</td>
</tr>
<tr class="odd">
<td align="left">divide</td>
<td align="right">1.46e+05</td>
<td align="right">2.77e+05</td>
<td align="right">1.90</td>
</tr>
<tr class="even">
<td align="left">gcd(128,128)</td>
<td align="right">7.53e+05</td>
<td align="right">1.82e+06</td>
<td align="right">2.42</td>
</tr>
<tr class="odd">
<td align="left">gcd(8192,8192)</td>
<td align="right">5.11e+03</td>
<td align="right">5.16e+03</td>
<td align="right">1.01</td>
</tr>
<tr class="even">
<td align="left">gcd(1048576,1048576)</td>
<td align="right">4.36e+00</td>
<td align="right">4.37e+00</td>
<td align="right">1.00</td>
</tr>
<tr class="odd">
<td align="left">gcd</td>
<td align="right">2.94e+03</td>
<td align="right">3.68e+03</td>
<td align="right">1.25</td>
</tr>
<tr class="even">
<td align="left">gcdext(128,128)</td>
<td align="right">3.44e+05</td>
<td align="right">8.40e+05</td>
<td align="right">2.44</td>
</tr>
<tr class="odd">
<td align="left">gcdext(8192,8192)</td>
<td align="right">3.16e+03</td>
<td align="right">3.20e+03</td>
<td align="right">1.01</td>
</tr>
<tr class="even">
<td align="left">gcdext(1048576,1048576)</td>
<td align="right">2.83e+00</td>
<td align="right">2.85e+00</td>
<td align="right">1.01</td>
</tr>
<tr class="odd">
<td align="left">gcdext</td>
<td align="right">1.74e+03</td>
<td align="right">2.22e+03</td>
<td align="right">1.28</td>
</tr>
<tr class="even">
<td align="left">[base]</td>
<td align="right">1.93e+04</td>
<td align="right">3.53e+04</td>
<td align="right">1.83</td>
</tr>
<tr class="odd">
<td align="left">rsa(512)</td>
<td align="right">1.37e+04</td>
<td align="right">1.49e+04</td>
<td align="right">1.08</td>
</tr>
<tr class="even">
<td align="left">rsa(2048)</td>
<td align="right">4.46e+02</td>
<td align="right">4.48e+02</td>
<td align="right">1.00</td>
</tr>
<tr class="odd">
<td align="left">rsa</td>
<td align="right">2.59e+03</td>
<td align="right">2.68e+03</td>
<td align="right">1.04</td>
</tr>
<tr class="even">
<td align="left">[app]</td>
<td align="right">2.59e+03</td>
<td align="right">2.68e+03</td>
<td align="right">1.04</td>
</tr>
<tr class="odd">
<td align="left">[bench]</td>
<td align="right">7.06e+03</td>
<td align="right">9.73e+03</td>
<td align="right">1.38</td>
</tr>
</tbody>
</table>
<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>
<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>
<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>
<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>
<ul>
<li>Improvements in "Matz's Ruby Interpreter" may reduce the overhead.</li>
<li>A different Ruby VM, such as Rubinius and JRuby, may reduce the overhead.</li>
<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>
<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>
</ul>
<h2 id="future-plans">Future Plans</h2>
<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>
<ul>
<li>The <code>pi</code> program (benchmark test) needs to be written, in order to compare more closely the gmp gem with GMP.</li>
<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>
<li>The <code>rsa</code> results between Ruby's Bignum and gmp gem need to be understood.</li>
<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>
<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>
<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>
<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>
</ul>