can this be optimized

Pete Shinners pete at visionart.com
Wed Jun 14 14:54:43 EDT 2000


i have this routine which is running pretty quick
on my machine. i may be getting overly greedy, but
if i could shave off more time i'd be pleased
(btw, this is with numeric python)
(btw2, WIDTH and SAMPLES are defined elsewhere as global)


def pumpmeup_func(img):
    rndpos = RandomArray.uniform(0, WIDTH, SAMPLES).astype(Int)
    rndcol = RandomArray.uniform(100, 255, SAMPLES).astype(Int)
    for x, color in map(None, rndpos, rndcol):
	img[x,-3:-1] = color



my thoughts for optimization are...
- a quicker way to create the random arrays?
  this is already by far the quickest way i've found.
  i'm not truly worried about nonuniform distribution
  of my random arrays, and i'm not sure if this
  uniform() function is taking the time to bother
  with that.

- replacing the for loop with a map/reduce thingy
  and a lamda. i've never done that before and couldn't
  get anything i tried to parse.

- somehow creating a single random array, and avoiding
  the need to map then together? (2 dimensional?)
  i question this would gain me anything
  (the map is still quicker than looping through
   range(SAMPLES) and indexing each array)

- i was wondering about stackless as well. it seems with
  a chunk of code like this it wouldn't make a noticeable
  difference, but maybe if the loop was processed with
  a lambda thingy it could offer benefits?

- creating a list/array of the slices and then use numpy
  to assign the random colors to that?  (like to see this!)


anyways, i've been twiddling with this for awhile and have
taken it from .045 seconds per call, down to around .013
WOOHOO! i'm excited as it is, but want to see if it can go
further

thanks all you gurus!



More information about the Python-list mailing list