Saturday, May 29, 2010

numpy array performance / divide and conquer considered harmful

This is again a post about python code speed, the data and inference are more than a few months old but still valid.
Here's a spreadsheet showing speed of array math operations (+, -, *, /) between numpy arrays and python lists.
Check this spreadsheet to see the timings of various operations

The operations I considered for comparison were:

  • x+0.1
  • x-0.1
  • x*0.1
  • x/0.1
  • x*(1/0.1)
  • x+y
  • x-y
  • x*y
  • x/y
  • [p+yp[j] for j,p in enumerate(xp)]
  • [xp[j]+yp[j] for j in xrange(i)]
where x and y are numpy arrays, xp and yp are python lists, all of size N which is varied for the comparison.
The raw timings data is available here:

    See the timings plot yourself
    • Use numpy arrays for size > 10
    • Avoid division as much as you can to improve the speed of your numerical codes
    • Instead of x/0.1 do x*(1/0.1) . This itself causes large speedup as N is increased.
    • x/0.1 and x/y take almost the same time
    • +, -, * take almost same time, / takes much more time, and its expense increases as N is increased.
    • Once again, do not divide.
    • The same thing is valid in cython code also. Avoid division even in cython code, and even if you are using double instead of numpy arrays (buffer). Rewrite expressions to minimize the usage of division operator.

    1 comment:

    1. It was much more of an odd piece than a practically rolex replica sale useful one, and hence it had not received instant recognition or success. It has to be noted though that replica watches uk at the time of its creation in the middle of the 1950's intercontinental business trips were much less common than rolex replica sale they are today, which rendered this watch less desirable than its similarly expensive counterparts which were equipped with other useful complications as judged by contemporary standards. In any event the original Patek Philippe World Timer is a complicated piece unto itself. The ingenious world time disc-system was invented by Louis Cottier in 1931 was later replica watches implemented by Patek, Vacheron Constantin, Rolex, and others. As times have changed, not only did this complication rolex replica uk become one of the most popular useful functions, but the reference 2523 has also become much more appreciated among collectors, as seen by the seven figure prices these replica watches uk world timers consistently achieve at auctions.