4 ms·
I wrote this quick little program on my laptop: (ql:quickload "cxml") (defclass finder (sax:default-handler) ((in-location :initform nil)
by eadmund 2y ago
I wrote this quick little program on my laptop:
(ql:quickload "cxml")
(defclass finder (sax:default-handler)
((in-location :initform nil)
(count :initform 0)))
(defmethod sax:start-element ((finder finder) namespace-uri local-name qname attributes)
(setf (slot-value finder 'in-location) (string= local-name "location")))
(defmethod sax:characters ((finder finder) data)
(when (and (slot-value finder 'in-location) (search "Africa" data))
(incf (slot-value finder 'count))))
(let ((finder (make-instance 'finder)))
(cxml:parse #P"out.xml" finder)
(slot-value finder 'count))
and got:
Evaluation took:
5.316 seconds of real time
5.339639 seconds of total run time (5.330888 user, 0.008751 system)
[ Real times consist of 0.032 seconds GC time, and 5.284 seconds non-GC time. ]
[ Run times consist of 0.049 seconds GC time, and 5.291 seconds non-GC time. ]
100.45% CPU
8,547,510,059 processor cycles
1,082,877,232 bytes consed
For comparison:
python etree: 3.04s
python lxml: 3.65
go stdlib: 5.18
go gosax: 2.37
C: 0.54
Really disappointing that the Common Lisp version performs worse and is less readable than the Python lxml version.
This one is more readable, but even slower:
(klacks:with-open-source (s (cxml:make-source #P"out.xml"))
(loop for key = (klacks:peek s)
with count = 0
while key
when (and (eq key :start-element)
(string= (klacks:current-lname s) "location"))
do (progn
(setq key (klacks:peek-next s))
(when (and (eq key :characters) (search "Africa" (klacks:current-characters s)))
(incf count)))
do (klacks:consume s)
finally (return count)))
It gets:
Evaluation took:
9.996 seconds of real time
10.054716 seconds of total run time (9.979712 user, 0.075004 system)
[ Real times consist of 0.092 seconds GC time, and 9.904 seconds non-GC time. ]
[ Run times consist of 0.101 seconds GC time, and 9.954 seconds non-GC time. ]
100.59% CPU
16,077,773,140 processor cycles
2,338,978,688 bytes consed