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: Wolfgang Laun
Subject: Re: [bug-gawk] Does gawk optimize length(row) in for-loop?
Date: Sat, 25 Mar 2017 21:29:34 +0100

There is no reason to try to "optimize" the length() call by assigning this to a variable. Implementations will not count the array elements each time this function is called. A crude test using gawk shows no noticeable difference.

Other replies emphasize the robustness of using length in for; I'd also say that the 2nd version expresses your intent more clearly.

-W

On 25 March 2017 at 19:00, Peng Yu <address@hidden> 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]
    }

--
Regards,
Peng



reply via email to

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