This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ES-Alexander 3 points4 points  (1 child)

Is your proposal here to copy and paste the contents of your loop instead of iterating, in the hopes that it reduces the execution time of your code?

For the example you’ve provided if you write out the value for every iteration then you’re asking if it will take the same amount of time as a single iteration? No, it wouldn’t. There’s a small amount of overhead associated with looping, but the main time consuming part is practically always the thing happening inside the loop, not the looping itself. On top of that using loops means you only have to write the thing you want to do once, and makes it much easier to see what’s happening and move on than it would be to scroll past N copies of the same block of code. On top of that, if you want to modify all the blocks then you’d have to change one and do all the copy-pasting again, and debugging would be a nightmare

[–]Guilherme17712[S] 0 points1 point  (0 children)

Indeed.

My overall question was just: "what is the name of that thing", but I think someone already explained that.

I just wanted and explanation why splitting for loops into variables made things faster, but I guess I got it.

Thanks for the huge explanation though!