Jump to content
Sign in to follow this  
Mike Torrettinni

Is variable value kept after For.. in ... do loop?

Recommended Posts

1 hour ago, Mike Torrettinni said:

I was referring to variable iterating For..in loop - in this case full array record content is copied into variable. No? At least that's what I see from the simple example above.

Yes that is correct. My argument stands.

 

Copying the full record into a local is only expensive (compared to reading a value from the record in-situ in the array) if the record is large.  For a small record, copying a handful of bytes costs no more than reading even a single byte.

 

I'm arguing against your claim that there would be a performance hit using a for in loop even for small records.  For large records there will be a hit.  Not for small records.

Edited by David Heffernan

Share this post


Link to post

Pointer shifting over an array might only be faster because the array variable itself needs to be accessed again every time - this is mostly the case when the array itself is a field in a class and you are inside of a method. Accessing say FItems[LIndex] in a loop causes access to Self.FItems all the time. Simple storing @FItems[0] in a local variable and Inc that might be faster - but only because you then have avoided the array field access, not because pointer is faster than indexing into an array.

 

Edit: even more so if you access that FItems[LIndex] multiple times - the compiler most of the time generates code to fetch FItems each and every time and does not treat it as if you stored it inside a temporary variable.

Edited by Stefan Glienke

Share this post


Link to post
1 minute ago, David Heffernan said:

Yes, we've agreed on that. The thing is that's only a performance issue (compared to direct access of the record in-situ in the array) if the record is large.  Because if the record is small, then copying a handful of bytes costs the same as reading even a single byte.

 

Yes. I have a situaition where initial simple structure has become quite extended with nested records. So, what seemed at the beginning a very elegant solution with For..in, is now becoming a little slower due to more complex structure and more extensive use. and I don't want to risk this to be performance hog for large customers (high number of records).

 

I have decided to stop using For..in, so when I code I don't need to think about it. Now I have good reason to stick with 1 style.

Except if I will open source anything, that's when I see a For..in looks much better.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×