bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gawk] Does gawk optimize length(row) in for-loop?


From: Andrew J. Schorr
Subject: Re: [bug-gawk] Does gawk optimize length(row) in for-loop?
Date: Sat, 25 Mar 2017 14:18:20 -0400
User-agent: Mutt/1.5.21 (2010-09-15)

On Sat, Mar 25, 2017 at 01:00:39PM -0500, Peng Yu wrote:
> Hi, I have the following two code snippets. The first one takes the
> length() out of the loop. I am wondering whether gawk does any
> optimization for case 2 so that length() is only called once. Or
> length() will be called multiple times in case 2?
> 
> # code snippet 1
>     nf = length(row)
>     for(i=dat_col; i<=nf; ++i) {
>       data[i] += row[i]
>     }
> 
> # code snippet 2
>     for(i=dat_col; i<=length(row); ++i) {
>       data[i] += row[i]
>     }

It does not and cannot optimize this, because it doesn't know whether the
loop body may create new entries in the row array. How does gawk
know whether all the indices dat_col through length(row) exist already
in the row array?

If you do know that they exist, then it's probably wiser to write:
   for (i in row)
      data[i] += row[i]
But this loses the (desirable?) side-effect of creating empty row[i]
entries for indices that are not already in the array.

In general, you can use the gawk debugger's "dump"  command to see how the code
is interpreted. Please see the manual to learn how to use the debugger.

Regards,
Andy



reply via email to

[Prev in Thread] Current Thread [Next in Thread]