Phil Harman wrote: > Roland Mainz wrote: > > Peter Tribble wrote: > >> On Thu, Sep 10, 2009 at 8:38 AM, Raymond Wong<raym...@gmail.com> wrote: > >> > > [snip] > > > >> while read a > >> do > >> line=$(($line+1)) > >> done > > > > This will run even faster if you remove the '$', put the assignment > > inside the arithmetric expression and declare "line" as integer > > variable, e.g. ... > > -- snip -- > > integer line=0 > > > > while read a ; do > > (( line=line+1 )) > > done > > -- snip -- > > These optimisations make sure that the shell won't do any unneccesary > > string-to-integer and integer-to-string conversions which consume lots > > of time. For floating-point values they are even a huge problem since > > strings used base10 and the internal IEEE754 floats use base2 and the > > string-to-float and float-to-string conversions trigger rounding > > problems... > > Whilst true, it pales compared to the inefficiency of executing external > commands.
Well, it depends how to use the external commands... the "bc"/"dc" version would be a lot faster if you queue all commands in a string and call the calculator only once... ... but for POSIX shells with airthmetric expressions this has more or less become obsolete... > I've always preferred the (( a = a + 1 )) construct because it > is slightly more readable. Nice to know that there's a performance > reason for doing it too. Thanks for sharing. No problem... :-) ... and http://www.opensolaris.org/os/project/shell/shellstyle/#math has more of those things... ---- Bye, Roland -- __ . . __ (o.\ \/ /.o) roland.ma...@nrubsig.org \__\/\/__/ MPEG specialist, C&&JAVA&&Sun&&Unix programmer /O /==\ O\ TEL +49 641 3992797 (;O/ \/ \O;) _______________________________________________ perf-discuss mailing list perf-discuss@opensolaris.org