Search code examples
pythonimportcall

Is one faster than another? func() or module.func()


I see that from x import * is discouraged all over the place. Corrupts naming space, etc.

So I'm inclined to use from . import x, and when I need to use the functions, I'll call x.func() instead of just using func().

The speed difference is probably very little, but I still want to know how much it might impact the performance? So that I can keep the good habit without needing to worry about other things.


Solution

  • It has practically no impact:

    >>> import timeit
    >>> timeit.timeit('math.pow(1, 1)', 'import math')
    0.20310196322982677
    >>> timeit.timeit('pow(1, 1)', 'from math import pow')
    0.19039931574786806
    

    Note I picked a function that would have very little run time so that any difference would be magnified.