help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: oct file: slower than expected using fortran_vec


From: John W. Eaton
Subject: Re: oct file: slower than expected using fortran_vec
Date: Mon, 16 May 2011 14:51:29 -0400

On 16-May-2011, Seb Astien wrote:

| Thank you so much. It is what I was missing.
| Indeed, now:
| 
| octave:1> A=rand(10000,10000);
| octave:2> tic; s1=sum(sum(A)); toc
| Elapsed time is 0.205881 seconds.
| octave:3> tic; s2=sumit(A); toc
| Elapsed time is 0.203057 seconds.
| 
| So it is faster than the built-in, which I was expecting since the
| built-in sums first per columns and then sum the results.

If you want to sum all the elements of an array, then you should write

  sum (A(:))

instead of

  sum (sum (A))

No copy is made when you write A(:) and then you can sum over all the
elements without having to know how many dimensions there are (so you
don't have to call sum ndims(A) times, or have different code for different
numbers of dimensions).

Also, tic/toc gives you wall clock time.  If you want to do
benchmarking, you should be using cputime instead.

Another thing that accounts for differences between your code and the
built-in sum function is that the built-in function does some argument
checking and dispatches based on the argument type.  For example, what
happens to your function if you call it with an argument that is not
an array of doubles?

Also, your error checking  is not quite right.  You should also return
a value if an error occurs.  The value won't be used, so it can be
anything, but you should still return something. The normal thing to
do is return an empty octave_value_list object, or a single undefined
octave_value object (it will be converted to an octave_value_list
object automatically).

jwe


reply via email to

[Prev in Thread] Current Thread [Next in Thread]