9

When searching for a solution, it's common to come across several methods. I often use the solution that most closely aligns with syntax I'm familiar with. But sometimes the far-and-away most upvoted solution involves importing a module new to me, as in this thread.

I'm already importing various modules in large script that will be looping 50K times. Does importing additional modules affect processing time, or otherwise affect the script's efficiency? Do I need to worry about the size of the module being called? Seeking guidance on whether, generally, it's worth the extra time/effort to find solutions using methods contained in modules I'm already using.

Community
  • 1
  • 1
joechoj
  • 1,339
  • 10
  • 12
  • Have you profiled your code yet? – Ignacio Vazquez-Abrams Sep 28 '16 at 09:33
  • Importing modules is insignificant compared to what your code does. Focusing on importing cost is not worth your time. Focus on what the module can do for your problem instead. – Martijn Pieters Sep 28 '16 at 09:41
  • @Ignacio: No, I haven't (and thanks for introducing me to the term). I'm still building the basic architecture. On first run, a single pass took ~11 seconds. I've added a bit more functionality since, but on the other hand I'm certain there's lots of room for improvement. – joechoj Sep 28 '16 at 09:55

1 Answers1

14

Every bytecode in Python affects performance. However, unless that code is on a critical path and repeated a high number of times, the effect is so small as to not matter.

Using import consists of two distinct steps: loading the module (done just once), and binding names (where the imported name is added to your namespace to refer to something loaded by the module, or the module object itself). Binding names is almost costless. Because loading a module happens just once, it won't affect your performance.

Focus instead on what the module functionality can do to help you solve your problem efficiently.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343