Re: How to increase the speed of this program?




Peter Otten wrote:
Peter Otten wrote:

HYRY wrote:

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.

oarray = array.array("h", [0]*(len(larray)+len(rarray))) #1

ITEMSIZE = 2
size = ITEMSIZE*(len(larray) + len(rarray))
oarray = array.array("h")
oarray.fromstring("\0" * size)

may be a bit faster.

Confirmed:

$ python2.5 -m timeit -s'from array import array; N = 10**6' 'a =
array("h"); a.fromstring("\0"*(2*N))'
100 loops, best of 3: 9.68 msec per loop
$ python2.5 -m timeit -s'from array import array; N = 10**6' 'a = array("h",
[0]*N);'
10 loops, best of 3: 199 msec per loop

Funny thing is that using huge temporary string is faster that
multiplying small array:

C:\Python25>python -m timeit -s"from array import array; N = 10**6" "a
=array('h'); a.fromstring('\0'*(2*N))"
100 loops, best of 3: 9.57 msec per loop

C:\Python25>python -m timeit -s"from array import array; N = 10**6" "a
= array('h','\0\0'); a*N"
10 loops, best of 3: 28.4 msec per loop

Perhaps if array multiplication was as smart as string multiplication
then array multiplication version would be the fastest.

-- Leo

.



Relevant Pages

  • Re: How to increase the speed of this program?
    ... the default python module. ... 100 loops, best of 3: ... Perhaps if array multiplication was as smart as string multiplication ... instance with typecode t and number of elements n with each element ...
    (comp.lang.python)
  • Re: How to increase the speed of this program?
    ... Here is my program, but it is much slower than the C version, so how ... 100 loops, best of 3: ... Funny thing is that using huge temporary string is faster that ... Perhaps if array multiplication was as smart as string multiplication ...
    (comp.lang.python)
  • Re: How to increase the speed of this program?
    ... Here is my program, but it is much slower than the C version, so how ... 100 loops, best of 3: ... Funny thing is that using huge temporary string is faster that ... Perhaps if array multiplication was as smart as string multiplication ...
    (comp.lang.python)
  • Block processing for 2D matrices
    ... The function im2col does exactly what I need, but strangely, I find ... it is running MUCH SLOWER than writing 2 nested "for" loops to index ...
    (comp.soft-sys.matlab)
  • Re: For Next statments
    ... > Is slower than ... If your loops are so dense or nested that seeing the identifier helps you tell what's happening, ...
    (microsoft.public.vb.general.discussion)