Created
August 3, 2017 13:08
-
-
Save masatoi/1cd83e0b6eaa17845c6e1a89fd8818e0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defun multiply-matrix (m1 m2 m3) | |
| (declare (optimize (speed 3) (safety 0)) | |
| (type (simple-array double-float) m1 m2 m3)) | |
| (let* ((rows1 (array-dimension m1 0)) | |
| (cols1 (array-dimension m1 1)) | |
| (cols2 (array-dimension m2 1))) | |
| (declare (type fixnum rows1 cols1 cols2)) | |
| (loop for row fixnum from 0 below rows1 do | |
| (loop for col fixnum from 0 below cols2 do | |
| (loop for i fixnum from 0 below cols1 do | |
| (setf (aref m3 row col) | |
| (+ (aref m3 row col) | |
| (* (aref m1 row i) | |
| (aref m2 i col))))))))) | |
| (defun make-random-mat (size) | |
| (let ((mat (make-array (list size size) :element-type 'double-float))) | |
| (dotimes (i (array-dimension mat 0)) | |
| (dotimes (j (array-dimension mat 1)) | |
| (setf (aref mat i j) (random 1d0)))) | |
| mat)) | |
| (defparameter m1 (make-random-mat 1000)) | |
| (defparameter m2 (make-random-mat 1000)) | |
| (defparameter m3 (make-array '(1000 1000) :element-type 'double-float)) | |
| (time (multiply-matrix m1 m2 m3)) | |
| ;; Evaluation took: | |
| ;; 3.761 seconds of real time | |
| ;; 3.760000 seconds of total run time (3.760000 user, 0.000000 system) | |
| ;; 99.97% CPU | |
| ;; 12,759,190,930 processor cycles | |
| ;; 0 bytes consed | |
| (defun multiply-matrix (m1 m2 m3) | |
| (declare (optimize (speed 3) (safety 0)) | |
| (type (simple-array double-float) m1 m2 m3)) | |
| (let* ((rows1 (array-dimension m1 0)) | |
| (cols1 (array-dimension m1 1)) | |
| (cols2 (array-dimension m2 1))) | |
| (declare (type fixnum rows1 cols1 cols2)) | |
| (loop for row fixnum from 0 below rows1 do | |
| (loop for col fixnum from 0 below cols2 do | |
| (loop for i fixnum from 0 below cols1 do | |
| (setf (aref m3 row col) | |
| (+ (aref m3 row col) | |
| (* (aref m1 row i) | |
| (aref m2 i col))))))))) | |
| (time (multiply-matrix m1 m2 m3)) | |
| ;; Evaluation took: | |
| ;; 6.683 seconds of real time | |
| ;; 6.680000 seconds of total run time (6.676000 user, 0.004000 system) | |
| ;; 99.96% CPU | |
| ;; 22,670,405,111 processor cycles | |
| ;; 0 bytes consed | |
| ;;; Clozure CL | |
| ;; (MULTIPLY-MATRIX M1 M2 M3) | |
| ;; took 4,361,247 microseconds (4.361247 seconds) to run. | |
| ;; During that period, and with 4 available CPU cores, | |
| ;; 4,364,000 microseconds (4.364000 seconds) were spent in user mode | |
| ;; 0 microseconds (0.000000 seconds) were spent in system mode | |
| ;; 6 minor page faults, 0 major page faults, 0 swaps. | |
| ;; (MULTIPLY-MATRIX M1 M2 M3) | |
| ;; took 11,228,603 microseconds (11.228603 seconds) to run. | |
| ;; During that period, and with 4 available CPU cores, | |
| ;; 11,232,000 microseconds (11.232000 seconds) were spent in user mode | |
| ;; 0 microseconds ( 0.000000 seconds) were spent in system mode |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment