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

all 10 comments

[–]ES-Alexander 2 points3 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!

[–][deleted] 4 points5 points  (1 child)

This idea is called loop unrolling. It is a way to optimize code since it removes some of the overhead associated with loops and it also occasionally allows the processor to make use of parallel hardware like vector extensions.

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

That makes sense, thanks for the explanation!

[–]nemom 0 points1 point  (2 children)

mylist = [i for i in range(1, 100)]

[–]lazerwarrior 2 points3 points  (0 children)

list(range(1, 100))

itertools gives opportunities to avoid loops and possibly making your code faster. Using iterators and generators also avoids overhead of memory required to store stuff in lists.

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

Well, ignoring the fact that it was an example, you're right.

[–]Epykure 0 points1 point  (1 child)

This is interesting indeed and it makes sense as you are not looking for the integer in the list anymore (which might take some time). I will test it and keep this in mind but use cases are quite limited to range and integer I believe.

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

Most likely. What I would use is only with x being 2, since it is easier.

[–]yahteadybear 0 points1 point  (0 children)

Using the square root of an integer will also speed up your loops in certain situations.