[Numpy-discussion] Iterative Matrix Multiplication

Friedrich Romstedt friedrichromstedt at gmail.com
Sat Mar 6 15:03:55 EST 2010

```2010/3/6 Ian Mallett <geometrian at gmail.com>:
> On Sat, Mar 6, 2010 at 1:20 AM, Friedrich Romstedt
> <friedrichromstedt at gmail.com> wrote:
>> Hmm.  Let's see ... Can you tell me how I can test the time calls in a
>> script take?  I have no idea.
>
> I've been doing:
> t1 = time.time()
> #code
> t2 = time.time()
> print "[code]:",t2-t1

Ok.  I just wonder how you can measure those times.  On my five-years
old machine, they are < 10ms resolution.

> Wow!  Drops to immeasurably quick :D

Yeaah!

>> > #0.128999948502
>> > doublet_count = same_edges.sum(axis = 0)
>>
>> Maybe try axis = 1 instead.  I wonder why this is so slow.  Or maybe
>> it's because he does the conversion to ints on-the-fly, so maybe try
>> same_edges.astype(numpy.int8).sum(axis = 0).
>
> Actually, it's marginally slower :S

Hmm.  I tried the axis = 1 thing, and it also gave no improvement
(maybe you can try too, I'm guessing I'm actually measuring the time
Python spends in exeuting my loop to get significant times ...)

>> 2010/3/5 Ian Mallett <geometrian at gmail.com>:
>> > #takes 0.04 seconds
>> > inner = np.inner(ns, v1s - some_point)
>>
>> Ok, I don't know why I was able to overlook this:
>>
>> dotprod = (ns * (v1s - some_point)).sum(axis = 1)
>
> Much faster :D

:-)  I'm glad to be able to help!

> So, the most costly lines:
> comparison_sum = (sum_1 == sum_2) #0.024 sec
> comparison_diff = (diff_1 == diff_2) #0.024 sec
> same_edges = np.logical_and(comparison_sum, comparison_diff) #0.029 sec
> doublet_count = same_edges.sum(axis = 0) #0.147

At the moment, I can do nothing about that.  Seems that we have
reached the limit.  Anyhow, is it now faster than your Python list
implementation, and if yes, how much?  How large was your gain by
using numpy means at all?  I'm just curious.

Friedrich

```