Performance of Python Code

Here’s a snippet from my Python Template.

Import Functions
from sys import stdin, stdout, stderr, setrecursionlimit
from math import pi, sqrt, gcd, ceil, floor, log2, log10, factorial
from math import cos, acos, tan, atan, atan2, sin, asin, radians, degrees, hypot
from bisect import insort, insort_left, insort_right, bisect_left, bisect_right, bisect
from functools import reduce
from itertools import combinations, combinations_with_replacement, permutations
from fractions import Fraction
from random import choice, getrandbits, randint, random, randrange, shuffle
from re import compile, findall, escape, search, match
from statistics import mean, median, mode
from heapq import heapify, heappop, heappush, heappushpop, heapreplace, merge, nlargest, nsmallest
from collections import deque, OrderedDict, defaultdict
from collections import Counter, namedtuple, ChainMap, UserDict, UserList, UserString
# from numpy import dot, trace, argmax, argmin, array, cumprod, cumsum, matmul

It is of course better to write them like this

Better way (to save time)
import sys
import math
import bisect
import functools
import itertools
import fractions
import random
import re
import statistics
import heapq
import collections
# import numpy

One of the best methods to import

Best way (saves time)
from sys import *
from math import *
from bisect import *
from functools import *
from itertools import *
from fractions import *
from random import *
from re import *
from statistics import *
from heapq import *
from collections import *
# from numpy import *

Coming to the performance issues, I am still unable to figure out which one is the best. Can anyone who is familiar with Python share their thoughts or maybe along with proofs (ex: execution times of various codes).
PS: Consider only the performance during execution, ignore the time taken to type

These are my findings, correct me if I’m wrong.

  • First is the namespace pollution. Importing all the functions into the global namespace risks name collisions, because we may not be aware of all the methods in the module, so we might re declare the same names. Hence, the risk of overriding the variables/functions etc always persist with the import * practice. Situation where I faced name collision many times is:


Whether you import a module or import a function from a module, Python will parse the whole module. Either way the module is imported. “Importing a function” is nothing more than binding the function to a name. In fact import module is less work for interpreter than from module import func.

I’ve also verified the above statement by taking a piece of code.

Time taken when I’ve used from module import * is → 1.0141
Time taken when I’ve used from module import func is → 1.0206
Note: Since I’ve executed this on gfg, output may vary depending on the system or server load. But I’ve run the code multiple times but still found that 1st type is taking less time than 2nd type.

In this there are many other reasons why import * can’t be used-> link

Hope its helpful. Thank you. :v: