Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python performance
#1
Hello,
I wrote a short python program with a few calls to OpenCV and Numpy functions. If I rewrite it into a C++ program, would that give a substantial performance boost? If the program calls a library function, is the library function a precompiled binary, or Python code that is executed by the interpreter?
Reply
#2
It's pretty much a fact that Python is slower than other languages, especially ones like C++ which have a low abstraction level. Python has stuff like dynamic variable naming, automatic garbage collection etc, which just abstracts it even further from the base computer.

And then there's the fact that Python is interpreted while C++ is compiled. Compiled programs are high level programs converted to low level code. Due to this, it executes much faster.

It's a common technique to re-write certain parts of large Python programs with C++ when speed and performance is critical. You could try this approach.

If you want to learn more about Abstraction, what it is, how it effects performance etc, you can read all about it in this article.
Reply
#3
Python-OpenCV is just a wrapper around the original C/C++ code.
It is normally used for combining best features of both the languages,Performance of C/C++ & Simplicity of Python.
NumPy is mostly written in C and optimized Fortran.
So what you use should have good speed in bottom.
Have to measure to see what's going on.

There is a lot going on in Python-based scientific computing that can speed up code,
eg Dask, PyTorch, Numba, Keras/TensorFlow.
Or more general PyPy , Cython.
Reply
#4
Have you looked at Numba at all?

Numba

I've tried it out with some fairly simple examples and had quite a lot of success with it, at least worth looking into eh?

Regards

Gary
Reply
#5
Here's an old example program I did sometime back just to test the speed increase using numba:
from numba import jit
import time

ITERATIONS = 5000_000
MAXFIB = 20


@jit(nopython=True)
def fastSum(max_No):
    val = 0
    for num in range(1,max_No):
        val += 1/num
    return val


def oldSum(max_No):
    val = 0
    for num in range(1,max_No):
        val += 1/num
    return val


def builtinSum(max_No):
    return sum((1/num for num in range(1,max_No)))

@jit(nopython=True)
def fib(num):
    return 1 if num <= 1 else fib(num-1) + fib(num - 2)

@jit(nopython=True)
def fibIter(num):
    val1,val2 = 1,1
    for n in range(1,num):
        val1,val2 = val2,val1+val2
        
    return val2


if __name__ == "__main__":
    for x in range(1,11):
        print(x,fibIter(x))
    for test in range(3):
        print(f"Running {'fast' if not test else 'old' if test == 1 else 'builtin'} sum() for {ITERATIONS} element(s)")
        for run in range(10):
            start = time.time()
            theSum = fastSum(ITERATIONS) if not test else oldSum(ITERATIONS) if test == 1 else builtinSum(ITERATIONS)
            print(f"\tRun {run} : Answer = {theSum} : Elapsed = {(time.time() - start)}")
       
    a , b = fibIter(MAXFIB) , fibIter(MAXFIB-1)
    print(MAXFIB,a,b,a/b)
    
If you run it you can see the sort of speed improvements possible, give it a go.

Regards

Gary
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What about Python 3.11 performance boost wavic 2 1,045 Nov-01-2022, 10:45 AM
Last Post: wavic
  performance time Skaperen 2 2,251 Apr-13-2019, 08:29 PM
Last Post: Skaperen
  why so much differnt performance in python 2.7.10, 2.7.14 and 3.6.4 jiangtianyong 0 2,543 Feb-09-2018, 06:29 AM
Last Post: jiangtianyong

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020