Fastest Way To Loop Through Every Pixel
Nick Vatamaniuc
vatamane at gmail.com
Thu Jul 27 22:31:14 EDT 2006
Hello Chaos,
Whatever you do in "#Actions here ..." might be expressed nicely as a
ufunction for numeric. Then you might be able to convert the expression
to a numeric expression. Check out numpy/scipy.
In general, if thisHeight, thisWidth are large use xrange not range.
range() generates a list of numbers first then iterates through them.
So try that first.
Then of course if you do the whole thing many times you could just
pre-generate the indices as in:
all_indices=[]
for i in xrange(thisHeight):
for j in xrange(thisWidth):
all_indices.append( (i,j) )
Then each time you need to run '#Actions here...' you can just use
for (i,j) in all_indices:
#Actions here ... blah blah
In general, if there would be a way to significantly optimize generic
for loops, they would probably be already optimized...
Nick V.
Chaos wrote:
> As my first attempt to loop through every pixel of an image, I used
>
> for thisY in range(0, thisHeight):
> for thisX in range(0, thisWidth):
> #Actions here for Pixel thisX, thisY
>
> But it takes 450-1000 milliseconds
>
> I want speeds less than 10 milliseconds
>
> I have tried using SWIG, and pypy but they all are unsuccessfull in
> compiling my files.
More information about the Python-list
mailing list