Last active
August 29, 2015 14:10
-
-
Save stevenroose/010f3b8671141f31ca79 to your computer and use it in GitHub Desktop.
Performance comparison between two big integer implementations in Dart
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
/** | |
* Results for the Dart VM (v1.7.2) | |
* Duration for bignum's BigInteger: 59s | |
* Duration for rational's BigInt: 197s | |
* | |
* Results for dart2js in Chrome (v39.0.2171.62) | |
* Duration for bignum's BigInteger: 77s | |
* Duration for rational's BigInt: 474s | |
*/ | |
library bigint_test; | |
import "package:bignum/bignum.dart"; | |
import "package:rational/bigint.dart"; | |
void bignumTest() { | |
BigInteger o = BigInteger.ONE; | |
BigInteger f = o; | |
BigInteger i = BigInteger.TWO; | |
BigInteger l = new BigInteger("99999"); | |
Stopwatch timer = new Stopwatch()..start(); | |
while(i < l) { | |
f = f * i; | |
i = i + o; | |
} | |
timer.stop(); | |
print("Duration for bignum's BigInteger: ${timer.elapsed.inSeconds}s"); | |
} | |
void rationalTest() { | |
BigInt o = new BigInt.fromJsInt(1); | |
BigInt f = o; | |
BigInt i = new BigInt.fromJsInt(2); | |
BigInt l = new BigInt.fromJsInt(99999); | |
Stopwatch timer = new Stopwatch()..start(); | |
while(i < l) { | |
f = f * i; | |
i = i + o; | |
} | |
timer.stop(); | |
print("Duration for rational's BigInt: ${timer.elapsed.inSeconds}s"); | |
} | |
void main() { | |
bignumTest(); | |
rationalTest(); | |
} |
Have you considered benchmarking the VM's built-in big integer implementation? It would be a fun comparison and it's an area that has improved quite a lot lately (1.9.0-dev.0.0 has lots of optimizations).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Consider using https://github.com/dart-lang/benchmark_harness ?