Subscribe Bookmark RSS Feed

Is it more efficient to assign a list size to variable instead of frequently using N Items function?

shaira

Contributor

Joined:

May 4, 2017

Hi,

I frequently use the size of user-input lists to end "for loops." I usually just use

For (i=1, i<=Nitems(list), i++,
"Blah Blah"
)

If I have many for loops, is it more efficient to declare A=Nitems(list) and call this variable in all my for loops?

 

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
markbailey

Staff

Joined:

Jun 23, 2011

Solution

It will be more efficient to store the result from the first function call than to call the function again. On the other hand, I rarely bother because the script is often so fast it is not worth the savings.

You can profile your code by calling Tick Seconds() or HP Time() before and after your test code and examine the difference in time.

Learn it once, use it forever!
2 REPLIES
markbailey

Staff

Joined:

Jun 23, 2011

Solution

It will be more efficient to store the result from the first function call than to call the function again. On the other hand, I rarely bother because the script is often so fast it is not worth the savings.

You can profile your code by calling Tick Seconds() or HP Time() before and after your test code and examine the difference in time.

Learn it once, use it forever!
shaira

Contributor

Joined:

May 4, 2017

Thanks! It was just a question out of curiosity. Thanks for teaching me about Tick Seconds() and HP Time().

It's good to learn something new.