[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Is length() optimized away in for-loop?
From: |
arnold |
Subject: |
Re: Is length() optimized away in for-loop? |
Date: |
Sun, 26 Apr 2020 07:53:25 -0600 |
User-agent: |
Heirloom mailx 12.5 7/5/10 |
Peng Yu <address@hidden> wrote:
> Hi,
>
> In the following awk code, is `length(a)` called multiple times? Or
> gawk is smart enough to know to just call `length(a)` once? Thanks.
>
> for(i=1;i<=length(a);++i) print a[i]
>
> --
> Regards,
> Peng
The call is not "optimized away". There is no anaylsis of the loop
body with respect to the loop control. If you're concerned about this,
then the way to write the code is:
j = length(a)
for (i = 1; i <= j; i++) print a[i]
Arnold