From: "Clément Pit-Claudel" <cpitclaudel@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: emacs-devel@gnu.org
Subject: Re: Why does adding a useless copy-sequence and discarding the result make my ELisp 40 times faster?
Date: Sat, 25 Mar 2017 09:45:26 -0400 [thread overview]
Message-ID: <929e2866-26fa-c480-a51e-742e0bd3023f@gmail.com> (raw)
In-Reply-To: <83vaqxbzub.fsf@gnu.org>
On 2017-03-25 03:05, Eli Zaretskii wrote:
>> From: Clément Pit-Claudel <cpitclaudel@gmail.com> Date: Sat, 25 Mar
>> 2017 00:24:20 -0400
>>
>> On 2017-03-25 00:06, Clément Pit-Claudel wrote:
>>> * Why does running additional, supposedly useless code speed
>>> things up so much? (1)
>>
>> Interestingly, it turns out that (copy-sequence) isn't needed:
>> instead, it's enough to use (sleep-for 0.000001) (or even
>> (sleep-for cl-least-positive-normalized-float)) to get the 40x
>> speedup.
>
> Because, if you don't sleep, the process is not yet running by the
> time you get to process-send-string, and then Emacs waits for a much
> longer time for it to become running?
Quick experiments suggest this isn't the case: hoisting the (sleep) calls out of the loop and sleeping n-inner-loops times instead yields much slower code than when the sleep calls are interleaved with the process-send-string calls.
That is, I don't observe the aforementioned speedup if I sleep a single, "long" time before the loop, instead of putting a small sleep in each iteration.
Similarly, taking the process creation out of the function being timed, then adding a long sleep and a call to accept-process-output before the benchmarked code doesn't change performance significantly:
(benchmark-send-string t 200 65536 proc) → (0.013895306 0 0.0)
(benchmark-send-string nil 200 65536 proc) → (2.3035461760000002 0 0.0)
I used this code to test this:
(require 'cl-lib)
(cl-float-limits)
(defun benchmark-send-string (cargo-cult n-inner-loops size proc)
(let* ((message (make-string size ?a)))
(dotimes (_ n-inner-loops)
(when cargo-cult
(sleep-for cl-least-positive-normalized-float))
(process-send-string proc message))
(kill-process proc)))
(pcase-let ((`(,cargo-cult ,n-inner-loops ,size . ,prog) argv))
(setq cargo-cult (read cargo-cult))
(setq size (string-to-number size))
(setq n-inner-loops (string-to-number n-inner-loops))
(let* ((process-connection-type nil)
(proc (apply #'start-process "reader" nil prog))
(form `(benchmark-send-string ,cargo-cult ,n-inner-loops ,size proc)))
(sleep-for 1.0)
(accept-process-output nil 1.0)
(message "%S → %S" form (eval `(benchmark-run 1 ,form)))))
(setq argv nil)
Clément.
next prev parent reply other threads:[~2017-03-25 13:45 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-03-25 4:06 Why does adding a useless copy-sequence and discarding the result make my ELisp 40 times faster? Clément Pit-Claudel
2017-03-25 4:24 ` Clément Pit-Claudel
2017-03-25 7:05 ` Eli Zaretskii
2017-03-25 10:38 ` Andreas Politz
2017-03-25 13:49 ` Clément Pit-Claudel
2017-03-25 16:38 ` Andreas Politz
2017-03-25 17:02 ` Clément Pit-Claudel
2017-03-25 17:26 ` Eli Zaretskii
2017-03-25 17:40 ` Andreas Politz
2017-03-25 17:49 ` Eli Zaretskii
2017-03-25 17:37 ` Andreas Politz
2017-03-25 13:45 ` Clément Pit-Claudel [this message]
2017-03-25 10:47 ` Andreas Politz
2017-03-25 13:54 ` Clément Pit-Claudel
2017-03-26 13:14 ` Stephen Leake
2017-03-26 13:35 ` Clément Pit-Claudel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=929e2866-26fa-c480-a51e-742e0bd3023f@gmail.com \
--to=cpitclaudel@gmail.com \
--cc=eliz@gnu.org \
--cc=emacs-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.