-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrun-py4cl2-cffi.lisp
52 lines (43 loc) · 1.34 KB
/
run-py4cl2-cffi.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
(asdf:load-system "py4cl2-cffi/config" :silent t)
(setf py4cl2-cffi/config:*disable-pystop* t)
(asdf:load-system "py4cl2-cffi" :force t :silent t)
(defpackage :py4cl2-cffi-user
(:use :cl :py4cl2-cffi)
(:import-from #:py4cl2-cffi
#:pyforeign-funcall))
(in-package :py4cl2-cffi-user)
(defun call-n-times (n function)
(let ((start-time (get-internal-real-time)))
(dotimes (i n)
(funcall function i))
(/ (- (get-internal-real-time) start-time)
internal-time-units-per-second)))
(defun calls-per-second (n fn)
(let* ((total-time (call-n-times n fn))
(per-call-time (/ total-time n)))
(/ 1.0 per-call-time)))
(defun pystr (i)
(declare (optimize speed)
(type (signed-byte 64) i))
(with-pygc
(cffi:foreign-string-to-lisp
(pyforeign-funcall "PyObject_Str"
:pointer (pythonize i)
:pointer))))
(defmacro print-and-eval-perf (n lambda-form)
(terpri)
(format t "Evaluating performance of~% ~S~%on the basis of ~D runs..."
lambda-form n)
(force-output)
`(format t "~%Calls per second: ~D~%" (calls-per-second ,n ,lambda-form)))
(pystart)
(print-and-eval-perf
1000000
(lambda (x)
(declare (optimize speed))
(pystr x)))
(print-and-eval-perf
1000000
(lambda (x)
(declare (optimize speed))
(pycall "str" x)))