;;; -*- lexical-binding: t -*- ;; ;; this file: ;; https://dataswamp.org/~incal/emacs-init/measure.el (require 'cl-lib) (defun point-at-final-line-1 () (= (line-number-at-pos) (line-number-at-pos (point-max)) )) (defun point-at-final-line-2 () (save-excursion (end-of-line) (= 1 (forward-line) ))) (defun point-at-final-line-3 () (= (line-end-position) (point-max)) ) (defmacro measure-time (&rest body) "Measure and return the running time of the code block. http://nullprogram.com/blog/2009/05/28/" (declare (indent defun)) (let ((start (make-symbol "start"))) `(let ((,start (float-time))) ,@body (- (float-time) ,start) ))) (defun create-random-list (max len) (let ((l '())) (cl-loop repeat len do (push (random max) l)) l) ) (defun test-final-line-f (fun pos-list) (cl-loop for p in pos-list do (goto-char p) (apply fun nil) )) (defun test-final-line (its) (let*((max (point-max)) (pos-list (create-random-list max its)) (funs (list #'point-at-final-line-1 #'point-at-final-line-2 #'point-at-final-line-3) ) (times '()) ) (cl-loop for f in funs do (push (list f (measure-time (test-final-line-f f pos-list))) times) ) (goto-char (point-max)) (let ((sorted-times (cl-sort times #'< :key #'cadr))) (cl-loop for time in sorted-times do (insert (format "\n;; %s %0.2f" (car time) (cadr time))) )))) ;; (test-final-line 10000) ;; ^eval me ;; results on RPi3: ;; ;; point-at-final-line-3 0.08 ;; point-at-final-line-2 0.38 ;; point-at-final-line-1 0.55 ;; ;; ditto gamer computer: ;; ;; point-at-final-line-3 0.01 ;; point-at-final-line-2 0.01 ;; point-at-final-line-1 0.05 ;; ;; improvements: ;; ;; (format "%.0f%%" (- 100 (* 100 (/ 0.01 0.08)))) ; 88% ;; (format "%.0f%%" (- 100 (* 100 (/ 0.01 0.38)))) ; 97% ;; (format "%.0f%%" (- 100 (* 100 (/ 0.05 0.55)))) ; 91% (provide 'measure)