emacs-devel
[Top][All Lists]
Advanced

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

Re: Temporarily select-window, without updating mode-line face and curso


From: Stefan Kangas
Subject: Re: Temporarily select-window, without updating mode-line face and cursor fill?
Date: Wed, 5 May 2021 14:32:27 -0500

Eli Zaretskii <eliz@gnu.org> writes:

>> > We really should simply speed up `line-number-at-pos`.
>> > It shouldn't be hard.  See below what I do in nlinum.el.
>>
>> Interesting, it seems much faster at least in this simplistic test:
>>
>>     (benchmark-run 100000 (line-number-at-pos))
>>     => (5.868677411999999 0 0.0)
>>
>>     (benchmark-run 100000 (line-number-at-pos))
>>     => (0.9772498589999999 1 0.5954873589998897)
>
> Doesn't this measure the line number of the same position?  If so,
> that's about as favorable benchmark for caching as possible, no?

Yup.

Here's another one I threw together, tested here in src/keymap.c:

(defvar bench-run-times 10000)

(defun line-number-at-pos-benchmark (lines)
  (let (results)
    (dolist (fun '(line-number-at-pos nlinum--line-number-at-pos))
      (goto-char (/ (point-max) 2))
      (save-excursion
        (push
         (cons fun
               (cons lines
                     (benchmark-run bench-run-times
                       (let ((n (- (random (/ lines 2))
                                   (random (/ lines 2)))))
                         (forward-line n))
                       (funcall fun))))
         results)))
    results))

(defun run-bench (from to inc)
  (with-current-buffer (get-buffer-create "*Bench-Results*")
    (erase-buffer))
  (mapc
   (lambda (res)
     ;; ((nlinum--line-number-at-pos 0 0.93488997 0 0.0)
     ;;  (line-number-at-pos 0 1.0096957629999999 0 0.0))
     (let ((a (car res))
           (b (cadr res)))
      (with-current-buffer (get-buffer-create "*Bench-Results*")
        (insert (format (concat "%3s: "
                                "%s %6.3f %2d %6.3f | "
                                "%s %6.3f %2d %6.3f  [%5.2f %% faster]\n")
                        (cadr b)
                        (car a) (caddr a) (cadddr a) (car (cddddr a))
                        (car b) (caddr b) (cadddr b) (car (cddddr b))
                        (* (- 1.0 (/ (caddr a) (caddr b))) 100))))))
   (mapcar #'line-number-at-pos-benchmark
           (number-sequence from to inc)))
  (pop-to-buffer "*Bench-Results*"))

Now, (run-bench 0 100 5) gives me:

  0: nlinum--line-number-at-pos  0.963  0  0.000 | line-number-at-pos
1.107  0  0.000  [13.04 % faster]
  5: nlinum--line-number-at-pos  0.027  0  0.000 | line-number-at-pos
0.118  0  0.000  [77.13 % faster]
 10: nlinum--line-number-at-pos  0.030  0  0.000 | line-number-at-pos
0.174  0  0.000  [82.56 % faster]
 15: nlinum--line-number-at-pos  0.031  0  0.000 | line-number-at-pos
0.170  0  0.000  [81.80 % faster]
 20: nlinum--line-number-at-pos  0.033  0  0.000 | line-number-at-pos
0.209  0  0.000  [84.26 % faster]
 25: nlinum--line-number-at-pos  0.035  0  0.000 | line-number-at-pos
0.170  0  0.000  [79.45 % faster]
 30: nlinum--line-number-at-pos  0.037  0  0.000 | line-number-at-pos
0.109  0  0.000  [65.96 % faster]
 35: nlinum--line-number-at-pos  0.039  0  0.000 | line-number-at-pos
0.094  0  0.000  [58.57 % faster]
 40: nlinum--line-number-at-pos  0.040  0  0.000 | line-number-at-pos
0.077  0  0.000  [48.01 % faster]
 45: nlinum--line-number-at-pos  0.044  0  0.000 | line-number-at-pos
0.117  0  0.000  [62.09 % faster]
 50: nlinum--line-number-at-pos  0.044  0  0.000 | line-number-at-pos
0.181  0  0.000  [75.77 % faster]
 55: nlinum--line-number-at-pos  0.046  0  0.000 | line-number-at-pos
0.106  0  0.000  [57.01 % faster]
 60: nlinum--line-number-at-pos  0.048  0  0.000 | line-number-at-pos
0.085  0  0.000  [43.54 % faster]
 65: nlinum--line-number-at-pos  0.052  0  0.000 | line-number-at-pos
0.114  0  0.000  [54.87 % faster]
 70: nlinum--line-number-at-pos  0.052  0  0.000 | line-number-at-pos
0.115  0  0.000  [54.48 % faster]
 75: nlinum--line-number-at-pos  0.055  0  0.000 | line-number-at-pos
0.208  0  0.000  [73.71 % faster]
 80: nlinum--line-number-at-pos  0.058  0  0.000 | line-number-at-pos
0.185  0  0.000  [68.89 % faster]
 85: nlinum--line-number-at-pos  0.059  0  0.000 | line-number-at-pos
0.157  0  0.000  [62.57 % faster]
 90: nlinum--line-number-at-pos  0.061  0  0.000 | line-number-at-pos
0.185  0  0.000  [67.22 % faster]
 95: nlinum--line-number-at-pos  0.063  0  0.000 | line-number-at-pos
0.102  0  0.000  [38.34 % faster]
100: nlinum--line-number-at-pos  0.064  0  0.000 | line-number-at-pos
0.729  1  0.597  [91.24 % faster]

However, if I ramp up bench-run-times to 100000 I start seeing GC hits,
and then nlinum--line-number-at-pos can be up to 10 times slower
instead...



reply via email to

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