I’m thinking I need to use numpy or some other library to fill these arrays fast enough but I don’t know much about it. Right now this operation takes about 1 second on a quad-core Intel PC, but I need it to be as fast as possible. Any help is greatly appreciated. Thanks!
import cv
class TestClass:
def __init__(self):
w = 960
h = 540
self.offx = cv.CreateMat(h, w, cv.CV_32FC1)
self.offy = cv.CreateMat(h, w, cv.CV_32FC1)
for y in range(h):
for x in range(w):
self.offx[y,x] = x
self.offy[y,x] = y
You’re generating a half million integers and creating over a million references while you’re at it. I’d just be happy it only takes 1 second.
If you’re doing this a lot, you should think about ways to cache the results.
Also, being on a quad-core anything doesn’t help in a case like this, you’re performing a serial operation that can only execute on one core at a time (and even if you threaded it, CPython can only be executing one pure-Python thread at a time due to the Global Interpreter Lock).