D looses in speed to Common Lisp

Dzhon Smit via Digitalmars-d digitalmars-d at puremagic.com
Mon May 11 14:15:31 PDT 2015


Just in case you wonder, here's a comparison of the Fibonacci 
numbers.  The D code was written by Dennis Ritchie (a user of 
this forum, not the guy who invented C).

[code]import std.stdio, std.bigint;

void main() {

     BigInt[] fib1, fib2;
     BigInt last = 0, next = 1;

     int n = 100000;

     int i;
     while (i != n) {
         fib1 ~= last, last = next, next += fib1[$ - 1];
         ++i;
     }

     i = 0, last = 0, next = 1;
     while (i != n) {
         fib2 ~= last, last = next, next += fib2[$ - 1];
         ++i;
     }

     BigInt sumFib1;
     foreach (e; fib1) {
         sumFib1 += e;
     }

     BigInt sumFib2;
     foreach (e; fib2) {
         sumFib2 += e;
     }

     writeln(sumFib2 - sumFib1); // 0
}[/code]
[code];;;; fib.lisp
(defun main ()
   (let ((n 100000))
     (let ((fib1 (do ((i 0 (incf i))
                      (last 0 next)
                      (next 1 (+ next last))
                      (fib '() (cons last fib)))
                   ((= i n) (nreverse fib))))
           (fib2 (do ((i 0 (incf i))
                      (last 0 next)
                      (next 1 (+ next last))
                      (fib '() (cons last fib)))
                   ((= i n) (nreverse fib)))))
       (let ((sum-fib-1 (loop :for e :in fib1 :sum e))
             (sum-fib-2 (loop :for e :in fib2 :sum e)))
         (- sum-fib-2 sum-fib-1)))))

(format t "~D~%" (main))[/code]

Tests on my machine:
[code]$ time ./fib
0

real    0m6.458s
user    0m2.250s
sys     0m0.933s
$ time sbcl --dynamic-space-size 4GB --script fib.lisp
0

real    0m1.884s
user    0m1.290s
sys     0m0.260s[/code]

[quote]Email address

When posting, you need to indicate an email address. It doesn't 
need to be a valid one; this software will not send anything to 
the specified address. The email address will be made public to 
other users of the news server / mailing list you are posting to. 
Therefore, please be aware that malicious robots may be able to 
collect your address and send spam to it.[/quote]

This is quite disappointing, I'd prefer spam from this forum 
rather than from elsewhere.


More information about the Digitalmars-d mailing list