/mobile Handheld Friendly website

 performance measurements

Each table row shows performance measurements for this Lisp SBCL program with a particular command-line input value N.

 N  CPU secs Elapsed secs Memory KB Code B ≈ CPU Load
500,0000.390.392681398  3% 0% 0% 100%
5,000,0003.763.773,9441398  1% 1% 1% 100%
50,000,00037.5537.563,9441398  0% 0% 0% 100%

Read the ↓ make, command line, and program output logs to see how this program was run.

Read n-body benchmark to see what this program should do.

 notes

This is SBCL 1.1.13, an implementation of ANSI Common Lisp.

 n-body Lisp SBCL #2 program source code

;;   The Computer Language Benchmarks Game

;;   http://benchmarksgame.alioth.debian.org/

;;;

;;; contributed by Patrick Frankenberger

;;; modified by Juho Snellman 2005-11-18

;;;   * About 40% speedup on SBCL, 90% speedup on CMUCL

;;;   * Represent a body as a DEFSTRUCT with (:TYPE VECTOR DOUBLE-FLOAT), a

;;;     not as a structure that contains vectors

;;;   * Inline APPLYFORCES

;;;   * Replace (/ DT DISTANCE DISTANCE DISTANCE) with

;;;     (/ DT (* DISTANCE DISTANCE DISTANCE)), as is done in the other

;;;     implementations of this test.

;;;   * Add a couple of declarations

;;;   * Heavily rewritten for style (represent system as a list instead of

;;;     an array to make the nested iterations over it less clumsy, use

;;;     INCF/DECF where appropriate, break very long lines, etc)

;;; modified by Marko Kocic 

;;;   * add optimization declarations


(declaim (optimize (speed 3)(safety 0)(space 0)(debug 0)))

(defconstant +days-per-year+ 365.24d0)
(defconstant +solar-mass+ (* 4d0 pi pi))

(defstruct (body (:type (vector double-float))
                 (:conc-name nil)
                 (:constructor make-body (x y z vx vy vz mass)))
  x y z
  vx vy vz
  mass)
(deftype body () '(vector double-float 7))

(defparameter *jupiter*
  (make-body 4.84143144246472090d0
             -1.16032004402742839d0
             -1.03622044471123109d-1
             (* 1.66007664274403694d-3 +days-per-year+)
             (* 7.69901118419740425d-3 +days-per-year+)
             (* -6.90460016972063023d-5  +days-per-year+)
             (* 9.54791938424326609d-4 +solar-mass+)))

(defparameter *saturn*
  (make-body 8.34336671824457987d0
             4.12479856412430479d0
             -4.03523417114321381d-1
             (* -2.76742510726862411d-3 +days-per-year+)
             (* 4.99852801234917238d-3 +days-per-year+)
             (* 2.30417297573763929d-5 +days-per-year+)
             (* 2.85885980666130812d-4 +solar-mass+)))

(defparameter *uranus*
  (make-body 1.28943695621391310d1
             -1.51111514016986312d1
             -2.23307578892655734d-1
             (* 2.96460137564761618d-03 +days-per-year+)
             (* 2.37847173959480950d-03 +days-per-year+)
             (* -2.96589568540237556d-05 +days-per-year+)
             (* 4.36624404335156298d-05 +solar-mass+)))

(defparameter *neptune*
  (make-body 1.53796971148509165d+01
             -2.59193146099879641d+01
             1.79258772950371181d-01
             (* 2.68067772490389322d-03 +days-per-year+)
             (* 1.62824170038242295d-03 +days-per-year+)
             (* -9.51592254519715870d-05 +days-per-year+)
             (* 5.15138902046611451d-05 +solar-mass+)))

(defparameter *sun*
  (make-body 0.0d0 0.0d0 0.0d0 0.0d0 0.0d0 0.0d0 +solar-mass+))

(declaim (inline applyforces))
(defun applyforces (a b dt)
  (declare (type body a b) (type double-float dt))
  (let* ((dx (- (x a) (x b)))
         (dy (- (y a) (y b)))
         (dz (- (z a) (z b)))
	 (distance (sqrt (+ (* dx dx) (* dy dy) (* dz dz))))
	 (mag (/ dt (* distance distance distance)))
         (dxmag (* dx mag))
         (dymag (* dy mag))
         (dzmag (* dz mag)))
    (decf (vx a) (* dxmag (mass b)))
    (decf (vy a) (* dymag (mass b)))
    (decf (vz a) (* dzmag (mass b)))
    (incf (vx b) (* dxmag (mass a)))
    (incf (vy b) (* dymag (mass a)))
    (incf (vz b) (* dzmag (mass a))))
  nil)

(defun advance (system dt)
  (declare (double-float dt))
  (loop for (a . rest) on system do
        (dolist (b rest)
          (applyforces a b dt)))
  (dolist (b system)
    (incf (x b) (* dt (vx b)))
    (incf (y b) (* dt (vy b)))
    (incf (z b) (* dt (vz b))))
  nil)

(defun energy (system)
  (let ((e 0.0d0))
    (declare (double-float e))
    (loop for (a . rest) on system do
          (incf e (* 0.5d0
                     (mass a)
                     (+ (* (vx a) (vx a))
                        (* (vy a) (vy a))
                        (* (vz a) (vz a)))))
          (dolist (b rest)
            (let* ((dx (- (x a) (x b)))
                   (dy (- (y a) (y b)))
                   (dz (- (z a) (z b)))
                   (dist (sqrt (+ (* dx dx) (* dy dy) (* dz dz)))))
              (decf e (/ (* (mass a) (mass b)) dist)))))
    e))

(defun offset-momentum (system)
  (let ((px 0.0d0)
	(py 0.0d0)
	(pz 0.0d0))
    (dolist (p system)
      (incf px (* (vx p) (mass p)))
      (incf py (* (vy p) (mass p)))
      (incf pz (* (vz p) (mass p))))
    (setf (vx (car system)) (/ (- px) +solar-mass+)
          (vy (car system)) (/ (- py) +solar-mass+)
          (vz (car system)) (/ (- pz) +solar-mass+))
    nil))

(defun nbody (n)
  (declare (fixnum n))
  (let ((system (list *sun* *jupiter* *saturn* *uranus* *neptune*)))
    (offset-momentum system)
    (format t "~,9F~%" (energy system))
    (dotimes (i n)
      (advance system 0.01d0))
    (format t "~,9F~%" (energy system))))

(defun main ()
  (let ((n (parse-integer (or (car (last #+sbcl sb-ext:*posix-argv*
                                         #+cmu  extensions:*command-line-strings*
					 #+gcl  si::*command-args*)) "1"))))
    (nbody n)))

 make, command-line, and program output logs

Thu, 28 Nov 2013 06:33:22 GMT

MAKE:
cp: ‘nbody.sbcl-2.sbcl’ and ‘./nbody.sbcl-2.sbcl’ are the same file
SBCL built with: /usr/local/bin/sbcl --userinit /dev/null --batch --eval '(load "nbody.sbcl-2.sbcl_compile")'
### START nbody.sbcl-2.sbcl_compile
(handler-bind ((sb-ext:defconstant-uneql      (lambda (c) (abort c))))      (load (compile-file "nbody.sbcl-2.sbcl" ))) (save-lisp-and-die "sbcl.core" :purify t)
### END nbody.sbcl-2.sbcl_compile

; compiling file "/home/dunham/benchmarksgame/bench/nbody/nbody.sbcl-2.sbcl" (written 24 JAN 2013 02:01:15 PM):
; compiling (DECLAIM (OPTIMIZE # ...))
; compiling (DEFCONSTANT +DAYS-PER-YEAR+ ...)
; compiling (DEFCONSTANT +SOLAR-MASS+ ...)
; compiling (DEFSTRUCT (BODY # ...) ...)
; file: /home/dunham/benchmarksgame/bench/nbody/nbody.sbcl-2.sbcl
; in: DEFSTRUCT BODY
;     (DEFSTRUCT
;         (BODY (:TYPE (VECTOR DOUBLE-FLOAT)) (:CONC-NAME NIL)
;          (:CONSTRUCTOR MAKE-BODY (X Y Z VX VY VZ MASS)))
;       X
;       Y
;       Z
;       VX
;       VY
;       VZ
;       MASS)
; --> PROGN DEFUN PROGN EVAL-WHEN SB-IMPL::%DEFUN BLOCK THE ELT AREF 
; --> SB-KERNEL:HAIRY-DATA-VECTOR-REF MULTIPLE-VALUE-BIND MULTIPLE-VALUE-CALL 
; ==>
;   #'(LAMBDA (&OPTIONAL (ARRAY) (SB-INT:INDEX) &REST #:G16)
;       (DECLARE (IGNORE #:G16))
;       (DECLARE (TYPE (SIMPLE-ARRAY DOUBLE-FLOAT 1) ARRAY))
;       (SB-KERNEL:DATA-VECTOR-REF ARRAY SB-INT:INDEX))
; 
; note: doing float to pointer coercion (cost 13) to "<return value>"

; --> PROGN DEFUN PROGN EVAL-WHEN SB-IMPL::%DEFUN BLOCK SETF SB-KERNEL:%SETELT 
; --> SETF LET* MULTIPLE-VALUE-BIND LET FUNCALL SB-C::%FUNCALL (SETF AREF) 
; --> SB-KERNEL:HAIRY-DATA-VECTOR-SET MULTIPLE-VALUE-BIND MULTIPLE-VALUE-CALL 
; ==>
;   #'(LAMBDA (&OPTIONAL (ARRAY) (SB-INT:INDEX) &REST #:G27)
;       (DECLARE (IGNORE #:G27))
;       (DECLARE (TYPE (SIMPLE-ARRAY DOUBLE-FLOAT 1) ARRAY)
;                (TYPE DOUBLE-FLOAT SB-C::NEW-VALUE))
;       (SB-KERNEL:DATA-VECTOR-SET ARRAY SB-INT:INDEX SB-C::NEW-VALUE))
; 
; note: doing float to pointer coercion (cost 13) to "<return value>"

; --> PROGN DEFUN PROGN EVAL-WHEN SB-IMPL::%DEFUN BLOCK THE ELT AREF 
; --> SB-KERNEL:HAIRY-DATA-VECTOR-REF MULTIPLE-VALUE-BIND MULTIPLE-VALUE-CALL 
; ==>
;   #'(LAMBDA (&OPTIONAL (ARRAY) (SB-INT:INDEX) &REST #:G32)
;       (DECLARE (IGNORE #:G32))
;       (DECLARE (TYPE (SIMPLE-ARRAY DOUBLE-FLOAT 1) ARRAY))
;       (SB-KERNEL:DATA-VECTOR-REF ARRAY SB-INT:INDEX))
; 
; note: doing float to pointer coercion (cost 13) to "<return value>"

; --> PROGN DEFUN PROGN EVAL-WHEN SB-IMPL::%DEFUN BLOCK SETF SB-KERNEL:%SETELT 
; --> SETF LET* MULTIPLE-VALUE-BIND LET FUNCALL SB-C::%FUNCALL (SETF AREF) 
; --> SB-KERNEL:HAIRY-DATA-VECTOR-SET MULTIPLE-VALUE-BIND MULTIPLE-VALUE-CALL 
; ==>
;   #'(LAMBDA (&OPTIONAL (ARRAY) (SB-INT:INDEX) &REST #:G43)
;       (DECLARE (IGNORE #:G43))
;       (DECLARE (TYPE (SIMPLE-ARRAY DOUBLE-FLOAT 1) ARRAY)
;                (TYPE DOUBLE-FLOAT SB-C::NEW-VALUE))
;       (SB-KERNEL:DATA-VECTOR-SET ARRAY SB-INT:INDEX SB-C::NEW-VALUE))
; 
; note: doing float to pointer coercion (cost 13) to "<return value>"

; --> PROGN DEFUN PROGN EVAL-WHEN SB-IMPL::%DEFUN BLOCK THE ELT AREF 
; --> SB-KERNEL:HAIRY-DATA-VECTOR-REF MULTIPLE-VALUE-BIND MULTIPLE-VALUE-CALL 
; ==>
;   #'(LAMBDA (&OPTIONAL (ARRAY) (SB-INT:INDEX) &REST #:G48)
;       (DECLARE (IGNORE #:G48))
;       (DECLARE (TYPE (SIMPLE-ARRAY DOUBLE-FLOAT 1) ARRAY))
;       (SB-KERNEL:DATA-VECTOR-REF ARRAY SB-INT:INDEX))
; 
; note: doing float to pointer coercion (cost 13) to "<return value>"

; --> PROGN DEFUN PROGN EVAL-WHEN SB-IMPL::%DEFUN BLOCK SETF SB-KERNEL:%SETELT 
; --> SETF LET* MULTIPLE-VALUE-BIND LET FUNCALL SB-C::%FUNCALL (SETF AREF) 
; --> SB-KERNEL:HAIRY-DATA-VECTOR-SET MULTIPLE-VALUE-BIND MULTIPLE-VALUE-CALL 
; ==>
;   #'(LAMBDA (&OPTIONAL (ARRAY) (SB-INT:INDEX) &REST #:G59)
;       (DECLARE (IGNORE #:G59))
;       (DECLARE (TYPE (SIMPLE-ARRAY DOUBLE-FLOAT 1) ARRAY)
;                (TYPE DOUBLE-FLOAT SB-C::NEW-VALUE))
;       (SB-KERNEL:DATA-VECTOR-SET ARRAY SB-INT:INDEX SB-C::NEW-VALUE))
; 
; note: doing float to pointer coercion (cost 13) to "<return value>"

; --> PROGN DEFUN PROGN EVAL-WHEN SB-IMPL::%DEFUN BLOCK THE ELT AREF 
; --> SB-KERNEL:HAIRY-DATA-VECTOR-REF MULTIPLE-VALUE-BIND MULTIPLE-VALUE-CALL 
; ==>
;   #'(LAMBDA (&OPTIONAL (ARRAY) (SB-INT:INDEX) &REST #:G64)
;       (DECLARE (IGNORE #:G64))
;       (DECLARE (TYPE (SIMPLE-ARRAY DOUBLE-FLOAT 1) ARRAY))
;       (SB-KERNEL:DATA-VECTOR-REF ARRAY SB-INT:INDEX))
; 
; note: doing float to pointer coercion (cost 13) to "<return value>"

; --> PROGN DEFUN PROGN EVAL-WHEN SB-IMPL::%DEFUN BLOCK SETF SB-KERNEL:%SETELT 
; --> SETF LET* MULTIPLE-VALUE-BIND LET FUNCALL SB-C::%FUNCALL (SETF AREF) 
; --> SB-KERNEL:HAIRY-DATA-VECTOR-SET MULTIPLE-VALUE-BIND MULTIPLE-VALUE-CALL 
; ==>
;   #'(LAMBDA (&OPTIONAL (ARRAY) (SB-INT:INDEX) &REST #:G75)
;       (DECLARE (IGNORE #:G75))
;       (DECLARE (TYPE (SIMPLE-ARRAY DOUBLE-FLOAT 1) ARRAY)
;                (TYPE DOUBLE-FLOAT SB-C::NEW-VALUE))
;       (SB-KERNEL:DATA-VECTOR-SET ARRAY SB-INT:INDEX SB-C::NEW-VALUE))
; 
; note: doing float to pointer coercion (cost 13) to "<return value>"

; --> PROGN DEFUN PROGN EVAL-WHEN SB-IMPL::%DEFUN BLOCK THE ELT AREF 
; --> SB-KERNEL:HAIRY-DATA-VECTOR-REF MULTIPLE-VALUE-BIND MULTIPLE-VALUE-CALL 
; ==>
;   #'(LAMBDA (&OPTIONAL (ARRAY) (SB-INT:INDEX) &REST #:G80)
;       (DECLARE (IGNORE #:G80))
;       (DECLARE (TYPE (SIMPLE-ARRAY DOUBLE-FLOAT 1) ARRAY))
;       (SB-KERNEL:DATA-VECTOR-REF ARRAY SB-INT:INDEX))
; 
; note: doing float to pointer coercion (cost 13) to "<return value>"

; --> PROGN DEFUN PROGN EVAL-WHEN SB-IMPL::%DEFUN BLOCK SETF SB-KERNEL:%SETELT 
; --> SETF LET* MULTIPLE-VALUE-BIND LET FUNCALL SB-C::%FUNCALL (SETF AREF) 
; --> SB-KERNEL:HAIRY-DATA-VECTOR-SET MULTIPLE-VALUE-BIND MULTIPLE-VALUE-CALL 
; ==>
;   #'(LAMBDA (&OPTIONAL (ARRAY) (SB-INT:INDEX) &REST #:G91)
;       (DECLARE (IGNORE #:G91))
;       (DECLARE (TYPE (SIMPLE-ARRAY DOUBLE-FLOAT 1) ARRAY)
;                (TYPE DOUBLE-FLOAT SB-C::NEW-VALUE))
;       (SB-KERNEL:DATA-VECTOR-SET ARRAY SB-INT:INDEX SB-C::NEW-VALUE))
; 
; note: doing float to pointer coercion (cost 13) to "<return value>"

; --> PROGN DEFUN PROGN EVAL-WHEN SB-IMPL::%DEFUN BLOCK THE ELT AREF 
; --> SB-KERNEL:HAIRY-DATA-VECTOR-REF MULTIPLE-VALUE-BIND MULTIPLE-VALUE-CALL 
; ==>
;   #'(LAMBDA (&OPTIONAL (ARRAY) (SB-INT:INDEX) &REST #:G96)
;       (DECLARE (IGNORE #:G96))
;       (DECLARE (TYPE (SIMPLE-ARRAY DOUBLE-FLOAT 1) ARRAY))
;       (SB-KERNEL:DATA-VECTOR-REF ARRAY SB-INT:INDEX))
; 
; note: doing float to pointer coercion (cost 13) to "<return value>"

; --> PROGN DEFUN PROGN EVAL-WHEN SB-IMPL::%DEFUN BLOCK SETF SB-KERNEL:%SETELT 
; --> SETF LET* MULTIPLE-VALUE-BIND LET FUNCALL SB-C::%FUNCALL (SETF AREF) 
; --> SB-KERNEL:HAIRY-DATA-VECTOR-SET MULTIPLE-VALUE-BIND MULTIPLE-VALUE-CALL 
; ==>
;   #'(LAMBDA (&OPTIONAL (ARRAY) (SB-INT:INDEX) &REST #:G107)
;       (DECLARE (IGNORE #:G107))
;       (DECLARE (TYPE (SIMPLE-ARRAY DOUBLE-FLOAT 1) ARRAY)
;                (TYPE DOUBLE-FLOAT SB-C::NEW-VALUE))
;       (SB-KERNEL:DATA-VECTOR-SET ARRAY SB-INT:INDEX SB-C::NEW-VALUE))
; 
; note: doing float to pointer coercion (cost 13) to "<return value>"

; --> PROGN DEFUN PROGN EVAL-WHEN SB-IMPL::%DEFUN BLOCK THE ELT AREF 
; --> SB-KERNEL:HAIRY-DATA-VECTOR-REF MULTIPLE-VALUE-BIND MULTIPLE-VALUE-CALL 
; ==>
;   #'(LAMBDA (&OPTIONAL (ARRAY) (SB-INT:INDEX) &REST #:G112)
;       (DECLARE (IGNORE #:G112))
;       (DECLARE (TYPE (SIMPLE-ARRAY DOUBLE-FLOAT 1) ARRAY))
;       (SB-KERNEL:DATA-VECTOR-REF ARRAY SB-INT:INDEX))
; 
; note: doing float to pointer coercion (cost 13) to "<return value>"

; --> PROGN DEFUN PROGN EVAL-WHEN SB-IMPL::%DEFUN BLOCK SETF SB-KERNEL:%SETELT 
; --> SETF LET* MULTIPLE-VALUE-BIND LET FUNCALL SB-C::%FUNCALL (SETF AREF) 
; --> SB-KERNEL:HAIRY-DATA-VECTOR-SET MULTIPLE-VALUE-BIND MULTIPLE-VALUE-CALL 
; ==>
;   #'(LAMBDA (&OPTIONAL (ARRAY) (SB-INT:INDEX) &REST #:G123)
;       (DECLARE (IGNORE #:G123))
;       (DECLARE (TYPE (SIMPLE-ARRAY DOUBLE-FLOAT 1) ARRAY)
;                (TYPE DOUBLE-FLOAT SB-C::NEW-VALUE))
;       (SB-KERNEL:DATA-VECTOR-SET ARRAY SB-INT:INDEX SB-C::NEW-VALUE))
; 
; note: doing float to pointer coercion (cost 13) to "<return value>"

; compiling (DEFTYPE BODY ...)
; compiling (DEFPARAMETER *JUPITER* ...)
; compiling (DEFPARAMETER *SATURN* ...)
; compiling (DEFPARAMETER *URANUS* ...)
; compiling (DEFPARAMETER *NEPTUNE* ...)
; compiling (DEFPARAMETER *SUN* ...)
; compiling (DECLAIM (INLINE APPLYFORCES))
; compiling (DEFUN APPLYFORCES ...)
; compiling (DEFUN ADVANCE ...)
; compiling (DEFUN ENERGY ...)
; file: /home/dunham/benchmarksgame/bench/nbody/nbody.sbcl-2.sbcl
; in: DEFUN ENERGY
;     (DEFUN ENERGY (SYSTEM)
;       (LET ((E 0.0d0))
;         (DECLARE (DOUBLE-FLOAT E))
;         (LOOP FOR (A . REST) ON SYSTEM
;               DO (INCF E (* 0.5d0 # #)) (DOLIST (B REST)
;                                           (LET* #
;                                             #)))
;         E))
; --> PROGN EVAL-WHEN 
; ==>
;   (SB-IMPL::%DEFUN 'ENERGY
;                    (SB-INT:NAMED-LAMBDA ENERGY
;                        (SYSTEM)
;                      (BLOCK ENERGY
;                        (LET (#)
;                          (DECLARE #)
;                          (LOOP FOR # ON SYSTEM
;                                DO # #)
;                          E)))
;                    NIL 'NIL (SB-C:SOURCE-LOCATION))
; 
; note: doing float to pointer coercion (cost 13) from E to "<return value>"

; compiling (DEFUN OFFSET-MOMENTUM ...)
; file: /home/dunham/benchmarksgame/bench/nbody/nbody.sbcl-2.sbcl
; in: DEFUN OFFSET-MOMENTUM
;     (/ (- PX) +SOLAR-MASS+)
; 
; note: unable to
;   convert to multiplication by reciprocal
; because:
;   39.47841760435743d0 does not have an exact reciprocal

;     (/ (- PY) +SOLAR-MASS+)
; 
; note: unable to
;   convert to multiplication by reciprocal
; because:
;   39.47841760435743d0 does not have an exact reciprocal

;     (/ (- PZ) +SOLAR-MASS+)
; 
; note: unable to
;   convert to multiplication by reciprocal
; because:
;   39.47841760435743d0 does not have an exact reciprocal

; compiling (DEFUN NBODY ...)
; compiling (DEFUN MAIN ...); 
; compilation unit finished
;   printed 18 notes


; /home/dunham/benchmarksgame_onecore/nbody/tmp/nbody.sbcl-2.fasl written
; compilation finished in 0:00:00.152
[undoing binding stack and other enclosing state... done]
[saving current Lisp image into sbcl.core:
writing 3528 bytes from the read-only space at 0x0x1000000
writing 2272 bytes from the static space at 0x0x1100000
writing 29450240 bytes from the dynamic space at 0x0x9000000
done]
### START nbody.sbcl-2.sbcl_run
(main) (quit)
### END nbody.sbcl-2.sbcl_run

0.75s to complete and log all make actions

COMMAND LINE:
/usr/local/bin/sbcl   --noinform --core sbcl.core --userinit /dev/null --load nbody.sbcl-2.sbcl_run 50000000

PROGRAM OUTPUT:
-0.169075164
-0.169059907

Revised BSD license

  Home   Conclusions   License   Play