performance hit with functions of vectors

David Bateman David.Bateman at motorola.com
Fri Jan 4 08:06:01 CST 2008


augustm wrote:
> Hello,
>
> I am using a version of octave-3.0.0, compiled on a 64-bit linux machine using gcc.
> (I am NOT using the experimental enable-64 options)
>
> I am troubled by the variability of function evaluations speeds applied to vectors;
> I have a vector of variables of length 30,000.
>
> octave:1> whos zz
>
> *** local user variables:
>
>   Prot Name        Size                     Bytes  Class
>   ==== ====        ====                     =====  ===== 
>    rwd zz      30000x1                     360008  double
>   
First observation, calculating back from the bytes used above, you have
a full column vector stored as a sparse matrix. Why do that? It would be
faster for later calculations to have it as a full vector rather than a
sparse one.

>
> Their range is quite reasonable- so there is no question of under/overflow
> octave:2> min(zz)
> ans = Compressed Column Sparse (rows = 1, cols = 1, nnz = 1)
>   (1, 1) ->  0.15938
> octave:3> max(zz)
> ans = Compressed Column Sparse (rows = 1, cols = 1, nnz = 1)
>   (1, 1) ->  170.70
>
>
> I perform a simple function evaluation- it's fast
>
> octave:4>  tic; j=sin(zz); toc
> Elapsed time is 0.0016 seconds.
>
>
> However now the problem!
>
> *********************************
> octave:5> tic; j=cos(zz); toc
> Elapsed time is 0.59 seconds.
> **********************************
>
> certain functions are 400 times slower than others.
>   

The reason is for this slow up is that cos(0) == 1. In that case the
zero elements of the sparse matrix become non zero and the loop in the
mapper calculation is different. I can see an optimization I can make to
that calculation as in the attached patch that will make it faster for
the cases you discussed here. With this patch I then see

octave:1> zz = sparse(171*rand(30000,1));
octave:2> tic; j=sin(zz); toc
Elapsed time is 0.00391608 seconds.
octave:3> tic; j=cos(zz); toc
Elapsed time is 0.00375593 seconds.
octave:4> tic; j=sin(zz); toc
Elapsed time is 0.00464701 seconds.
octave:5> tic; j=cos(zz); toc
Elapsed time is 0.00407792 seconds.
octave:6> any(full(cos(zz)) != cos(full(zz)))
ans = 0
octave:7> all(full(cos(zz)) == cos(full(zz)))
ans =  1

So this patch wins back the speed you demonstrated as being lacking..

>
> Thus
>
> tic; j=log(zz); toc
> Elapsed time is 0.59 seconds.
>
>
> but this is much better
>
> tic; j=tan(zz); toc
> Elapsed time is 0.002 seconds.
>   
This is the same case as above log(0) != 0 while tan(0) == 0.

Regards
David

-- 
David Bateman                                David.Bateman at motorola.com
Motorola Labs - Paris                        +33 1 69 35 48 04 (Ph) 
Parc Les Algorithmes, Commune de St Aubin    +33 6 72 01 06 33 (Mob) 
91193 Gif-Sur-Yvette FRANCE                  +33 1 69 35 77 01 (Fax) 

The information contained in this communication has been classified as: 

[x] General Business Information 
[ ] Motorola Internal Use Only 
[ ] Motorola Confidential Proprietary

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: patch5
Url: https://www.cae.wisc.edu/pipermail/bug-octave/attachments/20080104/77e9995c/attachment-0002.ksh 
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: patch5.changelog
Url: https://www.cae.wisc.edu/pipermail/bug-octave/attachments/20080104/77e9995c/attachment-0003.ksh 


More information about the Bug-octave mailing list