On 10/02/2022 12:13, BlindAnagram wrote:
Is there any difference in performance between these two program layouts:

   def a():
     ...
   def(b):
     c = a(b)

or

   def(b):
     def a():
       ...
     c = a(b)

I would appreciate any insights on which layout to choose in which circumstances.

The way to answer questions about performance is not to guess (often difficult or impossible), but to measure.  Profiling tools are available to see where a program spends how much of its time.  But a simpler approach might be to try to run the guts of your program 1000 or 1000000 times, and find how long it takes with each layout (not with a stopwatch 😁 but getting the program to record the start time, end time and the difference).
That said, I will venture a guess (or at least, an observation):
    def a():
        ...
is executable code.  It creates the function `a` which did not exist before (or creates the new version if it did).  This takes a certain amount of time.  In your 2nd layout, this overhead occurs every time b() is called.  So I would *guess* that this would be slower.  Of course it depends on how many times b() is called and probably on lots of other things.

But of course, performance is not the only consideration, as per Chris Angelico's answer.
Best wishes
Rob Cliffe
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to