Carlos Becker wrote: > Besides the matlab/numpy comparison, I think that there is an inherent > problem with how expressions are handled, in terms of efficiency. > For instance, k = (m - 0.5)*0.3 takes 52msec average here (2000x2000 > array), while k = (m - 0.5)*0.3*0.2 takes 0.079, and k = (m - > 0.5)*0.3*0.2*0.1 takes 101msec. > Placing parentheses around the scalar multipliers shows that it seems to > have to do with how expressions are handled, is there sometihng that can > be done about this so that numpy can deal with expressions rather than > single operations chained by python itself?
well, it is Python, and Python itself does not know anything about array math -- so you need to be careful to do that correctly yourself. Python aside, understanding how parentheses effect computation, even for algebraically equal expressions is a good thing to understand. Aside from issues with scalars, a Python expression like: a = a * b * c does: multiply a and b and put it in a temporary multiply that temporary by c and put that in a temporary assign the final temporary to a so it does, in fact, create two "unnecessary" temporaries for this simple expression. If you are concerned about performance, there are ways to control that. "in-place" operators is one: a *= b a *= c will not create any temporaries, and will probably be faster. It still does two loops through the data, though. If your arrays are too big to fit in cache, that could effect performance. To get around that you need to get fancy. One option is numexpr: http://code.google.com/p/numexpr/ numexpr takes an entire expression as input, and can thus optimize some of this at the expression level, make good use of cache, etc. -- it's pretty cool. There are a few other options, including weave, of course. -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception chris.bar...@noaa.gov _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion