-
-
Save g000001/2c81444eb9aa6daf2540641b8d752eb3 to your computer and use it in GitHub Desktop.
Common Lisp: read/write speed
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
;;; -*- mode: Lisp; coding: utf-8 -*- | |
(defpackage :read-write-test (:use :cl)) | |
(in-package :read-write-test) | |
(defconstant one-hundred-million 100000000) | |
(defun lexical-var-write () | |
(let ((x 0)) | |
(dotimes (i one-hundred-million) | |
(setf x i)))) | |
(defun lexical-var-read () | |
(let ((x 0)) | |
(dotimes (i one-hundred-million) | |
(values x)))) | |
(defun lexical-cons-1-write () | |
(let ((x (list 0))) | |
(dotimes (i one-hundred-million) | |
(setf (car x) i)))) | |
(defun lexical-cons-1-read () | |
(let ((x (list 0))) | |
(dotimes (i one-hundred-million) | |
(values (car x))))) | |
(defun lexical-cons-10-write () | |
(let ((x (make-list 10 :initial-element 0))) | |
(dotimes (i one-hundred-million) | |
(setf (car (nthcdr 9 x)) i)))) | |
(defun lexical-cons-10-read () | |
(let ((x (make-list 10 :initial-element 0))) | |
(dotimes (i one-hundred-million) | |
(values (car (nthcdr 9 x)))))) | |
(defun lexical-vector-1-write () | |
(let ((x (make-array 1 :initial-element 0))) | |
(dotimes (i one-hundred-million) | |
(setf (aref x 0) i)))) | |
(defun lexical-vector-1-read () | |
(let ((x (make-array 1 :initial-element 0))) | |
(dotimes (i one-hundred-million) | |
(values (aref x 0))))) | |
(defun lexical-vector-10-write () | |
(let ((x (make-array 10 :initial-element 0))) | |
(dotimes (i one-hundred-million) | |
(setf (aref x 9) i)))) | |
(defun lexical-vector-10-read () | |
(let ((x (make-array 10 :initial-element 0))) | |
(dotimes (i one-hundred-million) | |
(values (aref x 9))))) | |
(defun lexical-plist-1-write () | |
(let ((x (list :x 0))) | |
(dotimes (i one-hundred-million) | |
(setf (getf x :x) i)))) | |
(defun lexical-plist-1-read () | |
(let ((x (list :x 0))) | |
(dotimes (i one-hundred-million) | |
(values (getf x :x))))) | |
(defun lexical-plist-10-write () | |
(let ((x (append (make-list 18) (list :x 0)))) | |
(dotimes (i one-hundred-million) | |
(setf (getf x :x) i)))) | |
(defun lexical-plist-10-read () | |
(let ((x (append (make-list 18) (list :x 0)))) | |
(dotimes (i one-hundred-million) | |
(values (getf x :x))))) | |
(defun symbol-plist-1-write () | |
(let ((x (gensym))) | |
(dotimes (i one-hundred-million) | |
(setf (get x :x) i)))) | |
(defun symbol-plist-1-read () | |
(let ((x (gensym))) | |
(dotimes (i one-hundred-million) | |
(values (get x :x))))) | |
(defun symbol-plist-10-write () | |
(let ((x (gensym))) | |
(setf (symbol-plist x) | |
(append (make-list 18) (list :x 0))) | |
(dotimes (i one-hundred-million) | |
(setf (get x :x) i)))) | |
(defun symbol-plist-10-read () | |
(let ((x (gensym))) | |
(setf (symbol-plist x) | |
(append (make-list 18) (list :x 0))) | |
(dotimes (i one-hundred-million) | |
(values (get x :x))))) | |
(defun closure-1-write () | |
(let ((fctn (let ((x 0)) | |
(declare (ignorable x)) | |
(lambda (n) | |
(setf x n))))) | |
(dotimes (i one-hundred-million) | |
(funcall fctn i)))) | |
(defun closure-1-read () | |
(let ((fctn (let ((x 0)) | |
(lambda () (values x))))) | |
(dotimes (i one-hundred-million) | |
(values (funcall fctn))))) | |
(defstruct foo | |
(x 0)) | |
(defun struct-1-write () | |
(let ((s (make-foo))) | |
(dotimes (i one-hundred-million) | |
(setf (foo-x s) i)))) | |
(defun struct-1-read () | |
(let ((s (make-foo))) | |
(dotimes (i one-hundred-million) | |
(values (foo-x s))))) | |
(defstruct foo10 | |
y z a b c d e f g (x 0)) | |
(defun struct-10-write () | |
(let ((s (make-foo10))) | |
(dotimes (i one-hundred-million) | |
(setf (foo10-x s) i)))) | |
(defun struct-10-read () | |
(let ((s (make-foo10))) | |
(dotimes (i one-hundred-million) | |
(values (foo10-x s))))) | |
(defclass bar () | |
((x :initform 0 :accessor bar-x))) | |
(defun class-1-write () | |
(let ((o (make-instance 'bar))) | |
(dotimes (i one-hundred-million) | |
(setf (bar-x o) i)))) | |
(defun class-1-read () | |
(let ((o (make-instance 'bar))) | |
(dotimes (i one-hundred-million) | |
(values (bar-x o))))) | |
(defclass bar10 () | |
#.(cons '(x :initform 0 :accessor bar10-x) | |
(mapcar (lambda (x) | |
(list* (gensym) :initform 0 :accessor (gensym) x)) | |
(make-list 9)))) | |
(defun class-10-write () | |
(let ((o (make-instance 'bar10))) | |
(dotimes (i one-hundred-million) | |
(setf (bar10-x o) i)))) | |
(defun class-10-read () | |
(let ((o (make-instance 'bar10))) | |
(dotimes (i one-hundred-million) | |
(values (bar10-x o))))) | |
(defun hash-1-write () | |
(let ((h (make-hash-table))) | |
(setf (gethash :x h) 0) | |
(dotimes (i one-hundred-million) | |
(setf (gethash :x h) i)))) | |
(defun hash-1-read () | |
(let ((h (make-hash-table))) | |
(setf (gethash :x h) 0) | |
(dotimes (i one-hundred-million) | |
(values (gethash :x h))))) | |
(defun hash-10-write () | |
(let ((h (make-hash-table))) | |
(setf (gethash :x h) 0) | |
(dotimes (i 9) | |
(setf (gethash (gensym) h) 0)) | |
(dotimes (i one-hundred-million) | |
(setf (gethash :x h) i)))) | |
(defun hash-10-read () | |
(let ((h (make-hash-table))) | |
(setf (gethash :x h) 0) | |
(dotimes (i 9) | |
(setf (gethash (gensym) h) 0)) | |
(dotimes (i one-hundred-million) | |
(values (gethash :x h))))) | |
(defun symbol-value-1-write () | |
(let ((sym (gensym))) | |
(setf (symbol-value sym) 0) | |
(dotimes (i one-hundred-million) | |
(setf (symbol-value sym) i)))) | |
(defun symbol-value-1-read () | |
(let ((sym (gensym))) | |
(setf (symbol-value sym) 0) | |
(dotimes (i one-hundred-million) | |
(values (symbol-value sym))))) | |
(defvar *x* 0) | |
(defun global-special-1-write () | |
(dotimes (i one-hundred-million) | |
(setf *x* i))) | |
(defun global-special-1-read () | |
(dotimes (i one-hundred-million) | |
(values *x*))) | |
(defun local-special-1-write () | |
(let ((*ls*)) | |
(declare (special *ls*)) | |
(dotimes (i one-hundred-million) | |
(setf *ls* i)))) | |
(defun local-special-1-read () | |
(let ((*ls*)) | |
(declare (special *ls*)) | |
(dotimes (i one-hundred-million) | |
(values *ls*)))) | |
(defun lexical-alist-1-write () | |
(let ((x (acons :x 0 ()))) | |
(dotimes (i one-hundred-million) | |
(setf (cdr (assoc :x x)) i)))) | |
(defun lexical-alist-1-read () | |
(let ((x (acons :x 0 ()))) | |
(dotimes (i one-hundred-million) | |
(values (cdr (assoc :x x)))))) | |
(defun lexical-alist-10-write () | |
(let ((x (reverse (acons :x 0 (make-list 9 :initial-element (list (gensym) (gensym))))))) | |
(dotimes (i one-hundred-million) | |
(setf (cdr (assoc :x x)) i)))) | |
(defun lexical-alist-10-read () | |
(let ((x (reverse (acons :x 0 (make-list 9 :initial-element (list (gensym) (gensym))))))) | |
(dotimes (i one-hundred-million) | |
(values (cdr (assoc :x x)))))) | |
(defun find/writers () | |
(let ((ans () )) | |
(do-symbols (s :read-write-test) | |
(when (and (eq :internal (nth-value 1 (find-symbol (string s)))) | |
(search "-WRITE" (string s))) | |
(push s ans))) | |
ans)) | |
(defun find/readers () | |
(let ((ans () )) | |
(do-symbols (s :read-write-test) | |
(when (and (eq :internal (nth-value 1 (find-symbol (string s)))) | |
(search "-READ" (string s))) | |
(push s ans))) | |
ans)) | |
(defun run/writers () | |
(format t "~&~60,,,'=:@< ~A ~>~%" 'write) | |
(format t | |
"~:{~7,3F sec. ~A~%~}" | |
(sort | |
(mapcar (lambda (x) | |
(let ((t1 (get-internal-real-time))) | |
(funcall x) | |
(list (/ (- (get-internal-real-time) t1) internal-time-units-per-second) | |
x))) | |
(find/writers)) | |
#'< | |
:key #'first))) | |
(defun run/readers () | |
(format t "~&~60,,,'=:@< ~A ~>~%" 'read) | |
(format t | |
"~:{~7,3F sec. ~A~%~}" | |
(sort | |
(mapcar (lambda (x) | |
(let ((t1 (get-internal-real-time))) | |
(funcall x) | |
(list (/ (- (get-internal-real-time) t1) internal-time-units-per-second) | |
x))) | |
(find/readers)) | |
#'< | |
:key #'first))) | |
(progn | |
(run/readers) | |
(run/writers)) | |
;;; *EOF* |
Author
g000001
commented
Mar 23, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment