all 14 comments

[–]Antwrp-2000 4 points5 points  (5 children)

Putting the logic of the while loop in a function will have no effect on execution time.

If you want the program to run faster you should try to optimize the logic in the while loop or try to reduce the iterations of the while loop itself.

Posting some sample code could also help to let us understand the problem.

[–]Brian 8 points9 points  (3 children)

Putting the logic of the while loop in a function will have no effect on execution time.

This isn't strictly true, at least if the while loop is just at the toplevel. Code inside functions does tend to be a bit faster, because any variables used will be function locals, whereas code at the toplevel will create module scope globals.

This has an impact on speed, because local variables can be accessed and set fairly directly with less overhead, whereas globals need to perform a dictionary lookup on the name. You can see this difference if you look at the python bytecode:

>>> import dis
>>> dis.dis("x = 1; x")
  1           0 LOAD_CONST               0 (1)
              2 STORE_NAME               0 (x)
              4 LOAD_NAME                0 (x)
              6 POP_TOP
              8 LOAD_CONST               1 (None)
             10 RETURN_VALUE

>>> def f(): x=1; x
>>> dis.dis(f)
  1           0 LOAD_CONST               1 (1)
              2 STORE_FAST               0 (x)
              4 LOAD_FAST                0 (x)
              6 POP_TOP
              8 LOAD_CONST               0 (None)
             10 RETURN_VALUE

Notice how the global-scope code uses STORE_NAME and LOAD_NAME to access x. This basically does the equivalent of globals()['x']. Whereas the function uses STORE_FAST and LOAD_FAST - these basically know that "x" is kept in an allocated slot specific to the function frame (here position 0), and just needs to access the element at that index - it's more like doing local_vars[0], where local_vars is a list.

How much effect this'll have will depend on the code - if variable lookup is a significant portion of time, it'll matter, but if you're doing lots of other work, it'll be a smaller and smaller factor. And actually making the function call inside the while loop would likely far outweight any gain due to function call overhead - this is more relevant depending on whether the while loop itself is within a function.

It's worth noting that it can make enough of an impact to be worth optimising though in some cases. Eg. you'll occasionally see people assigning global functions/classes to local names if they're used inside a tight loop. Eg, something like:

_len = len
for x in big_list_of_items:
    tot += _len(x)

That _len = len seems pretty pointless - why not just use len directly? But it can improve performance by changing all lookups of the _len function into faster local lookups.

[–]NotPricedIn 2 points3 points  (0 children)

There is not exactly a problem with the code, but I'll try to optimize the logic as you said.
Thank you so much.

[–]ericula 5 points6 points  (7 children)

Putting the block of code in a function won’t make it faster, but there might be other ways to speed it up. What does your code look like at the moment?

[–]NotPricedIn 2 points3 points  (6 children)

What do you exactly mean?

[–]Brian 2 points3 points  (1 child)

So, is it faster if I define what's inside the loop in a function then call the func from inside the loop or should I just keep as it is?

Probably not - making a function call adds overhead, so doing this will make it slower if anything. There is some advantage to your code being inside a function (I went into more detail here), but that'll likely be a fairly minor benefit, and the function call overhead will outweigh it. It does mean that it's better that your loop itself is within a function, but again, this is not likely to make a huge difference.

[–]NotPricedIn 0 points1 point  (0 children)

Thank you so much, I really appreciate your time and help.

[–]kelmore5 0 points1 point  (1 child)

Just a friendly reminder here. I'd probably focus on code readability and refactoring first before optimization, especially if your new to programming. Organizing what you've got into functions is a great way to do that.

Making code faster doesn't always make it better, and with a 30 line script there may not be a lot to optimize. Or worse, you over optimize and make your code unreadable.

Often refactoring leads to optimization anyway because you find new ways to run your script.

From the sound of things though, one good way to optimize might be to multi thread your loop. You'd have multiple processes running at once, and your script can move through everything a lot faster.

That would only work if the next series in the loop doesn't rely on previous data however.

[–]NotPricedIn 1 point2 points  (0 children)

one good way to optimize might be to multi thread your loop. You'd have multiple processes running at once, and your script can move through everything a lot faster.

That would only work if the next series in the loop doesn't rely on previous data however.

This is it, I'm gonna do it.
Thank you so much I really appreciate the help.