{"id":150,"date":"2008-11-06T05:54:58","date_gmt":"2008-11-06T05:54:58","guid":{"rendered":"http:\/\/www.blueraja.com\/blog2\/?p=150"},"modified":"2011-04-28T12:16:15","modified_gmt":"2011-04-28T17:16:15","slug":"java-biginteger-benchmark","status":"publish","type":"post","link":"https:\/\/www.blueraja.com\/blog\/150\/java-biginteger-benchmark","title":{"rendered":"Java BigInteger Benchmarks"},"content":{"rendered":"<p>According to <a href=\"http:\/\/java.sun.com\/javase\/6\/docs\/technotes\/guides\/performance\/speed.html\">this page<\/a> (near the bottom), when Java 1.3 switched from using a native BigInteger implementation to a purely-Java implementation, it gained an increase in speed of &quot;as much as 5x or more.&quot; This has led many people to believe that Java&#8217;s BigInteger class is comparable in speed to the popular <a href=\"http:\/\/www.openssl.org\/docs\/crypto\/bn.html\">Bignum<\/a> and <a href=\"http:\/\/gmplib.org\/\">GMP<\/a> C-libraries.<\/p>\n<p>I recently got to work with both BigInteger and Bignum for a project implementing a signing scheme related to <a href=\"http:\/\/www.blueraja.com\/blog\/node\/3\">RSA<\/a>, so I decided to go a bit out of my way and compare the speeds of each on my machine. In addition, I needed a list of the first 16-million odd prime-numbers, so I implemented a simple <a href=\"http:\/\/en.wikipedia.org\/wiki\/Sieve_of_Erastothenes\">Sieve<\/a> (pronounced &quot;<em>siv<\/em>,&quot; not &quot;<em>seeve<\/em>&quot;) in both Java and C, with some surprising benchmarks.<\/p>\n<p><span class=\"heading\">The Setup<\/span><br \/>\n  I used the current latest versions of the Java runtime (1.6.0.07) and OpenSSL (0.9.8i) in conducting these tests. They were run on an HP Pavilion dv9720us laptop with a dual-core Turion64 TL-60 (2 GHz) under Windows XP. Neither program was written to explicitly take advantage of the dual-core setup, and as far as I could tell (observing Windows task-manager), neither of them did.<\/p>\n<p><span class=\"heading\">BigInteger vs Bignum<\/span><br \/>\n  The following table summarizes the results. Note that when I say <em>small exponentiation<\/em>, what I mean is a large (1024-bit) base to a small (32-bit) power, with a large modulus.<\/p>\n<table style=\"width:50%;\" border=\"1\">\n<tr>\n<td colspan=\"3\" scope=\"col\" align=\"center\"><strong>BigInteger vs. Bignum<br \/>\n    <\/strong><span style=\"font-size: 0.8em; font-style: italic;\">(smaller is better)<\/span>\n    <\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/td>\n<td><strong>BigInteger (Java)<\/strong><\/td>\n<td><strong>Bignum(C)<\/strong><\/td>\n<\/tr>\n<tr>\n<td><em>16-million small multiplies and a large exponentiation<\/em><\/td>\n<td>62.94 seconds<\/td>\n<td>16.42 seconds<\/td>\n<\/tr>\n<tr>\n<td><em>2-million small exponentiations<\/em><\/td>\n<td>1892.92 seconds<\/td>\n<td>553.41 seconds<\/td>\n<\/tr>\n<\/table>\n<p>Also, here are some graphs for those of you who can&#8217;t stand to read a blog-post with no pictures:<br \/>\n  <img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.blueraja.com\/images\/blog\/post15\/16-million_multiplies.png\" alt=\"There would be a picture here if you had a decent browser\" width=\"450\" height=\"320\" \/><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.blueraja.com\/images\/blog\/post15\/2-million_exponentiations.png\" alt=\"Here too.\" width=\"450\" height=\"320\" \/><\/p>\n<p><span class=\"heading\">Sieve of Eratosthenes<\/span><br \/>\n  Perhaps the previous results should have been expected; after all, Bignum is written mostly in pure assembly, and I&#8217;m sure there&#8217;s some crazy assembly voodoo that can dramatically increase the speed of adding and multiplying large numbers. However,  surely with all the amazing advancements in JIT technology I hear about so often, a plain ol&#8217; prime-number generator in Java will perform almost as fast as, if not faster than (the JIT does, after all, have more information available to it than a normal compiler) its half-brother, C &#8230; right?\n<\/p>\n<table style=\"width:50%;\" border=\"1\">\n<tr>\n<td colspan=\"2\" scope=\"col\" align=\"center\">\n<div align=\"center\"><strong>Sieve of Eratosthenes on 16-million numbers<br \/>\n    <\/strong><span style=\"font-size: 0.8em; font-style: italic;\">(smaller is better)<\/span> <\/div>\n<\/td>\n<\/tr>\n<tr>\n<td><strong>Java<\/strong><\/td>\n<td><strong>C<\/strong><\/td>\n<\/tr>\n<tr>\n<td>30.94 seconds<\/td>\n<td>7.80 seconds<\/td>\n<\/tr>\n<\/table>\n<p>And the graph, for those of you who dun&#8217; do numbers so good:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.blueraja.com\/images\/blog\/post15\/Sieve.png\" width=\"345\" height=\"299\" \/><\/p>\n<p><span class=\"heading\">Conclusion<br \/>\n<\/span>What can I say? Everyone knows that for calculation-intensive tasks, native programs run faster than those that run under a VM &#8211; but I believe we&#8217;ve all been drastically misled as to the difference in speed between the two. Perhaps you should keep this in mind the next time you&#8217;re doing requirements analysis for your next project.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>According to this page (near the bottom), when Java 1.3 switched from using a native BigInteger implementation to a purely-Java implementation, it gained an increase in speed of &quot;as much as 5x or more.&quot; This has led many people to believe that Java&#8217;s BigInteger class is comparable in speed to the popular Bignum and GMP [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.blueraja.com\/blog\/wp-json\/wp\/v2\/posts\/150"}],"collection":[{"href":"https:\/\/www.blueraja.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.blueraja.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.blueraja.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.blueraja.com\/blog\/wp-json\/wp\/v2\/comments?post=150"}],"version-history":[{"count":3,"href":"https:\/\/www.blueraja.com\/blog\/wp-json\/wp\/v2\/posts\/150\/revisions"}],"predecessor-version":[{"id":223,"href":"https:\/\/www.blueraja.com\/blog\/wp-json\/wp\/v2\/posts\/150\/revisions\/223"}],"wp:attachment":[{"href":"https:\/\/www.blueraja.com\/blog\/wp-json\/wp\/v2\/media?parent=150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blueraja.com\/blog\/wp-json\/wp\/v2\/categories?post=150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blueraja.com\/blog\/wp-json\/wp\/v2\/tags?post=150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}