The underlying function is find_newline in emacs/src/search.c. It uses a cache of sections of text that contain no newlines, so the function can skip over them quickly. The cache is controlled by the variable cache-long-scans, whose doc string says:
Non-nil means that Emacs should use caches in attempt to speedup buffer scans.
There is no reason to set ...
This interactive function times format-mode-line.
(defmacro mode-line-benchmark-elapse-time (&rest forms)
"Return the time in seconds elapsed for execution of FORMS."
(declare (indent 0) (debug t))
(let ((t1 (make-symbol "t1")))
(setq ,t1 (current-time))
(float-time (time-since ,t1))...