From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 6121 invoked from network); 22 Apr 2005 14:36:20 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 22 Apr 2005 14:36:20 -0000 Received: (qmail 98549 invoked from network); 22 Apr 2005 14:36:12 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 22 Apr 2005 14:36:12 -0000 Received: (qmail 21453 invoked by alias); 22 Apr 2005 14:36:02 -0000 Mailing-List: contact zsh-users-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 8737 Received: (qmail 21429 invoked from network); 22 Apr 2005 14:36:01 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by sunsite.dk with SMTP; 22 Apr 2005 14:36:01 -0000 Received: (qmail 97497 invoked from network); 22 Apr 2005 14:36:01 -0000 Received: from ns9.hostinglmi.net (213.194.149.146) by a.mx.sunsite.dk with SMTP; 22 Apr 2005 14:35:49 -0000 Received: from 212.red-80-35-44.pooles.rima-tde.net ([80.35.44.212] helo=localhost) by ns9.hostinglmi.net with esmtpa (Exim 4.44) id 1DOzG3-0000Av-0j for zsh-users@sunsite.dk; Fri, 22 Apr 2005 16:35:51 +0200 Date: Fri, 22 Apr 2005 16:36:59 +0200 From: DervishD To: zsh-users@sunsite.dk Subject: Re: while read; problems Message-ID: <20050422143659.GC7326@DervishD> Mail-Followup-To: zsh-users@sunsite.dk References: <20050422122206.GA11327@globtel.pl> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20050422122206.GA11327@globtel.pl> User-Agent: Mutt/1.4.2.1i Organization: DervishD X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - ns9.hostinglmi.net X-AntiAbuse: Original Domain - sunsite.dk X-AntiAbuse: Originator/Caller UID/GID - [0 0] / [47 12] X-AntiAbuse: Sender Address Domain - dervishd.net X-Source: X-Source-Args: X-Source-Dir: X-Spam-Checker-Version: SpamAssassin 3.0.2 on a.mx.sunsite.dk X-Spam-Level: X-Spam-Status: No, score=-2.6 required=6.0 tests=BAYES_00 autolearn=ham version=3.0.2 X-Spam-Hits: -2.6 Hi Mariusz :) * Mariusz Gniazdowski 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 result. > 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...