Created
July 3, 2013 00:25
-
-
Save anonymous/5914482 to your computer and use it in GitHub Desktop.
Benchmark of Clojure and Java on simple factorial loop with BigInteger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns example.factorial | |
(:import java.math.BigInteger)) | |
(set! *unchecked-math* true) | |
(set! *warn-on-reflection* true) | |
(defn jf! [n] | |
(loop [i (int n) | |
ret 1N] | |
(if (== 1 i) | |
ret | |
(recur (dec i) (* ret i))))) | |
(defn jf-biginteger [n] | |
(loop [i (long n) | |
ret (BigInteger/valueOf 1)] | |
(if (== 1 i) | |
ret | |
(recur (dec i) | |
(.multiply ret (BigInteger/valueOf i)))))) | |
(defn -main [& args] | |
(println "Original jf!") | |
(dotimes [_ 7] | |
(let [start (System/currentTimeMillis) | |
sum (loop [i 0 | |
sum 0N] | |
(if (= i 10000) | |
sum | |
(recur (inc i) (+ sum (jf! 100))))) | |
stop (System/currentTimeMillis) | |
elapsed (- stop start)] | |
(println "Computed" (.hashCode ^Object sum) "in" elapsed "ms"))) | |
(println "jf modified to use java.math.BigInteger") | |
(dotimes [_ 7] | |
(let [start (System/currentTimeMillis) | |
sum (loop [i 0 | |
sum 0N] | |
(if (= i 10000) | |
sum | |
(recur (inc i) (+ sum (jf-biginteger 100))))) | |
stop (System/currentTimeMillis) | |
elapsed (- stop start)] | |
(println "Computed" (.hashCode ^Object sum) "in" elapsed "ms")))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example; | |
import java.math.BigInteger; | |
public class Factorial { | |
public static BigInteger factorial(final int n) { | |
BigInteger res = BigInteger.valueOf(1L); //build upresult | |
for (int i = n; i > 1; i--) | |
res = res.multiply(BigInteger.valueOf(i)); | |
return res; | |
} | |
public static void main(String[] args) { | |
for (int i = 0; i < 7; i++) { | |
BigInteger sum = BigInteger.valueOf(0); | |
long start = System.currentTimeMillis(); | |
for (int j = 0; j < 10000; j++) { | |
sum = sum.add(factorial(100)); | |
} | |
long stop = System.currentTimeMillis(); | |
long elapsed = stop - start; | |
System.out.println("Computed " + sum.hashCode() + " in " + elapsed + " ms"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# With Clojure 1.5.1 | |
$ java -version | |
java version "1.6.0_51" | |
Java(TM) SE Runtime Environment (build 1.6.0_51-b11-457-11M4509) | |
Java HotSpot(TM) 64-Bit Server VM (build 20.51-b01-457, mixed mode) | |
$ lein javac | |
$ java -cp `lein classpath` example.Factorial | |
Computed -1097089025 in 120 ms | |
Computed -1097089025 in 94 ms | |
Computed -1097089025 in 92 ms | |
Computed -1097089025 in 96 ms | |
Computed -1097089025 in 93 ms | |
Computed -1097089025 in 93 ms | |
Computed -1097089025 in 94 ms | |
$ java -cp `lein classpath` clojure.main -m example.factorial | |
Original jf! | |
Computed -1097089025 in 244 ms | |
Computed -1097089025 in 134 ms | |
Computed -1097089025 in 123 ms | |
Computed -1097089025 in 107 ms | |
Computed -1097089025 in 108 ms | |
Computed -1097089025 in 107 ms | |
Computed -1097089025 in 105 ms | |
jf modified to use java.math.BigInteger | |
Computed -1097089025 in 118 ms | |
Computed -1097089025 in 97 ms | |
Computed -1097089025 in 93 ms | |
Computed -1097089025 in 95 ms | |
Computed -1097089025 in 95 ms | |
Computed -1097089025 in 96 ms | |
Computed -1097089025 in 95 ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment