From: Eric Firing <efiring@ha...>  20120606 17:41:35

On 06/06/2012 06:42 AM, Ethan Gutmann wrote: >> ... >> No, but you can do this: >> >> plt.plot([3] * 4, [60, 80, 120, 180], ...) > > This started from a simple enough question, but it got me thinking about what the fastest way to do this is (in case you have HUGE arrays, or many loops over them). This may be old news to some of you, but I thought it was interesting: > > In ipython pylab > > In [1]: %timeit l=[3]*10000 > 10000 loops, best of 3: 53.3 us per loop > > In [2]: %timeit l=np.zeros(10000)+3 > 10000 loops, best of 3: 26.9 us per loop > > In [3]: %timeit l=np.ones(10000)*3 > 10000 loops, best of 3: 32.9 us per loop > > In [4]: %timeit l=(np.zeros(1)+3).repeat(10000) > 10000 loops, best of 3: 87.4 us per loop > > In [5]: %timeit l=np.zeros(10000);l[:]=3 > 10000 loops, best of 3: 21.6 us per loop > > In [6]: %timeit l=np.zeros(10000,dtype=np.uint8);l[:]=3 > 100000 loops, best of 3: 13.9 us per loop > > Using int16, int32, float32 get progressively slower to the default float64 case listed on line [5], changing the datatype in other methods doesn't result in nearly as large a speed up as it does in the last case. > > Ben's method is probably the most elegant for small arrays, but does any one else have a faster way to do this? (I'm assuming no use of blitz, inline C, f2py, but if you think you can do it faster in one of those, show me the way). > Since we end up needing float64 anyway: In [3]: %timeit l=np.empty(10000,dtype=np.float64); l.fill(3) 100000 loops, best of 3: 14.1 us per loop In [4]: %timeit l=np.zeros(10000,dtype=np.float64);l[:]=3 10000 loops, best of 3: 26.6 us per loop Eric > Sorry, maybe this is more appropriate on the numpy list. > > Ethan > > >  > Live Security Virtual Conference > Exclusive live event will cover all the ways today's security and > threat landscape has changed and how IT managers can respond. Discussions > will include endpoint security, mobile security and the latest in malware > threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ > _______________________________________________ > Matplotlibusers mailing list > Matplotlibusers@... > https://lists.sourceforge.net/lists/listinfo/matplotlibusers 