# 除了保存/恢復點的保存偏移的替代方案？

Q: is there an alternative to save-excursion that only saves/restores point?

(defmacro save-point (&rest body)
(cl-declare (debug t) (indent 0))
(let ((orig (cl-gensym)))
(let ((,orig (point-marker)))
(unwind-protect
(progn ,@body)
(goto-char ,orig)))))


save-excursion is written in C, so it wasn't a priori clear that this macro would be faster than the C code despite saving and restoring fewer objects. And, in fact, it's about an order of magnitude slower:

(benchmark-run-compiled 1000
(save-point t))                   ; => .0005ish

(benchmark-run-compiled 1000
(save-excursion t))               ; => .00005ish


• 這樣的 save-point 宏是否已存在？
• 如何簡化此宏以加快速度？
• 有沒有辦法彌補C代碼和elisp代碼之間的速度差異？

## 最佳答案

There is a reason that these things are saved & restored together by save-excursion. Otherwise, you had better be sure about which buffer you want to restore the value of point for etc.

My advice: use save-excursion. My guess is that it really costs nothing more than what you would code yourself to save & restore just point, and in fact will be quite a bit faster because it is coded in C.