[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: oct file: slower than expected using fortran_vec
From: |
Seb Astien |
Subject: |
Re: oct file: slower than expected using fortran_vec |
Date: |
Mon, 16 May 2011 08:55:01 +0200 |
On Mon, May 16, 2011 at 7:29 AM, Andy Buckle <address@hidden> wrote:
> 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 */
>
It is a bit faster the second time, but it does not explain the gap
between the two:
The bigger the matrix, the bigger the gap between built-in function
and the oct one.
I suspect a copying taking it place somehwere.
octave:12> a=rand(1000,1000);
octave:13> tic; sum(sum((a))); toc
Elapsed time is 0.00299295 seconds.
octave:14> tic; sumit(a); toc
Elapsed time is 0.020828 seconds.
octave:15> a=rand(10000,10000);
octave:16> tic; sum(sum((a))); toc
Elapsed time is 0.208262 seconds.
octave:17> tic; sumit(a); toc
Elapsed time is 1.49816 seconds.
Seb
Re: oct file: slower than expected using fortran_vec, Jordi Gutiérrez Hermoso, 2011/05/16