[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: oct file: slower than expected using fortran_vec
From: |
Andy Buckle |
Subject: |
Re: oct file: slower than expected using fortran_vec |
Date: |
Mon, 16 May 2011 06:29:25 +0100 |
On Sun, May 15, 2011 at 9:10 PM, Seb Astien <address@hidden> wrote:
> Hi,
>
> I have been experimenting a bit with oct files to see how much faster
> it would be.
> I wrote a trivial example to sum the elements of an array.
> To my surprised, this was still much slower than built-in functions:
>
> octave:1> A=rand(10000,10000);
> octave:2> tic; s1=sum(sum(A)); toc
> Elapsed time is 0.222649 seconds.
> octave:3> tic; s2=sumit(A); toc
> Elapsed time is 1.5032 seconds.
>
> The C++ code is below. I added a const in front of *p to prevent
> copying, but I guess it is still happening.
>
> Any comment, pointer, explanation would be greatly appreciated.
>
> Regards,
>
> Seb
>
> ===
> #include <octave/oct.h>
>
> DEFUN_DLD (sumit, args, , "Sum elements of an array")
> {
> int nargin = args.length();
>
> if (nargin != 1){
> print_usage();
> return octave_value_list();
> }
>
> NDArray A = args(0).array_value();
> const double *p = A.fortran_vec();
> double sum = 0;
> octave_idx_type N = args(0).nelem();
>
> if (! error_state){
> for(octave_idx_type i=0; i<N; i++)
> sum += *(p++);
> return octave_value (sum);
> }
> }
> ===
The first time you run sumit, it will have to be loaded. This will ad
overhead. If you run sumit a few times, what is the comparison like?
--
/* andy buckle */
Re: oct file: slower than expected using fortran_vec, Jordi Gutiérrez Hermoso, 2011/05/16