Bossgaming099 / ctypes-opencv

Automatically exported from code.google.com/p/ctypes-opencv
0 stars 0 forks source link

Poor cvMat performance - thousand times better as_numpy #46

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
From this test code:

"""
import datetime
import opencv

def bench_mat(i__size=1000, i__numpy=False):
    if not i__numpy:
        l__mat = opencv.cvCreateMat(i__size, i__size, opencv.CV_32FC1)
        for l__x in range(i__size):
            for l__y in range(i__size):
                l__mat[l__x, l__y] = 0.0
    else:
        l__mat = opencv.cvCreateMat(i__size, i__size, opencv.CV_32FC1)
        l__mat_numpy = l__mat.as_numpy_array()
        for l__x in range(i__size):
            for l__y in range(i__size):
                l__mat_numpy[l__x, l__y] = 0.0

if __name__ == '__main__':
    print 'benching mat'
    print datetime.datetime.now()
    bench_mat(1000, False)
    print datetime.datetime.now()
    print 'benching numpy'
    print datetime.datetime.now()
    bench_mat(1000, True)
    print datetime.datetime.now()
    print 'benching numpy'
"""

I get this result:

C:\tmp>python bench_cvmat.py
benching mat
2009-07-16 20:24:10.109000
2009-07-16 20:24:23.109000
benching numpy
2009-07-16 20:24:23.109000
2009-07-16 20:24:23.390000
benching numpy

C:\tmp>

Why is that ??? Why cvMat is performing much poorer than as numpy array ?
Is there something to correct this ?

Original issue reported on code.google.com by kpo...@gmail.com on 16 Jul 2009 at 11:26

GoogleCodeExporter commented 8 years ago
Thanks for raising the issue.

CvMat in Python is _not_ supposed to run fast when dealing with an operation 
that
requires a large amount of per-element accesses. This is because the interface 
is
written purely in Python. You should try to use C-based functions that do the 
same
job instead. For example, "cvSetZero(l_mat)" or "l__mat_numpy[:] = 0". 

I can write a C function to make access to CvMat's elements faster but it will 
make
ctypes-opencv non-pure. Besides, people have already done that in other 
projects,
e.g. numpy. It is best to avoid reinventing the wheel.

Best,

Minh Tri

Original comment by pmtri80@gmail.com on 16 Jul 2009 at 11:44