Re: How to increase the speed of this program?



"HYRY" <zhangry@xxxxxxxxxx> wrote in message
news:1164699156.682944.97410@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I want to join two mono wave file to a stereo wave file by only using
the default python module.
Here is my program, but it is much slower than the C version, so how
can I increase the speed?
I think the problem is at line #1, #2, #3.

I'm not overly familiar with the array module, but one place you may be
paying a penalty is in allocating the list of 0's, and then interleaving the
larray and rarray lists.

What if you replace lines 1-3 with:

def takeOneAtATime(tupleiter):
for i in tupleiter:
yield i[0]
yield i[1]

oarray = array.array("h",takeOneAtATime(itertools.izip(larray,rarray)))

Or in place of calling takeOneAtATime, using itertools.chain.

oarray = array.array("h", itertools.chain(*itertools.izip(larray,rarray)))

Use itertools.izip (have to import itertools somewhere up top) to take left
and right values in pairs, then use takeOneAtATime to yield these values one
at a time. The key though, is that you aren't making a list ahead of time,
but a generator expression. On the other hand, array.array may be just
building an internal list anyway, so this may just be a wash.

Also, try psyco, if you can, especially with this version. Or pyrex to
optimize this data-interleaving.

HTH,
-- Paul


.



Relevant Pages

  • Re: flatten(), [was Re: map/filter/reduce/lambda opinions ...]
    ... My sort in place routines were cheating because the lists weren't reset between runs so it had the advantage after the first time though of sorting already sorted list. ... for item in seq: ... for flatten in flattens: s = sequence# make new copy! ...
    (comp.lang.python)
  • Re: Is there an easier way to express this list slicing?
    ... Note that alist and blist are not necessarily lists, ... not use slice notation. ... If you are iterating through that sequence of slices a lot, ... yield items ...
    (comp.lang.python)
  • Re: can Python be useful as functional?
    ... def sieve: ... I also know that Python got some useful tool such as map, filter, ... These should be almost the same: listprimes actually lists prime ...
    (comp.lang.python)
  • RE: Creating unique combinations from lists
    ... Creating unique combinations from lists ... def iterall: ... for head in iterables: ... yield + remainder ...
    (comp.lang.python)
  • cloning generator iterators
    ... if (yield 1): ... What is the best way to implement this clone operation? ... Keep track of the values that where sent to the generator iterator, and feed the same values to the clone. ... the elapsed time by testing three lists simultaneously: ...
    (comp.lang.python)