Skip to content

Commit 2cb6eb5

Browse files
committed
benchmark
1 parent c094750 commit 2cb6eb5

File tree

4 files changed

+63
-16
lines changed

4 files changed

+63
-16
lines changed

benchmark/insert.lisp

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
(in-package #:rope/benchmark)
2+
3+
;; (progn
4+
;; (sb-ext:restrict-compiler-policy 'speed 3 3)
5+
;; (sb-ext:restrict-compiler-policy 'debug 0 0)
6+
;; ;; (sb-ext:restrict-compiler-policy 'space 0 0)
7+
;; (sb-ext:restrict-compiler-policy 'safety 0 0))
8+
9+
10+
(defparameter *readme*
11+
(merge-pathnames "README.md" (asdf:system-source-directory :rope)))
12+
13+
(defun print-time (time reps length)
14+
(format t "rope size: ~a, ~a microseconds~%"
15+
length
16+
(* 1000000 (/ time reps))))
17+
18+
(defmacro time* (&body body)
19+
`(let ((time))
20+
(sb-ext:call-with-timing
21+
(lambda (&rest plist) (setf time (getf plist :real-time-ms)))
22+
(lambda () ,@body))
23+
(coerce (/ time 1000) 'float)))
24+
25+
(defun benchmark-insert (&optional (reps 1000000))
26+
(with-open-file (s *readme*)
27+
(let* ((starting-rope (rope:split-rope (rope:make-rope s) 1000))
28+
(rope starting-rope))
29+
(dotimes (i 100)
30+
(print-time
31+
(time*
32+
(dotimes (i reps)
33+
(rope:insert-rope rope
34+
(random (rope:rope-length rope))
35+
"Hello, world!")))
36+
reps
37+
(rope:rope-length rope))
38+
(force-output)
39+
(setf rope (rope:concat-rope rope starting-rope))))))

benchmark/package.lisp

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
(defpackage #:rope/benchmark
2+
(:use #:cl))

rope.asd

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
(print result)
2121
(assert (eql t stat)))))
2222

23+
(asdf:defsystem #:rope/benchmark
24+
:depends-on (#:rope)
25+
:components ((:module "benchmark"
26+
:components ((:file "package")
27+
(:file "insert")))))
28+
2329
(asdf:defsystem #:rope/dev
2430
:depends-on (#:cl-dot #:rope)
2531
:components ((:module "dev"

rope.lisp

+16-16
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@
3535
;; Utils ;;
3636
;;-------;;
3737

38+
(defun branch-weight (branch)
39+
(rope-length (branch-left branch)))
40+
41+
(defun leaf-short-p (leaf)
42+
(>= *short-leaf* (rope-length leaf)))
43+
44+
(defun strcat (a b)
45+
(concatenate 'string a b))
46+
47+
(defun make-leaf (string &optional length)
48+
(make-instance 'leaf :string string :length (or length (length string))))
49+
3850
(defgeneric make-rope (source)
3951
(:documentation "Create a new rope from a string, stream, or pathname.")
4052
(:method ((source rope))
@@ -43,7 +55,7 @@
4355
(labels ((read-leaves (&optional acc)
4456
(let* ((string (make-string *long-leaf*))
4557
(length (read-sequence string source))
46-
(leaf (make-instance 'leaf :length length :string (subseq string 0 length))))
58+
(leaf (make-leaf (subseq string 0 length) length)))
4759
(if (= *long-leaf* length)
4860
(read-leaves (cons leaf acc))
4961
(cons leaf acc)))))
@@ -57,19 +69,7 @@
5769
(if (<= *long-leaf* length)
5870
(concat-rope (make-rope (subseq source 0 (round length 2)))
5971
(make-rope (subseq source (round length 2))))
60-
(make-instance 'leaf :length length :string source)))))
61-
62-
(defgeneric rope-weight (rope)
63-
(:method ((rope leaf))
64-
(rope-length rope))
65-
(:method ((rope branch))
66-
(rope-length (branch-left rope))))
67-
68-
(defun leaf-short-p (leaf)
69-
(>= *short-leaf* (rope-length leaf)))
70-
71-
(defun strcat (a b)
72-
(concatenate 'string a b))
72+
(make-leaf source length)))))
7373

7474
;;-----------;;
7575
;; Iteration ;;
@@ -175,7 +175,7 @@
175175
(:method ((rope leaf) index)
176176
(char (leaf-string rope) index))
177177
(:method ((rope branch) index)
178-
(let ((weight (rope-weight rope)))
178+
(let ((weight (branch-weight rope)))
179179
(if (< index weight)
180180
(index-rope (branch-left rope) index)
181181
(index-rope (branch-right rope) (- index weight))))))
@@ -212,7 +212,7 @@
212212
(make-rope (subseq (leaf-string rope) index))))
213213
(:method ((rope branch) index)
214214
(with-slots (left right) rope
215-
(let ((weight (rope-weight rope)))
215+
(let ((weight (branch-weight rope)))
216216
(cond ((= index weight)
217217
(values left right))
218218
((< index weight)

0 commit comments

Comments
 (0)