Week 2 Optional NumPy Lab

I’m confused by the output from the first vectorization example. The four times for dot, outer, element wise, and general dot with looping were (in ms) .0906, .3045, .1127, .1733, while the vectorized times were .4296, .0945, .0825, 1.325. So vectorized doesn’t always outperform? Am I missing something? I did not change any code in the example cells.

Yes, the examples they use here are a bit unfortunate in that the sizes of the objects are really too small to always clearly see the benefits of vectorization. If you try it on your own and use matrices or vectors with O(10^3) or O(10^4) sizes, you’ll see consistently better numbers for the vectorized case.

Note that they are using a time function that gives actual process CPU time, instead of wall clock time, so it should be relatively reliable. Wall clock times are all over the map, because then you’re at the mercy of the context switching that the OS is doing underneath you. But even with process CPU times, you get unpredictable behavior because of the python interpreter and the JIT compiler and all that.

Hi Paul,

Thanks very much for the clarification!