hash.lisp 1.93 KB
Newer Older
Eric Marsden's avatar
Eric Marsden committed
1
2
3
;; hashtable and READ-LINE benchmarking code
;;
;; some code by Paul Foley
4
;; Time-stamp: <2016-05-10 13:11:57 jack>
Eric Marsden's avatar
Eric Marsden committed
5

6
7
8
9
10
(defpackage :cl-bench.hash
  (:use :common-lisp)
  (:export #:run-slurp-lines
           #:hash-strings
           #:hash-integers))
Eric Marsden's avatar
Eric Marsden committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

(in-package :cl-bench.hash)


(defun read-many-lines (file)
  (with-open-file (f file :direction :input)
    (loop :for l = (read-line f nil)
          :while l
          :count (length l))))

(defun run-slurp-lines ()
  (cond ((probe-file "/usr/share/dict/words")
         (read-many-lines "/usr/share/dict/words"))
        ((probe-file "/usr/dict/words")
         (read-many-lines "/usr/dict/words"))))

27
(defparameter +digit+ "0123456789ABCDEF")
Eric Marsden's avatar
Eric Marsden committed
28

29
30
31
(defparameter +digits-needed+
  #((10 100 1000 10000 100000 10000000 100000000 536870911)
    (16 256 4096 65536 1048576 16777216 268435456 4294967296 536870911)))
Eric Marsden's avatar
Eric Marsden committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

(defvar *table* nil)


(defun fixnum-to-string (n base)
  (declare (fixnum n base))
  (let* ((tsize (position-if (lambda (x) (> (the fixnum x) n))
                            (aref +digits-needed+ (ash base -4))))
         (result (make-string (1+ tsize))))
    (loop for i fixnum from tsize downto 0 with q fixnum = n and r fixnum = 0
      do (multiple-value-setq (q r) (floor q base))
         (setf (schar result i) (aref +digit+ r)))
    result))

;; CMUCL-18c seems to run into a bug here: it mistakenly declares
;; counter to be a fixnum
(defun hash-strings (&optional (size 300))
  (declare (fixnum size))
   (setq *table* (make-hash-table :test #'equal :size size))
   (dotimes (i 100000)
     (setf (gethash (fixnum-to-string i 16) *table*) i))
   (maphash (lambda (key value) (incf (gethash key *table*) value)) *table*))
  
(defun hash-integers (&optional (size 300))
  (declare (fixnum size))
  (setq *table* (make-hash-table :test #'eql :size size))
  (dotimes (i 100000)
    (setf (gethash i *table*) (1+ i)))
  (maphash (lambda (key value) (incf (gethash key *table*) value)) *table*))

;; EOF