PDA

View Full Version : [Python] Why speedup with float arithmetic


regomodo
June 9th, 2008, 02:20 PM
I've just noticed a weird thing. I'm doing basic integer manipulation on images and it's a little slow. At some point i set my image array dtype to float. All of a sudden they've sped up no end. Why?

Here's a bit of simple code where float arithmetic isn't necessary.

#! /usr/bin/env python
import Image,numpy

im = Image.open("test.png").convert("L")
dimx = im.size[0]
dimy = im.size[1]

# use dtype = float for speed up
im_1 = numpy.asarray(im,dtype = float) # image as an array. READ-ONLY

def threshold(n):
im_m = numpy.zeros( (dimy,dimx) ,dtype = "uint8" )
for x in range(im.size[0]):
for y in range(im.size[1]):
if im_1[y,x] > n:
im_m[y,x] = 255
# not needed, they're already zero
# else:
# im_m[y,x] = 0
Image.fromarray(im_m).show()

threshold(10)

pointone
June 10th, 2008, 12:06 AM
I would guess it has something to do with this (http://en.wikipedia.org/wiki/Floating_point_unit)!

regomodo
June 10th, 2008, 04:17 AM
I would guess it has something to do with this (http://en.wikipedia.org/wiki/Floating_point_unit)!

But would that account for the speed difference between integer arithmetic on the CPU and float arithmetic on the FPU. Unfortunately, as my course only got as far as the Moto' 68xxx series i've no idea.