<div dir="ltr">Apologies, correct timeit code this time (I had gotten the wrong shape for the output matrix in the loop case):<div><br></div><div><div>if __name__ == '__main__':</div><div><br></div><div>    repeat = 1000</div><div>    items = [Item('item_%d'%(i+1)) for i in xrange(500)]</div><div><br></div><div>    output = numpy.asarray([item.do_something() for item in items]).T</div><div>    statements = ['''</div><div>                  output = numpy.asarray([item.do_something() for item in items]).T</div><div>                  ''',</div><div>                  '''</div><div>                  output = numpy.empty((8, 500))</div><div>                  for i, item in enumerate(items):</div><div>                      output[:, i] = item.do_something()</div><div>                  ''']</div><div><br></div><div>    methods = ['List Comprehension', 'Empty plus Loop   ']                  </div><div>    setup  = 'from __main__ import numpy, items'</div><div><br></div><div>    for stmnt, method in zip(statements, methods):</div><div><br></div><div>        elapsed = timeit.repeat(stmnt, setup=setup, number=1, repeat=repeat)</div><div>        minv, maxv, meanv = min(elapsed), max(elapsed), numpy.mean(elapsed)</div><div>        elapsed.sort()</div><div>        best_of_3 = numpy.mean(elapsed[0:3])</div><div>        result = numpy.asarray((minv, maxv, meanv, best_of_3))*repeat</div><div><br></div><div>        print method, ': MIN: %0.2f ms , MAX: %0.2f ms , MEAN: %0.2f ms , BEST OF 3: %0.2f ms'%tuple(result.tolist())</div></div><div><br></div><div><br></div><div>Results are the same as before...</div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 7 October 2017 at 11:52, Andrea Gavana <span dir="ltr"><<a href="mailto:andrea.gavana@gmail.com" target="_blank">andrea.gavana@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi All,<div><br></div><div>    I have this little snippet of code:</div><div><br></div><div><div>import timeit</div><div>import numpy</div><div><br></div><div>class Item(object):</div><div><br></div><div>    def __init__(self, name):</div><div><br></div><div>        <a href="http://self.name" target="_blank">self.name</a> = name</div><div>        self.values = numpy.random.rand(8, 1)</div><div><br></div><div>    def do_something(self):</div><div><br></div><div>        sv = self.values.sum(axis=0)</div><div>        array = numpy.empty((8, ))</div><div>        f = numpy.dot(0.5*numpy.ones((8, )), self.values)[0]</div><div>        array.fill(f)</div><div>        return array</div></div><div><br></div><div><br></div><div>In my real application, the method do_something does a bit more than that, but I believe the snippet is enough to start playing with it. What I have is a list of (on average) 500-1,000 classes Item, and I am trying to retrieve the output of do_something for each of them in a single, big 2D numpy array.</div><div><br></div><div>My current approach is to use list comprehension like this:</div><div><br></div><div>output = numpy.asarray([item.do_<wbr>something() for item in items]).T<br></div><div><br></div><div>(Note: I need the transposed of that 2D array, always).</div><div><br></div><div>But then I though: why not preallocating the output array and make a simple loop:</div><div><br></div><div><div>output = numpy.empty((500, 8))</div><div>for i, item in enumerate(items):</div><div>    output[i, :] = item.do_something()</div></div><div><br></div><div><br></div><div>I was expecting this version to be marginally faster - as the previous one has to call asarray and then transpose the matrix, but I was in for a surprise:</div><div><br></div><div><div>if __name__ == '__main__':</div><div><br></div><div>    repeat = 1000</div><div>    items = [Item('item_%d'%(i+1)) for i in xrange(500)]</div><div><br></div><div>    statements = ['''</div><div>                  output = numpy.asarray([item.do_<wbr>something() for item in items]).T</div><div>                  ''',</div><div>                  '''</div><div>                  output = numpy.empty((500, 8))</div><div>                  for i, item in enumerate(items):</div><div>                      output[i, :] = item.do_something()</div><div>                  ''']</div><div><br></div><div>    methods = ['List Comprehension', 'Empty plus Loop   ']                  </div><div>    setup  = 'from __main__ import numpy, items'</div><div><br></div><div>    for stmnt, method in zip(statements, methods):</div><div><br></div><div>        elapsed = timeit.repeat(stmnt, setup=setup, number=1, repeat=repeat)</div><div>        minv, maxv, meanv = min(elapsed), max(elapsed), numpy.mean(elapsed)</div><div>        elapsed.sort()</div><div>        best_of_3 = numpy.mean(elapsed[0:3])</div><div>        result = numpy.asarray((minv, maxv, meanv, best_of_3))*repeat</div><div><br></div><div>        print method, ': MIN: %0.2f ms , MAX: %0.2f ms , MEAN: %0.2f ms , BEST OF 3: %0.2f ms'%tuple(result.tolist())</div><div><br></div></div><div><br></div><div>I get this:</div><div><br></div><div><div>List Comprehension : MIN: 7.32 ms , MAX: 9.13 ms , MEAN: 7.85 ms , BEST OF 3: 7.33 ms</div><div>Empty plus Loop    : MIN: 7.99 ms , MAX: 9.57 ms , MEAN: 8.31 ms , BEST OF 3: 8.01 ms</div><div><br></div><div><br></div></div><div>Now, I know that list comprehensions are renowned for being insanely fast, but I though that doing asarray plus transpose would by far defeat their advantage, especially since the list comprehension is used to call a method, not to do some simple arithmetic inside it...</div><div><br></div><div>I guess I am missing something obvious here... oh, and if anyone has suggestions about how to improve my crappy code (performance wise), please feel free to add your thoughts.</div><div><br></div><div>Thank you.</div><span class="HOEnZb"><font color="#888888"><div><br></div><div>Andrea.</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div></font></span></div>
</blockquote></div><br></div>