;;; -*- lexical-binding: t -*- ;; ;; ----------------------------------------------------------------------------- ;; this file: ;; https://dataswamp.org/~incal/peval/peval.el ;; ----------------------------------------------------------------------------- (require 'cl-lib) (cl-pushnew (expand-file-name ".") load-path :test #'string=) ;; ----------------------------------------------------------------------------- (let ((dm "peval.so")) (load dm nil t) (declare-function peval dm)) ;; ----------------------------------------------------------------------------- (require 'benchmark) (require 'calc-comb) (require 'cl-lib) ;; ----------------------------------------------------------------------------- (defun peval--eform (&rest form) (format "%s" (eval (car form)))) ;; (peval--eform (+ 1 3 3 7)) ; "14" ;; ----------------------------------------------------------------------------- (defun peval--primes-num (beg end &optional vrb) (cl-loop for i from beg to end count (= 1 (calcFunc-prime i)) into res if vrb finally do (message "%d" res) finally return res)) ;; (peval--primes-num 1 99 t) ;; ----------------------------------------------------------------------------- (defun peval-test () (let* ((beg 1) (int 2) (exp 22) (end (expt int exp)) (crs 8) (prts `(,beg ,@(butlast (cdr (mapcar #'floor (number-sequence beg end (/ (- end beg 0.0) crs))))) ,end)) (i 0) (del (make-string 44 ?\-)) (peval-res 0) (peval-time (benchmark-elapse (mapc (lambda (s) (cl-incf peval-res (string-to-number s))) (peval `(peval--primes-num ,beg ,(nth (cl-incf i) prts)) ; 1 `(peval--primes-num ,(1+ (nth i prts)) ,(nth (cl-incf i) prts)) ; 2 `(peval--primes-num ,(1+ (nth i prts)) ,(nth (cl-incf i) prts)) ; 3 `(peval--primes-num ,(1+ (nth i prts)) ,(nth (cl-incf i) prts)) ; 4 `(peval--primes-num ,(1+ (nth i prts)) ,(nth (cl-incf i) prts)) ; 5 `(peval--primes-num ,(1+ (nth i prts)) ,(nth (cl-incf i) prts)) ; 6 `(peval--primes-num ,(1+ (nth i prts)) ,(nth (cl-incf i) prts)) ; 7 `(peval--primes-num ,(1+ (nth i prts)) ,end))))) ; 8 (elisp-res 0) (elisp-time (benchmark-elapse (setf elisp-res (peval--primes-num beg end)))) (optimal-time (/ elisp-time (* 1.0 crs))) (peval-perc (* 100 (/ peval-time (* 1.0 elisp-time)))) (optimal-perc (* 100 (/ optimal-time (* 1.0 peval-time))))) (cl-flet ((report (fun) (funcall fun "%s" del) (funcall fun "[intro] parallel `peval' vs sequential Elisp") (funcall fun "[emacs] GNU Emacs %s" emacs-version) (funcall fun "[scope] %d^%d = %d" int exp end) (funcall fun "%s" del) (funcall fun "[elisp] (Emacs = %d) %d %.1f s" 1 elisp-res elisp-time) (funcall fun "[peval] (Emacs = %d) %d %.1f s" (1+ crs) peval-res peval-time) (funcall fun "[peval] %.1f%% runtime" peval-perc) (funcall fun "[peval] %.1f%% optimal" optimal-perc))) (if noninteractive (report #'message) (when-let* ((buf (get-buffer-create "*peval*")) (_ (bufferp buf))) (with-current-buffer buf (erase-buffer) (report (lambda (s) (insert (format "%s\n" s)))) (goto-char (point-min))) (pop-to-buffer buf)))))) ;; (peval-test) ;; ----------------------------------------------------------------------------- (provide 'peval) ;; -----------------------------------------------------------------------------