[Numpy-discussion] Iterative Matrix Multiplication

Ian Mallett geometrian@gmail....
Sat Mar 6 12:45:36 CST 2010


Hi,

On Sat, Mar 6, 2010 at 1:20 AM, Friedrich Romstedt <
friedrichromstedt@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

> > #0.0840001106262
> > sum_1 = sum.reshape((len(sum), 1)).repeat(len(sum), axis = 1)
> >
> > #0.0329999923706
> > sum_2 = sum.reshape((1, len(sum))).repeat(len(sum), axis = 0)
> >
> > #0.0269999504089
> > comparison_sum = (sum_1 == sum_2)
>
> We can leave out the repeat() calls and leave only the reshape() calls
> there.  Numpy will substitute dimi == 1 dimensions with stride == 0,
> i.e., it will effectively repeat those dimension, just as we did it
> explicitly.
>
Wow!  Drops to immeasurably quick :D

> > #0.0909998416901
> > diff_1 = diff.reshape((len(diff), 1)).repeat(len(diff), axis = 1)
> >
> > #0.0340001583099
> > diff_2 = diff.reshape((1, len(diff))).repeat(len(diff), axis = 0)
> >
> > #0.0269999504089
> > comparison_diff = (diff_1 == diff_2)
>
> Same here.  Delete the repeat() calls, but not the reshape() calls.
>
Once again, drops to immeasurably quick :D

> > #0.0230000019073
> > same_edges = comparison_sum * comparison_diff
>
> Hmm, maybe use numpy.logical_and(comparison_sum, comparison_diff)?  I
> don't know, but I guess it is in some way optimised for such things.
>
It's marginally faster.

> > #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

> Hope this gives some improvement.  I attach the modified version.
>
> Ah, one thing to mention, have you not accidentally timed also the
> printout functions?  They should be pretty slow.
>
Nope--I've been timing as above.

> Friedrich
>
On Sat, Mar 6, 2010 at 1:42 AM, Friedrich Romstedt <
friedrichromstedt@gmail.com> wrote:

> 2010/3/5 Ian Mallett <geometrian@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

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

Thanks again,
Ian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20100306/228e6978/attachment.html 


More information about the NumPy-Discussion mailing list