Skip to content

Instantly share code, notes, and snippets.

@stevenroose
Last active August 29, 2015 14:10
Show Gist options
  • Save stevenroose/010f3b8671141f31ca79 to your computer and use it in GitHub Desktop.
Save stevenroose/010f3b8671141f31ca79 to your computer and use it in GitHub Desktop.
Performance comparison between two big integer implementations in Dart
/**
* 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();
}
@kevmoo
Copy link

kevmoo commented Nov 26, 2014

@kasperl
Copy link

kasperl commented Dec 2, 2014

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