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

Re: while read; problems

    Hi Mariusz :)

 * Mariusz Gniazdowski <cellsan@xxxxxxxxxx> dixit:
> function count {
>         local line max=0 idx=0 maxidx=0
>         while read line; do
>                 line=${#line}
>                 (( idx++ ))
>                 [[ $max -lt $line ]] && { max=$line; maxidx=$idx; }
>         done < "$1"
>         echo "$max in line nr: $maxidx" 
> }                         

> First problem: this runs much faster on bash.

    Don't know why :? Here your function applied to a binary file
31MB in size results in 26 seconds in zsh, 14 in bash, so it's true,
the difference is noticeable. Unfortunately not both shells consider
a 'line' the same in a binary file (more in this subject below), and
bash may be reading longer lines and doing much less I/O :???
> Second problem: zsh results differ from run to run. I had results like:
> 6097 in line nr: 169
> 6553 in line nr: 300
> etc.

    Not reproduceable here. I always have the same, consistent
> This file was binary, but i did'nt output it, or pass it to any
> function (in variable). It was only: readed line by line and
> counted.

    The problem with binary files is: what's a line? Some arbitrary
stream of characters until a '\n' is found? Some arbitary stream of
characters that ends in a '\0'? Your measurement is incorrect because
you don't make sure what's in $IFS, and 'read' uses $IFS. You cannot
do such things on binary files from a shell script.

    Just out of curiosity: why are you messing with lines in a binary
file? Maybe you can do what you want in a faster and more portable
way. If you tell us, maybe we can help :)

    Raúl Núñez de Arenas Coronado

Linux Registered User 88736
http://www.dervishd.net & http://www.pleyades.net/
It's my PC and I'll cry if I want to...

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