* why is reverse a string in-place so much slower than a vector?
@ 2014-04-25 8:12 Leo Liu
2014-04-25 8:17 ` Leo Liu
0 siblings, 1 reply; 7+ messages in thread
From: Leo Liu @ 2014-04-25 8:12 UTC (permalink / raw)
To: emacs-devel
(defun rev (a)
(let ((l (length a)))
(dotimes (i (floor l 2) a)
(cl-rotatef (aref a i) (aref a (1- (- l i)))))))
--
Sent from my Emacs
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: why is reverse a string in-place so much slower than a vector?
2014-04-25 8:12 why is reverse a string in-place so much slower than a vector? Leo Liu
@ 2014-04-25 8:17 ` Leo Liu
2014-04-25 8:55 ` Rüdiger Sonderfeld
2014-04-25 9:14 ` Andreas Schwab
0 siblings, 2 replies; 7+ messages in thread
From: Leo Liu @ 2014-04-25 8:17 UTC (permalink / raw)
To: emacs-devel
On 2014-04-25 16:12 +0800, Leo Liu wrote:
> (defun rev (a)
> (let ((l (length a)))
> (dotimes (i (floor l 2) a)
> (cl-rotatef (aref a i) (aref a (1- (- l i)))))))
Sorry didn't finish the post.
Assume we are in a buffer visiting subr.el:
(benchmark-run 1 (rev (buffer-string)))
(11.774416366 1 0.060193897999999635)
(benchmark-run 1 (rev (cl-coerce (buffer-string) 'vector)))
(0.067042623 0 0.0)
So why is this so much slower on string?
Leo
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: why is reverse a string in-place so much slower than a vector?
2014-04-25 8:17 ` Leo Liu
@ 2014-04-25 8:55 ` Rüdiger Sonderfeld
2014-04-25 9:14 ` Andreas Schwab
1 sibling, 0 replies; 7+ messages in thread
From: Rüdiger Sonderfeld @ 2014-04-25 8:55 UTC (permalink / raw)
To: emacs-devel; +Cc: Leo Liu
On Friday 25 April 2014 16:17:42 Leo Liu wrote:
> So why is this so much slower on string?
This is a bit of a guess. But I think the problem is that `aref' has to do
more complicated operations for multi-byte strings to correctly decode them.
While for a vector it can simply access every single bytes.
(with-current-buffer "subr.el" (multibyte-string-p (buffer-substring (point-
min) (point-max))))
-> t
Regards,
Rüdiger
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: why is reverse a string in-place so much slower than a vector?
2014-04-25 8:17 ` Leo Liu
2014-04-25 8:55 ` Rüdiger Sonderfeld
@ 2014-04-25 9:14 ` Andreas Schwab
2014-04-25 9:35 ` Leo Liu
1 sibling, 1 reply; 7+ messages in thread
From: Andreas Schwab @ 2014-04-25 9:14 UTC (permalink / raw)
To: Leo Liu; +Cc: emacs-devel
Leo Liu <sdl.web@gmail.com> writes:
> Assume we are in a buffer visiting subr.el:
>
> (benchmark-run 1 (rev (buffer-string)))
> (11.774416366 1 0.060193897999999635)
>
> (benchmark-run 1 (rev (cl-coerce (buffer-string) 'vector)))
> (0.067042623 0 0.0)
>
> So why is this so much slower on string?
String random access has linear complexity: there is a single element
cache for the last known char->byte mapping for the last accessed
string, and the runtime depends on the distance from this point. Your
rev function represents the worst case behaviour.
Andreas.
--
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5
"And now for something completely different."
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: why is reverse a string in-place so much slower than a vector?
2014-04-25 9:14 ` Andreas Schwab
@ 2014-04-25 9:35 ` Leo Liu
2014-04-25 11:00 ` Andreas Schwab
0 siblings, 1 reply; 7+ messages in thread
From: Leo Liu @ 2014-04-25 9:35 UTC (permalink / raw)
To: Andreas Schwab; +Cc: emacs-devel
On 2014-04-25 11:14 +0200, Andreas Schwab wrote:
> String random access has linear complexity: there is a single element
> cache for the last known char->byte mapping for the last accessed
> string, and the runtime depends on the distance from this point. Your
> rev function represents the worst case behaviour.
Thanks for this piece of critical information. I guess this is all due
to `string_char_to_byte'?
Leo
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: why is reverse a string in-place so much slower than a vector?
2014-04-25 9:35 ` Leo Liu
@ 2014-04-25 11:00 ` Andreas Schwab
2014-04-26 0:19 ` Leo Liu
0 siblings, 1 reply; 7+ messages in thread
From: Andreas Schwab @ 2014-04-25 11:00 UTC (permalink / raw)
To: Leo Liu; +Cc: emacs-devel
Leo Liu <sdl.web@gmail.com> writes:
> I guess this is all due to `string_char_to_byte'?
No, it is all due to the variable length nature of the UTF-8 encoding.
Andreas.
--
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5
"And now for something completely different."
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2014-04-26 0:19 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-04-25 8:12 why is reverse a string in-place so much slower than a vector? Leo Liu
2014-04-25 8:17 ` Leo Liu
2014-04-25 8:55 ` Rüdiger Sonderfeld
2014-04-25 9:14 ` Andreas Schwab
2014-04-25 9:35 ` Leo Liu
2014-04-25 11:00 ` Andreas Schwab
2014-04-26 0:19 ` Leo Liu
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.