zsh-workers
 help / color / mirror / code / Atom feed
* accessing array by index very slow
@ 2023-04-16  9:03 Stephane Chazelas
  2023-04-16  9:10 ` Roman Perepelitsa
  0 siblings, 1 reply; 7+ messages in thread
From: Stephane Chazelas @ 2023-04-16  9:03 UTC (permalink / raw)
  To: Zsh hackers list

$ zsh -c 'a=($(seq 1000000)); time (for ((i=0;i<10000;i++)) { : "${a[1]}"; })'
( for ((i=0; i<10000; i++)) do; : "${a[1]}"; done; )  0.07s user 0.03s system 96% cpu 0.102 total

$ zsh -c 'a=($(seq 1000000)); time (for ((i=0;i<10000;i++)) { : "${a[999999]}"; })'
( for ((i=0; i<10000; i++)) do; : "${a[999999]}"; done; )  14.56s user 0.06s system 99% cpu 14.616 total

Accessing array elements via their index is very slow,
especially for large index values.

Comparing with other shells:

~$ bash -c 'a=($(seq 1000000)); time (for ((i=0;i<10000;i++)) { : "${a[999999]}"; })'

real    0m0.078s
user    0m0.077s
sys     0m0.000s
~$ ksh -c 'a=($(seq 1000000)); time (for ((i=0;i<10000;i++)) { : "${a[999999]}"; })'

real    0m00.02s
user    0m00.02s
sys     0m00.00s

cachegrind reveals the time is spent in arrlen_ge(), as zsh
needs to walk through the whole array to determine whether
999999 is past the end of the array or not.

Why doesn't zsh record the length of arrays to avoid that
gymnastic (and speed up $#array expansion)?

-- 
Stephane


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-04-30 19:27 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-16  9:03 accessing array by index very slow Stephane Chazelas
2023-04-16  9:10 ` Roman Perepelitsa
2023-04-16  9:19   ` Sebastian Gniazdowski
2023-04-16 15:27     ` Bart Schaefer
2023-04-16 22:03       ` Mikael Magnusson
2023-04-18  2:43         ` Bart Schaefer
2023-04-30 19:26           ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).