D looses in speed to Common Lisp
Justin Whear via Digitalmars-d
digitalmars-d at puremagic.com
Mon May 11 14:38:10 PDT 2015
All those allocations aren't helping. Here's a much more idiomatic D
version:
import std.stdio, std.bigint;
import std.range;
void main() {
int n = 100000;
auto fib1 = recurrence!("a[n-1] + a[n-2]")(BigInt(0), BigInt
(1)).takeExactly(n);
auto fib2 = recurrence!("a[n-1] + a[n-2]")(BigInt(0), BigInt
(1)).takeExactly(n);
BigInt sumFib1;
foreach (e; fib1)
sumFib1 += e;
BigInt sumFib2;
foreach (e; fib2)
sumFib2 += e;
writeln(sumFib2 - sumFib1); // 0
}
Timing on my box:
$ time ./fib
0
real 0m1.520s
user 0m1.520s
sys 0m0.000s
Compiling with `ldmd2 fib.d -inline -noboundscheck -O -release`:
$ time ./fib
0
real 0m0.784s
user 0m0.776s
sys 0m0.000s
More information about the Digitalmars-d
mailing list