Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Fwd: Re: Brace expansion performance




On 22/03/2010 18.03, Nikolai Weibull wrote:
On Mon, Mar 22, 2010 at 17:24, Radoulov, Dimitre<cichomitiko@xxxxxxxxx>  wrote:

does anybody know why the zsh brace expansion (?) performs so ...
differently?
Ouch.

Zsh is faster for

for (( i = 1; i<= 300000; i++ )); do :; done,

though.

Yes,
it's not the expansion by itself:

% time bash --norc  -c ': {1..300000}'
bash --norc -c ': {1..300000}'  0.76s user 0.54s system 95% cpu 1.357 total

% time zsh -fc ': {1..300000}'
zsh -fc ': {1..300000}'  0.12s user 0.16s system 96% cpu 0.300 total

It's not the for loop:

% time bash --norc -c 'for ((;++i<=300000;)); do :;done'
bash --norc -c 'for ((;++i<=300000;)); do :;done' 2.62s user 1.99s system 89% cpu 5.135 total

% time zsh -fc 'for ((;++i<=300000;)); do :;done'
zsh -fc 'for ((;++i<=300000;)); do :;done' 0.83s user 1.94s system 82% cpu 3.345 total

It seams that it's that particular combination with the for loop:

% strace -c zsh -fc 'for i in {1..1000}; do :;done'
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
 69.44    0.063650          21      3003           rt_sigprocmask
 15.84    0.014518          15       967       960 fcntl64
  7.21    0.006606          21       317       286 open
  1.69    0.001550          39        40           close
  1.45    0.001328          30        45           mmap2
  1.03    0.000942          32        29           fstat64
  0.95    0.000870          40        22           read
  0.53    0.000483          54         9           munmap
  0.51    0.000472         236         2           readlink
  0.51    0.000470          94         5           dup
  0.44    0.000400         400         1           time
  0.41    0.000378          34        11        11 access
  0.00    0.000000           0         1           execve
  0.00    0.000000           0         1           getpid
  0.00    0.000000           0         3           alarm
  0.00    0.000000           0         1           pipe
  0.00    0.000000           0         3           brk
  0.00    0.000000           0         6         1 ioctl
  0.00    0.000000           0         1           getppid
  0.00    0.000000           0         1           getrusage
  0.00    0.000000           0         1           gettimeofday
  0.00    0.000000           0         2           uname
  0.00    0.000000           0        12           mprotect
  0.00    0.000000           0         4           _llseek
  0.00    0.000000           0         7           rt_sigaction
  0.00    0.000000           0        16           getrlimit
  0.00    0.000000           0        20        18 stat64
  0.00    0.000000           0         2           getuid32
  0.00    0.000000           0         1           getgid32
  0.00    0.000000           0         1           geteuid32
  0.00    0.000000           0         1           getegid32
  0.00    0.000000           0         1           set_thread_area
  0.00    0.000000           0         2           socket
  0.00    0.000000           0         2         2 connect
------ ----------- ----------- --------- --------- ----------------
100.00    0.091667                  4540      1278 total


% strace -c bash --norc -c 'for i in {1..1000}; do :;done'
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
 42.65    0.006292          43       147       125 open
 24.51    0.003615        3615         1           execve
 12.77    0.001884          31        60           brk
  5.58    0.000823         274         3           munmap
  5.49    0.000810          31        26           mmap2
  4.92    0.000726          33        22           close
  4.07    0.000601          30        20           fstat64
  0.00    0.000000           0         7           read
  0.00    0.000000           0         1           time
  0.00    0.000000           0         1           getpid
  0.00    0.000000           0         5         5 access
  0.00    0.000000           0         1           getppid
  0.00    0.000000           0         1           getpgrp
  0.00    0.000000           0         1           gettimeofday
  0.00    0.000000           0         1           uname
  0.00    0.000000           0         7           mprotect
  0.00    0.000000           0         8           rt_sigaction
  0.00    0.000000           0         6           rt_sigprocmask
  0.00    0.000000           0         1           getrlimit
  0.00    0.000000           0        27        22 stat64
  0.00    0.000000           0         1           getuid32
  0.00    0.000000           0         1           getgid32
  0.00    0.000000           0         1           geteuid32
  0.00    0.000000           0         1           getegid32
  0.00    0.000000           0         1           getgroups32
  0.00    0.000000           0         1           set_thread_area
------ ----------- ----------- --------- --------- ----------------
100.00    0.014751                   352       152 total




On 22/03/2010 18.22, Bart Schaefer wrote:

[...]
First make sure "zsh -c" isn't reading some expensive init files that
bash/ksh aren't, but:

I'm pretty sure zsh is actually allocating an array of 300000 integers
during expansion of that expression, and probably copying it a few times.

schaefer[633] strace bash -c 'echo $BASH_VERSION;echo $BASH_VERSION;for i in {1..300000}; do :;done' |&  wc
     322    1262   17792
schaefer[634] strace Src/zsh -fc 'echo $ZSH_VERSION;for i in {1..300000}; do :;done' |&  wc
  901381 5408181 43590584

Yes, thanks!
Could this explain such a big difference in the timings?



Regards
Dimitre



Messages sorted by: Reverse Date, Date, Thread, Author