1

I just try figure out why my program is so slow and find the following result.

In [11]: n = 1000000

In [12]: x = randn(n)

In [13]: %timeit norm(x)
100 loops, best of 3: 2.25 ms per loop

In [14]: %timeit (x.dot(x))**0.5
1000 loops, best of 3: 387 µs per loop

I know the norm function will contain many if else detecting the input and select the right norm. But I am still wondering this big difference especially when calling in loops.

Is this normal in numpy?

Another examples is that computing the eigenvalue and eigenvector of a 10000x10000 random generated matrix from randn. Firstly I use Matlab compute and get the result in several minutes. But numpy took a very very very long time to compute this and finally I Ctrl+c the process. Both use the eig function respectively.

Saddle Point
  • 3,074
  • 4
  • 23
  • 33
  • 1
    What version of NumPy are you using? As of version 1.9 (and possibly older) this is special-cased so that `norm(x)` and `(x.dot(x))**0.5` perform comparably. – unutbu May 31 '15 at 16:01
  • 1
    These timings aren't necessarily indicative. On my machine I see times that differ - `norm()` took only 592 µs per loop, the `dot()` method just 567 µs per loop. You will find good advice in [this answer](http://stackoverflow.com/questions/6684238/whats-the-fastest-way-to-find-eigenvalues-vectors-in-python) – holdenweb May 31 '15 at 16:01
  • @holdenweb I'm using `line-profiler` to locate why my program is so slow and just find `norm` took a large among of time. Since I'm new to python and numpy, I'm wondering if this's normal. And thanks for you link. :) – Saddle Point Jun 01 '15 at 01:09
  • @unutbu `pip list` show that it's `1.9.2` – Saddle Point Jun 01 '15 at 01:12
  • @unutbu All right. It seems that OSX's built in `numpy 1.8` is loaded due to the order of `sys.path`. Now I'm using the 'true' 1.9.2, and the result seems comparable. – Saddle Point Jun 01 '15 at 03:33
  • There is no "built-in `numpy`" on OSX - numpy is a third-party extension module - it does not come with the language and must be separately installed. It's possible you installed `numpy` into your system Python, of course, but you'd have had to use `sudo` or similar to elevate your privileges. – holdenweb Jun 01 '15 at 12:49

0 Answers0