|
Prev: static typing
Next: Java is going to have closures.
From: Pierre THIERRY on 13 Aug 2006 08:51 Le Sun, 13 Aug 2006 13:50:12 +0200, Lars Brinkhoff a écrit : > I get consistently *faster* run times with SBCL 0.9.14.30 than with > GCC 4.0.4 using plain C, not C++. (There's no reason C++ would be > faster, is there?) How exactly did you instrument the C/C++ code to time it? As I didn't want to take much time on it, I just compiled the given code with a main doing 10,000 iterations with a for loop, and timed it with my shell, but this includes spawning and shutting down the process... Ha, wait. Don't mind. I could just change the number of iterations and then easily calculate the impact it really has. OK, I'm also significantly faster in Lisp... It took 1.7s even with -O3 for the C++ version (ten times with ten times iterations, so spawning the process is quite lightweight, in fact), and 1.1s in Lisp for the following: (defun pi/2-opt () (declare (optimize (debug 0)(space 0)(safety 0)(speed 3))) (do ((x -9.98 (+ x 0.01)) (v 0.0 (exp (- (expt x 2.0)))) (sum (* 2 (exp (- (expt 10.0 2.0)))) (+ sum (* 2 v)))) ((>= x 10.0) (* 0.005 sum)) (declare (type float x v sum)))) I'm impressed... Quickly, Nowhere man -- nowhere.man(a)levallois.eu.org OpenPGP 0xD9D50D8A
From: verec on 13 Aug 2006 10:06 On 2006-08-13 13:51:43 +0100, Pierre THIERRY <nowhere.man(a)levallois.eu.org> said: > (defun pi/2-opt () > (declare (optimize (debug 0)(space 0)(safety 0)(speed 3))) > (do ((x -9.98 (+ x 0.01)) > (v 0.0 (exp (- (expt x 2.0)))) > (sum (* 2 (exp (- (expt 10.0 2.0)))) (+ sum (* 2 v)))) > ((>= x 10.0) (* 0.005 sum)) > (declare (type float x v sum)))) > > I'm impressed... So am I :-) I tested this version sith sbcl 0.9.15 and indeed: * (time (dotimes (i 10000) (pi/2-optPT))) Evaluation took: 4.413 seconds of real time 3.753417 seconds of user run time 0.036023 seconds of system run time 0 page faults and 81,920 bytes consed. NIL I'm amazed at the difference a compiler makes... Many thanks to all, for all your time and efforts. -- JFB
From: Lars Brinkhoff on 13 Aug 2006 10:06 Pierre Thierry wrote: > Lars Brinkhoff wrote: > > I get consistently *faster* run times with SBCL 0.9.14.30 than > > with GCC 4.0.4. > How exactly did you instrument the C/C++ code to time it? Here's the whole thing: #include <math.h> static double pi_over_2 (void) { double sum = exp (-pow (-10.0, 2.0)) + exp (-pow (10.0, 2.0)); double x; for (x = -9.99; x < 10.0; x += 0.01) sum += 2.0 * exp (-pow (x, 2.0)) ; return sum * 0.005; } int main (void) { int i; for (i = 0; i < 100000; i++) pi_over_2 (); return 0; } > OK, I'm also significantly faster in Lisp... It took 1.7s even with > -O3 for the C++ version [...], and 1.1s in Lisp I didn't see any significant differences between -O2, -O3, and -Os, or between C and C++, so I guess that's not a factor to consider here. > (defun pi/2-opt () > (declare (optimize (debug 0)(space 0)(safety 0)(speed 3))) > (do ((x -9.98 (+ x 0.01)) > (v 0.0 (exp (- (expt x 2.0)))) > (sum (* 2 (exp (- (expt 10.0 2.0)))) (+ sum (* 2 v)))) > ((>= x 10.0) (* 0.005 sum)) > (declare (type float x v sum)))) You can also avoid boxing the return value, but I find that it doesn't improve speed noticeably in this toy program. (defun pi/2 (box) (declare (type (simple-array double-float ()) box)) (do (...) ((>= x 10.0d0) (setf (aref box) (* 0.005d0 sum)) nil) ...)) A one-element vector works just as well, but it's always fun to use a zero-dimensional array. :) (Perhaps other implementations optimize one of those better than another.)
From: Marcin 'Qrczak' Kowalczyk on 13 Aug 2006 14:50 Pierre THIERRY <nowhere.man(a)levallois.eu.org> writes: > OK, I'm also significantly faster in Lisp... It took 1.7s even with -O3 > for the C++ version (ten times with ten times iterations, so spawning > the process is quite lightweight, in fact), and 1.1s in Lisp Compile the C version with -ffast-math and do something to avoid optimizing out the whole computation (e.g sum the results and print the sum). It's twice faster than without -ffast-math on my box. -- __("< Marcin Kowalczyk \__/ qrczak(a)knm.org.pl ^^ http://qrnik.knm.org.pl/~qrczak/
From: Lars Brinkhoff on 13 Aug 2006 16:42
Marcin 'Qrczak' Kowalczyk wrote: > Pierre Thierry wrote; > > OK, I'm also significantly faster in Lisp... It took 1.7s even > > with -O3 for the C++ version [...], and 1.1s in Lisp > Compile the C version with -ffast-math and do something to avoid > optimizing out the whole computation (e.g sum the results and print > the sum). It's twice faster than without -ffast-math on my box. Significantly faster, yes, but not by a factor of two on my machine. SBCL 17.772 seconds of real time 17.745108 seconds of user run time 0.004 seconds of system run time 0 page faults and 0 bytes consed. GCC real 0m15.863s user 0m15.785s sys 0m0.012s Old results, without summing the return values: SBCL 17.729 seconds of real time 17.713108 seconds of user run time 0.004 seconds of system run time 0 page faults and 0 bytes consed. GCC real 0m19.948s user 0m19.861s sys 0m0.004s |