help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: point-at-final-line


From: Emanuel Berg
Subject: Re: point-at-final-line
Date: Tue, 30 Jan 2018 03:18:18 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)

Eli Zaretskii wrote:

> I'm saying that you _can_ measure this: just
> run each function many times in a loop, and
> then divide the time by the number of
> iterations. This is a standard method of
> timing short code fragments.

I don't know about that method. The reason is
it seems to favor your suggestion. And not by
a little! Here are the results in seconds for
10 000 runs in a buffer of 55 lines:

    ;; point-at-final-line-3   0.08
    ;; point-at-final-line-2   0.38
    ;; point-at-final-line-1   0.55

The entire code yanked:

;; This file: http://user.it.uu.se/~embe8573/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 1)) ))

(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.
Not mine: 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)

;; point-at-final-line-3   0.08
;; point-at-final-line-2   0.38
;; point-at-final-line-1   0.55

-- 
underground experts united
http://user.it.uu.se/~embe8573


reply via email to

[Prev in Thread] Current Thread [Next in Thread]