简体   繁体   中英

Is there a performance difference between imported functions and classes?

Is there a difference in performance between these two examples:

Example 1:

class Calculator:
    def add(self, a, b):
        return a+b
import Calculator

calc = Calculator()
print(calc.add(1, 2))

Example 2:

def add(a, b)
    return a+b
from Calculator import add

print(add(1, 2))

Am i going to have performance issues with going for classes instead of imported functions? Considering Multithreading and Multiprocessing.

I have done some tests and it seems to be a small difference of about ~6% but if speed is a concern try using numpy or something similar or dont use python (or any other scripting language) at all.

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