Re: How to increase the speed of this program?



John Machin wrote:

Thanks, that's indeed faster than array(t, [v]*n) but what I had in
mind was something like an additional constructor:

array.filledarray(typecode, repeat_value, repeat_count)

which I speculate should be even faster. Looks like I'd better get a
copy of arraymodule.c and start fiddling.

Anyone who could use this? Suggestions on name? Argument order?

Functionality: same as array.array(typecode, [repeat_value]) *
repeat_count. So it would cope with array.filledarray('c', "foo", 10)

Why not just optimize array.__mul__? The difference is clearly in the
repeated memcpy() in arraymodule.c:683. Pseudo-unrolling the loop in
python demonstrates a speed up:

[klaas@worbo ~]$ python -m timeit -s "from array import array"
"array('c',['\0'])*100000"
100 loops, best of 3: 3.14 msec per loop
[klaas@worbo ~]$ python -m timeit -s "from array import array"
"array('c',['\0','\0','\0','\0'])*25000"
1000 loops, best of 3: 732 usec per loop
[klaas@worbo ~]$ python -m timeit -s "from array import array"
"array('c','\0'*20)*5000"10000 loops, best of 3: 148 usec per loop

Which is quite close to your fromstring solution:

[klaas@worbo ~]$ python -m timeit -s "from array import array"
"array('c').fromstring('\0'*100000)"
10000 loops, best of 3: 137 usec per loop

In fact, you can make it about 4x faster by balancing:

[klaas@worbo ~]$ python -m timeit -s "from array import array"
"array('c','\0'*200)*500"
10000 loops, best of 3: 32.4 usec per loop

For the record:

[klaas@worbo ~]$ python -m timeit -s "from array import array"
"array('c','\0'*100000)"
10000 loops, best of 3: 140 usec per loop

-Mike

.



Relevant Pages

  • Re: timeit module: am I missing something obvious?
    ... Why am I passing strings around when functions are ... best of 3 trials: 0.792 usec per loop ... quickly choosing number of iterations. ... - The result from the final pass through the convergence loop ...
    (comp.lang.python)
  • Re: Trivial performance questions
    ... 1000000 loops, best of 3: 0.47 usec per loop ... about the hasattr vs getattr issue...: ... bit longer than hasattr -- about 0.2 microseconds. ...
    (comp.lang.python)
  • Re: Optimizing tips for os.listdir
    ... When used on folders with lots of files, ... filtering out all plain files ... 100 2.74e+03 usec per loop ...
    (comp.lang.python)
  • Re: reduce()--what is it good for? (was: Re: reduce() anomaly?)
    ... > favorite language, prior to Python, was APL. ... the old quip about "coding Fortran in any language" does not ... 100 loops, best of 3: 2.9e+03 usec per loop ...
    (comp.lang.python)
  • Re: Creating a List of Empty Lists
    ... 4.5uSec per loop, whereas the same with "list)" gives about 2.3uSec per ... @echo Length: 0 ... 4.38 usec per loop ... With the optimisation tweaked to ignore 0 length lists entirely and only ...
    (comp.lang.python)