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: [EMAIL PROTECTED] ~]$ python -m timeit -s "from array import array" "array('c',['\0'])*100000" 100 loops, best of 3: 3.14 msec per loop [EMAIL PROTECTED] ~]$ python -m timeit -s "from array import array" "array('c',['\0','\0','\0','\0'])*25000" 1000 loops, best of 3: 732 usec per loop [EMAIL PROTECTED] ~]$ 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: [EMAIL PROTECTED] ~]$ 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: [EMAIL PROTECTED] ~]$ 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: [EMAIL PROTECTED] ~]$ python -m timeit -s "from array import array" "array('c','\0'*100000)" 10000 loops, best of 3: 140 usec per loop -Mike -- http://mail.python.org/mailman/listinfo/python-list