简体   繁体   中英

Does the position of python import statements affect performance

I was wondering if the position of import statements in a python program has any affect on performance. For example if I have this

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import urllib2
import json
import requests
from flask import render_template, request, Flask, session, Markup, jsonify, send_from_directory
from wit import Wit
from os import urandom
from datetime import datetime
from uuid import uuid1
from random import choice
from FAAWrapper import FAA_API
from bs4 import BeautifulSoup



def function1():
    from OpenSSL import SSL
    from fuzzywuzzy import process
    continue

def function2():
    continue

Would performance be adversely affected by calling function1() being that function1 contains import statements? Should all of my imports be placed at the top or does the import only happen once the first time the function is called?

Importing does two things:

  1. If there is no sys.modules entry yet, find and load the module; if Python code, executing the top-level code produces the namespace for that module. This step is skipped if the module has already been loaded.

  2. Bind a name in the current namespace to the imported object. import foo sets the name foo . from foo import bar binds the name bar , etc.

Now, local names (in functions) have a speed advantage in that Python stores these in a C array and uses indices in the bytecode to reference them. Global names are stored in a dictionary and have a small hashing overhead each time you do a lookup.

So importing something into a function results in a local, accessing which is faster than referencing a global. This is offset by the hash lookup in sys.modules each time your function runs so only if the name is used in a loop would you notice this.

However, you should only make such optimisations if that code is used on a critical path, in code that is executed a lot. You are paying a maintenance price by hiding imports in functions, and that cost needs to be weighed against the (marginal) speed benefits.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM