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/performance.md ADDED
@@ -0,0 +1,321 @@
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
+ # GMP gem Performance
42
+
43
+ Performance analysis of the GMP gem
44
+
45
+ 28 November 2012
46
+
47
+ \vfill
48
+
49
+ written by Sam Rawlins
50
+
51
+ \newpage
52
+
53
+ \tableofcontents
54
+
55
+ \newpage
56
+
57
+ ## Introduction to the performance benchmarks
58
+
59
+ 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:
60
+
61
+ * `multiply`, `divide`, `gcd`, `gcdext`, `rsa`, and `pi` are 6 small programs that use GMP to measure a specific piece of functionality. `multiply`, `divide`, `gcd`, and `gcdext` are the "base" benchmarks that test small pieces of functionality. `rsa` and `pi` are the "application" benchmarks that measure the performance of a larger concept implemented with GMP.
62
+ * `runbench` 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.
63
+
64
+ The benchmarking system in the gmp gem uses Ruby versions of each of the 6 programs (actually, `pi` is still being ported), attempting to be as identical to their C code siblings. This system also just uses `runbench` unmodified from the original gmpbench suite.
65
+
66
+ 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:
67
+
68
+ * `benchmark/gmp/bin_op` uses binary operators, such as `*`, on `GMP::Z` integers. Since `a * b` creates a new `mpz_t` 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.
69
+ * `benchmark/gmp/gc` 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.
70
+ * `benchmark/gmp/functional` uses the "functional", `GMP::Z` singleton methods to perform what would otherwise be binary operations. For example, `x * y` is replaced with `GMP::Z.mul(z,x,y)` in order to use `z` as the "return argument" through each iteration of a benchmark. In this version, `z` is only created once, before the benchmark begins measuring time.
71
+
72
+ ## Run the Benchmarks
73
+
74
+ In order to run a set of benchmarks (a directory containing `multiply`, `runbench`, etc.), just use the command:
75
+
76
+ ./runbench -n
77
+
78
+ 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.
79
+
80
+ \newpage
81
+
82
+ ## Ruby benchmarks
83
+
84
+ In addition to the above variations of the benchmark suite located in `benchmark/gmp`, there is one more variation of the benchmark suite that measure's Ruby's Bignum algorithms. This suite is located at `benchmark/ruby`.
85
+
86
+ ### New `Bignum` methods
87
+
88
+ Several methods provided in `GMP::Z` are not provided in `Bignum`, in Ruby's standard library. In order to attempt a vague comparison between `Bignum` and `GMP::Z`, a simple and "fast enough" version of the following methods is provided in `benchmark/ruby/ruby-enhancements`:
89
+
90
+ * `Bignum.gcdext`
91
+ * `Bignum.invert`
92
+ * `Bignum.powmod`
93
+ * `Bignum#[]=`
94
+ * `Bignum#gcd`
95
+
96
+ `Bignum.gcdext`, `Bignum.invert`, and `Bignum.powmod` are all borrwed from John Nishinaga, available at <https://gist.github.com/2388745>.
97
+
98
+ ### Modifications to `benchmark/ruby` benchmarks
99
+
100
+ Ruby's `Bignum` class is not advanced enough to handle several of the benchmark test cases, namely:
101
+
102
+ * `multiply 16777216 512` (Ruby's `Bignum` cannot raise 2 to a 16777216-bit number.)
103
+ * `multiply 16777216 262144` (Ruby's `Bignum` cannot raise 2 to a 16777216-bit number.)
104
+ * `divide 8388608 4194304` (Ruby's `Bignum` cannot raise 2 to a 8388608-bit number.)
105
+ * `divide 16777216 262144` (Ruby's `Bignum` cannot raise 2 to a 16777216-bit number.)
106
+
107
+ Ruby can raise 2 to approximately 4,194,000.
108
+
109
+ In the `benchmark/ruby` suite, these have been removed, so that summary scores can still be produced. In order to compare these summary scores against `GMP::Z` benchmarks, there is also a `benchmark/gmp/reduced` suite that uses the same test cases. `benchmark/gmp/reduced` is the only test suite that should be compared against `benchmark/ruby` (or, with some work, one can manually calculate the weighted geometric means, using the same method found in `runbench`.
110
+
111
+ \newpage
112
+
113
+ ## Results
114
+
115
+ Raw benchmark results can be found in `benchmark/benchmark-results-5.0.5_1.9.3_0.6.7.ods`, a LibreOffice spreadsheet. Below I show some interpreted results.
116
+
117
+ ### Ruby v Ruby
118
+
119
+ 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 `divide`, 1.9.3 outperformed 1.8.7, and often dramatically:
120
+
121
+ Program Ruby 1.8.7 Ruby 1.9.3 1.9.3 over 1.8.7*
122
+ --------- ------------ ------------ -------------------
123
+ multiply 1.98e+03 4.89e+03 2.47
124
+ divide 2.45e+04 2.32e+04 0.95
125
+ gcd 2.23e+01 3.08e+01 1.38
126
+ gcdext 6.41e+00 1.05e+01 1.64
127
+ [base] 8.34e+04 1.27e+03 1.52
128
+ rsa 1.17e+02 1.45e+02 1.24
129
+ [app] 1.17e+02 1.45e+02 1.24
130
+ [bench] 3.12e+02 4.29e+02 1.37
131
+
132
+ \* 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."
133
+
134
+ We can look at individual tests to see where 1.9.3 specifically improves over 1.8.7:
135
+
136
+ * Firstly, in the `multiply` 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!
137
+ * Second, the reverse phenomenon happens with `gcd` and `gcdext`, 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 `Bignum` 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.
138
+
139
+ \newpage
140
+
141
+ ## gmp gem: Binary Operators v Functional Operators
142
+
143
+ It is beneficial to look at the two different forms of methods sometimes offered: binary operators (such as `GMP::Z#+` which is used like `c = a + b`) and "functional" operators (such as `GMP::Z.add` which is used like `GMP::Z.add(c, a, b)`). At this time, only the `GMP::Z#*` binary operator is available as a functional operator (`GMP::Z.multiply`), 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:
144
+
145
+ Test Case Bin Op Functional Functional over Bin Op
146
+ ----------- ---------- ------------ ------------------------
147
+ multiply(128) 9.30e+05 4.39e+06 4.72
148
+ multiply(512) 9.19e+05 3.10e+06 3.37
149
+ multiply(8192) 7.93e+04 9.24e+04 1.17
150
+ multiply(131072) 1.66e+03 1.75e+03 1.06
151
+ multiply(2097152) 6.24e+01 6.20e+01 0.99
152
+ multiply(128, 128) 9.57e+05 4.41e+06 4.61
153
+ multiply(512, 512) 8.40e+05 2.78e+06 3.31
154
+ multiply(8192, 8192) 5.44e+03 5.92e+04 1.09
155
+ multiply(131072, 131072) 1.20e+03 1.23e+03 1.02
156
+ multiply(2097152, 2097152) 4.08e+01 4.00e+01 0.98
157
+ multiply(15000, 10000) 2.95e+04 3.19e+04 1.08
158
+ multiply(20000, 10000) 2.32e+04 2.51e+04 1.08
159
+ multiply(30000, 10000) 1.54e+04 1.60e+04 1.04
160
+
161
+ We can see the effects of allocating new `GMP::Z` 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 `GMP::Z#*`) in a tight loop.
162
+
163
+ 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.
164
+
165
+ \newpage
166
+
167
+ ### GNU Multiple Precision Arithmetic Library, without Ruby
168
+
169
+ 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.
170
+
171
+ Program GMP 5.0.5 GMP 5.0.5, reduced
172
+ -------------------------- ----------- --------------------
173
+ multiply(128, 128) 4.55e+07 4.55e+07
174
+ multiply(2097152, 2097152) 4.09e+01 4.09e+01
175
+ multiply(16777216, 262144) 9.97e+00 n/a
176
+ multiply 2.15e+04 5.58e+04
177
+ divide(8192, 32) 7.23e+05 7.23e+05
178
+ divide(16777216, 262144) 4.98e+00 n/a
179
+ divide 1.93e+04 2.77e+05
180
+ gcd 3.68e+03 3.68e+03
181
+ gcdext 2.22e+03 2.22e+03
182
+ [base] 1.06e+04 3.53e+04
183
+ rsa 2.68e+03 2.68e+03
184
+ [app] 2.68e+03 2.68e+03
185
+ [bench] 5.33e+03 9.73e+03
186
+
187
+ In both columns of results, the `pi` 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 `multiply` and `divide` tests that Ruby's Bignum algorithms cannot handle.
188
+
189
+ These results have been included to primarily show the results of two tests that Ruby's Bignum is unable to compute: `multiply(16777216, 262144)` and `divide(16777216, 262144)`. 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.
190
+
191
+ 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.
192
+
193
+ One can also get a grasp of how why the _geometric_ mean is important when computing the scores for, say, the `multiply` or the `divide` program. Removing the two slowest test cases from the `multiply` 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.
194
+
195
+ 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 _some_ other scores also analyzed in this document.
196
+
197
+ \newpage
198
+
199
+ ### GMP v gmp gem v Ruby Bignum
200
+
201
+ 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:
202
+
203
+ Program GMP gmp gem Ruby Bignum
204
+ --------- -------- --------- -------------
205
+ multiply 5.58e+04 2.17e+04 4.89e+03
206
+ divide 2.77e+05 1.46e+05 2.32e+04
207
+ gcd 3.68e+03 2.94e+03 3.08e+01
208
+ gcdext 2.22e+03 1.74e+03 1.05e+01
209
+ [base] 3.53e+04 1.93e+04 1.27e+03
210
+ rsa 2.68e+03 2.59e+03 1.45e+02
211
+ [app] 2.68e+03 2.59e+03 1.45e+02
212
+ [bench] 9.73e+03 7.06e+03 4.29e+02
213
+
214
+ 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.
215
+
216
+ Here are the specifics of these tests:
217
+
218
+ * The pure GMP tests used GMP 5.0.5, compiled with Apple's GCC 4.2.1.
219
+ * 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.
220
+ * The Ruby Bignum tests used Ruby 1.9.3, compiled with Apple's GCC 4.2.1
221
+
222
+ \newpage
223
+
224
+ ### gmp gem v Ruby Bignum
225
+
226
+ 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:
227
+
228
+ Test Case Ruby Bignum gmp gem Bignum over gmp gem
229
+ ------------------------- ------------- --------- ---------------------
230
+ multiply(128) 1.81e+06 1.06e+06 0.59
231
+ multiply(512) 8.48e+05 9.13e+05 1.08
232
+ multiply(2097152) 2.85e+00 6.24e+01 21.95
233
+ multiply(128,128) 1.73e+06 1.07e+06 0.62
234
+ multiply(512,512) 8.25e+05 8.77e+05 1.06
235
+ multiply(2097152,2097152) 2.29e+00 3.99e+01 17.48
236
+ multiply 4.89e+03 2.17e+04 4.43
237
+ divide(8192,32) 1.98e+05 3.90e+05 1.97
238
+ divide(8192,4096) 1.61e+04 1.41e+05 8.81
239
+ divide(131072,65536) 6.60e+01 1.62e+03 24.47
240
+ divide 2.32e+04 1.46e+05 6.29
241
+ gcd(128,128) 3.58e+04 7.53e+05 21.05
242
+ gcd(8192,8192) 6.65e+01 5.11e+03 76.83
243
+ gcd(1048576,1048576) 8.17e-03 4.36e+00 532.98
244
+ gcd 3.08e+01 2.94e+03 95.38
245
+ gcdext(128,128) 1.32e+04 3.44e+05 26.03
246
+ gcdext(8192,8192) 2.07e+01 3.16e+03 152.61
247
+ gcdext(1048576,1048576) 3.23e-03 2.83e+00 876.51
248
+ gcdext 1.05e+01 1.74e+03 165.59
249
+ [base] 1.27e+03 1.93e+04 15.20
250
+ rsa(512) 5.52e+02 1.37e+04 24.88
251
+ rsa(2048) 3.48e+01 4.46e+02 12.80
252
+ rsa 1.45e+02 2.59e+03 17.84
253
+ [app] 1.45e+02 2.59e+03 17.84
254
+ [bench] 4.29e+02 7.06e+03 16.47
255
+
256
+ 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.
257
+
258
+ 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.
259
+
260
+ 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.
261
+
262
+ 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 _catching up_ to the gmp gem. This is currently not understood.
263
+
264
+ \newpage
265
+
266
+ ### Pure GMP vs gmp gem
267
+
268
+ 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:
269
+
270
+ Test Case gmp gem Pure GMP GMP over gmp gem
271
+ ------------------------- ---------- ---------- ------------------
272
+ multiply(128) 1.06e+06 4.56e+07 43.05
273
+ multiply(512) 9.13e+05 8.79e+06 9.63
274
+ multiply(2097152) 6.24e+01 6.38e+01 1.02
275
+ multiply(128,128) 1.07e+06 4.55e+07 42.50
276
+ multiply(512,512) 8.77e+05 6.39e+06 7.29
277
+ multiply(2097152,2097152) 3.99e+01 4.09e+01 1.02
278
+ multiply 2.17e+04 5.58e+04 2.57
279
+ divide(8192,32) 3.90e+05 7.23e+05 1.86
280
+ divide(8192,4096) 1.41e+05 1.72e+05 1.22
281
+ divide(131072,65536) 1.62e+03 1.64e+03 1.01
282
+ divide 1.46e+05 2.77e+05 1.90
283
+ gcd(128,128) 7.53e+05 1.82e+06 2.42
284
+ gcd(8192,8192) 5.11e+03 5.16e+03 1.01
285
+ gcd(1048576,1048576) 4.36e+00 4.37e+00 1.00
286
+ gcd 2.94e+03 3.68e+03 1.25
287
+ gcdext(128,128) 3.44e+05 8.40e+05 2.44
288
+ gcdext(8192,8192) 3.16e+03 3.20e+03 1.01
289
+ gcdext(1048576,1048576) 2.83e+00 2.85e+00 1.01
290
+ gcdext 1.74e+03 2.22e+03 1.28
291
+ [base] 1.93e+04 3.53e+04 1.83
292
+ rsa(512) 1.37e+04 1.49e+04 1.08
293
+ rsa(2048) 4.46e+02 4.48e+02 1.00
294
+ rsa 2.59e+03 2.68e+03 1.04
295
+ [app] 2.59e+03 2.68e+03 1.04
296
+ [bench] 7.06e+03 9.73e+03 1.38
297
+
298
+ 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.
299
+
300
+ 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.
301
+
302
+ 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.
303
+
304
+ 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:
305
+
306
+ * Improvements in "Matz's Ruby Interpreter" may reduce the overhead.
307
+ * A different Ruby VM, such as Rubinius and JRuby, may reduce the overhead.
308
+ * 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.
309
+ * 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.
310
+
311
+ ## Future Plans
312
+
313
+ 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:
314
+
315
+ * The `pi` program (benchmark test) needs to be written, in order to compare more closely the gmp gem with GMP.
316
+ * Various Bignum methods need to be written more seriously, namely `gcd` and `gcdext`. These can use faster alrogithms, but still exist as Ruby code (see <http://gmplib.org/manual/Greatest-Common-Divisor-Algorithms.html>), or be reimplemented as Ruby C extensions. Also, `Bignum#[]=` should probably be reimplemented as a C extension. All of these would be candidates to contribute back to Ruby Core.
317
+ * The `rsa` results between Ruby's Bignum and gmp gem need to be understood.
318
+ * 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.
319
+ * 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.
320
+ * 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.
321
+ * The results listed in this report were all conducted on Mac OS X 10.6.8. While they should certainly translate _roughly_ 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.
data/performance.pdf ADDED
Binary file
data/test/gmp_tlcm.rb ADDED
@@ -0,0 +1,67 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
2
+
3
+ class GMP_TLCM < Test::Unit::TestCase
4
+ Primes = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,
5
+ 101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,
6
+ 191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,
7
+ 281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,
8
+ 389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,
9
+ ].map { |p| GMP::Z(p) }
10
+ NumPrimes = Primes.size
11
+
12
+ def test_lcm()
13
+ want = GMP::Z(1)
14
+ Primes.each do |y|
15
+ x = want
16
+ want = want * y
17
+ check(want, x, y)
18
+ end
19
+
20
+ x = want
21
+ Primes.each do |y|
22
+ check(want, x, y)
23
+ end
24
+
25
+ want = Primes[0]
26
+ (1...NumPrimes).each do |i|
27
+ x = want
28
+ y = Primes[i] * Primes[i-1]
29
+ want = want * Primes[i]
30
+ check(want, x, y)
31
+ end
32
+
33
+ want = GMP::Z(1)
34
+ x = GMP::Z(1)
35
+ y = GMP::Z(1)
36
+ (0...(NumPrimes-2)).step(3).each do |i|
37
+ want = want * Primes[i]
38
+ want = want * Primes[i+1]
39
+ want = want * Primes[i+2]
40
+
41
+ x = x * Primes[i]
42
+ x = x * Primes[i+1]
43
+
44
+ y = y * Primes[i+1]
45
+ y = y * Primes[i+2]
46
+
47
+ check(want, x, y)
48
+ end
49
+ end
50
+
51
+ def check(want, x_orig, y_orig)
52
+ x = x_orig.dup
53
+ y = y_orig.dup
54
+
55
+ [0,1].each do |swap|
56
+ x.swap(y)
57
+ [0,1].each do |negx|
58
+ x = -x
59
+ [0,1].each do |negy|
60
+ y = -y
61
+ got = x.lcm y
62
+ assert_equal(want, got, "lcm should produce the correct least common multiple for #{x}, #{y}")
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,124 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
2
+
3
+ class GMP_TPRINTF < Test::Unit::TestCase
4
+ MAX_OUTPUT = 1024
5
+ def setup
6
+ end
7
+
8
+ def check_plain(want, fmt_orig, *args)
9
+ fmt = ''
10
+ idx2 = 0
11
+ fmt_orig.split(//).each_with_index do |char, idx|
12
+ case char
13
+ # The exact value of the exponent isn't guaranteed in glibc, and it and
14
+ # gmp_printf do slightly different things, so don't compare directly.
15
+ when 'a' then return
16
+ when 'A' then return
17
+ when 'F'
18
+ if idx > 0 and fmt_orig[idx-1] == '.'
19
+ return # don't test the "all digits" cases
20
+ end
21
+ # discard 'F' type
22
+ next
23
+ when 'Z'
24
+ # transmute
25
+ # was 'l' in t-printf.c, but Ruby does not have such an 'l'
26
+ next
27
+ else
28
+ fmt[idx2] = fmt_orig[idx]
29
+ idx2 += 1
30
+ end
31
+ end
32
+ assert(fmt.size < MAX_OUTPUT)
33
+ got = GMP.sprintf(fmt, *args)
34
+
35
+ assert_equal(want, got, "GMP.sprintf() generates correct output.")
36
+ end
37
+
38
+ def check_one(want, fmt, *args)
39
+ assert_equal(want, GMP.sprintf(fmt, *args))
40
+ end
41
+
42
+ def hex_or_octal(fmt)
43
+ fmt =~ /[xXo]/
44
+ end
45
+
46
+ def test_check_z
47
+ data = [
48
+ ['%Zd', '0', '0'],
49
+ ['%Zd', '1', '1'],
50
+ ['%Zd', '123', '123'],
51
+ ['%Zd', '-1', '-1'],
52
+ ['%Zd', '-123', '-123'],
53
+
54
+ ['%+Zd', '0', '+0'],
55
+ ['%+Zd', '123', '+123'],
56
+ ['%+Zd', '-123', '-123'],
57
+
58
+ ['%Zx', '123', '7b'],
59
+ ['%ZX', '123', '7B'],
60
+ ['%Zx', '-123', '-7b'],
61
+ ['%ZX', '-123', '-7B'],
62
+ ['%Zo', '123', '173'],
63
+ ['%Zo', '-123', '-173'],
64
+
65
+ ['%#Zx', '0', '0'],
66
+ ['%#ZX', '0', '0'],
67
+ ['%#Zx', '123', '0x7b'],
68
+ ['%#ZX', '123', '0X7B'],
69
+ ['%#Zx', '-123', '-0x7b'],
70
+ ['%#ZX', '-123', '-0X7B'],
71
+
72
+ ['%#Zo', '0', '0'],
73
+ ['%#Zo', '123', '0173'],
74
+ ['%#Zo', '-123', '-0173'],
75
+
76
+ ['%10Zd', '0', ' 0'],
77
+ ['%10Zd', '123', ' 123'],
78
+ ['%10Zd', '-123', ' -123'],
79
+
80
+ ['%-10Zd', '0', '0 '],
81
+ ['%-10Zd', '123', '123 '],
82
+ ['%-10Zd', '-123', '-123 '],
83
+
84
+ ['%+10Zd', '123', ' +123'],
85
+ ['%+-10Zd', '123', '+123 '],
86
+ ['%+10Zd', '-123', ' -123'],
87
+ ['%+-10Zd', '-123', '-123 '],
88
+
89
+ ['%08Zd', '0', '00000000'],
90
+ ['%08Zd', '123', '00000123'],
91
+ ['%08Zd', '-123', '-0000123'],
92
+
93
+ ['%+08Zd', '0', '+0000000'],
94
+ ['%+08Zd', '123', '+0000123'],
95
+ ['%+08Zd', '-123', '-0000123'],
96
+
97
+ ['%#08Zx', '0', '00000000'],
98
+ ['%#08Zx', '123', '0x00007b'],
99
+ ['%#08Zx', '-123', '-0x0007b'],
100
+
101
+ ['%+#08Zx', '0', '+0000000'],
102
+ ['%+#08Zx', '123', '+0x0007b'],
103
+ ['%+#08Zx', '-123', '-0x0007b'],
104
+
105
+ ['%.0Zd', '0', ''],
106
+ ['%.1Zd', '0', '0'],
107
+ ['%.2Zd', '0', '00'],
108
+ ['%.3Zd', '0', '000']
109
+ ]
110
+
111
+ data.each do |parameters|
112
+ z = GMP::Z(parameters[1])
113
+ if not hex_or_octal(parameters[0]) and (parameters[0]['+'] or z < 0)
114
+ check_plain(parameters[2], parameters[0], z.to_i)
115
+ end
116
+
117
+ check_one(parameters[2], parameters[0], z)
118
+
119
+ # Same again, with %N and possibly some high zero limbs
120
+ nfmt = parameters[0].dup
121
+ #lots more weird code... resulting in another check_one
122
+ end
123
+ end
124
+ end