Alex Rivera | Logout

function definition inside another function definition -slow?

Asked 2011-10-19T15:12:47.137
9

Look at two ways of structuring my functions:

class myClass:
    def _myFunc(self):
        pass

    def myFunc2(self):
        self._myFunc()

class myClass:
    def myFunc2(self):
        def myFunc():
            pass

        myFunc()

Will the second option be slower? I only need to call myFunc from myFunc2, so 'd like to hide it from my module documentation, I could use an underscore for that, but I thought it would be cleaner to have it inside the function. On the other hand I might need to call myFunc2 few hundred times per second, so "redefining" myFunc when calling myFunc2 each time might be slow... is that a good guess?

Edit
Report

1 Answer

8

Using Python 2.6.5 on 64-bit Ubuntu, there is no discernible difference:

# version 1
In [2]: %timeit c1.myFunc2()
1000000 loops, best of 3: 461 ns per loop

# version 2
In [3]: %timeit c2.myFunc2()
1000000 loops, best of 3: 464 ns per loop
answered 2011-10-19T15:15:14.967

Your Answer