Jump to page: 1 2 3
Thread overview
D looses in speed to Common Lisp
May 11, 2015
Dzhon Smit
May 11, 2015
weaselcat
May 11, 2015
weaselcat
May 11, 2015
maarten van damme
May 11, 2015
Justin Whear
May 11, 2015
Daniel Kozak
May 11, 2015
Daniel Kozak
May 11, 2015
John Colvin
May 11, 2015
anonymous
May 11, 2015
Dzhon Smit
May 12, 2015
Laeeth Isharc
May 12, 2015
Laeeth Isharc
May 12, 2015
Daniel Kozák
May 12, 2015
anonymous
May 12, 2015
John Colvin
May 11, 2015
Dennis Ritchie
May 12, 2015
bachmeier
May 12, 2015
Chris
May 12, 2015
Marco Leise
May 12, 2015
bachmeier
May 12, 2015
Vladimir Panteleev
May 12, 2015
anonymous
May 11, 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.
May 11, 2015
On Monday, 11 May 2015 at 21:15:33 UTC, Dzhon Smit wrote:
> ...

time sbcl --dynamic-space-size 4GB --script fib.lisp
0
1.16user 1.49system 0:02.67elapsed 99%CPU (0avgtext+0avgdata 1658860maxresident)k

ldc -O5 -release -boundscheck=off fib.d
$ time ./fib
0
1.33user 0.81system 0:02.15elapsed 99%CPU (0avgtext+0avgdata 1230712maxresident)k
May 11, 2015
On Monday, 11 May 2015 at 21:30:20 UTC, weaselcat wrote:
> On Monday, 11 May 2015 at 21:15:33 UTC, Dzhon Smit wrote:
>> ...
>
> time sbcl --dynamic-space-size 4GB --script fib.lisp
> 0
> 1.16user 1.49system 0:02.67elapsed 99%CPU (0avgtext+0avgdata 1658860maxresident)k
>
> ldc -O5 -release -boundscheck=off fib.d
> $ time ./fib
> 0
> 1.33user 0.81system 0:02.15elapsed 99%CPU (0avgtext+0avgdata 1230712maxresident)k

oh, and if you make the arrays static it should be turned into CTFE.
May 11, 2015
It may be a good idea to prealocate the whole array so you don't resize the dynamic arrays every execution of the loop.

2015-05-11 23:30 GMT+02:00 weaselcat via Digitalmars-d < digitalmars-d@puremagic.com>:

> On Monday, 11 May 2015 at 21:15:33 UTC, Dzhon Smit wrote:
>
>> ...
>>
>
> time sbcl --dynamic-space-size 4GB --script fib.lisp
> 0
> 1.16user 1.49system 0:02.67elapsed 99%CPU (0avgtext+0avgdata
> 1658860maxresident)k
>
> ldc -O5 -release -boundscheck=off fib.d
> $ time ./fib
> 0
> 1.33user 0.81system 0:02.15elapsed 99%CPU (0avgtext+0avgdata
> 1230712maxresident)k
>


May 11, 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
May 11, 2015
On Mon, 11 May 2015 21:15:31 +0000
Dzhon Smit via Digitalmars-d <digitalmars-d@puremagic.com> wrote:

> 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.

On my machine this is much faster

import std.stdio, std.bigint;
import std.range;
import std.algorithm;

void main() {

     int n = 100000;
     auto sumFib1 = recurrence!("a[n-1] + a[n-2]")(BigInt(0),
             BigInt(1)).take(n).sum;
     auto sumFib2 = recurrence!("a[n-1] + a[n-2]")(BigInt(0),
             BigInt(1)).take(n).sum;

     writeln(sumFib2 - sumFib1); // 0
}


May 11, 2015
On Mon, 11 May 2015 21:38:10 +0000 (UTC)
Justin Whear via Digitalmars-d <digitalmars-d@puremagic.com> wrote:

> 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
Yep, this is what come to my mind when I read OP
May 11, 2015
On Monday, 11 May 2015 at 21:15:33 UTC, Dzhon Smit wrote:
> 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.

Turns out you can write slow code in any language. Who knew?
May 11, 2015
On Monday, 11 May 2015 at 21:15:33 UTC, Dzhon Smit wrote:
> 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]

Can't confirm that. Times for me (linux x86_64):

----
$ dmd fib.d && time ./fib
0

real    0m2.410s
user    0m1.844s
sys     0m0.558s

$ time sbcl --dynamic-space-size 4GB --script fib.lisp
0

real    0m2.659s
user    0m2.144s
sys     0m0.506s
----

As usual, ldc produces a faster binary than dmd:

----
$ ldc2 fib.d && time ./fib
0

real    0m1.900s
user    0m1.396s
sys     0m0.499s
----

Optimization flags don't seem to matter much for this program.
May 11, 2015
On Monday, 11 May 2015 at 21:15:33 UTC, Dzhon Smit wrote:
> 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]

It's some kind of stuffing the community Lisp programmers who try to prove that SBSL faster than D. Nothing like that.
« First   ‹ Prev
1 2 3