Dear Jurgen and Charles,
Thanks for your replies.
I was also asking myself whether the same applies to returning a large
matrix from a function: if there is a big difference between using
'return' on the one hand, and passing a reference and writing the
result to it. The second option seems to avoid copying the matrix.
In the example below (modified from Charles) the second option is
twice as fast, so it looks like the first function writes a value of 1
to every position of the matrix and then copies them one by one when
returning. The second function writes 1's just once, directly to the
supplied address. So, with a large return and inside a loop, it is
better.
Best,
Marek
Ox version 4.04 (Windows/U) (C) J.A. Doornik, 1994-2006
Time spent in routines
return 32.03 0.68
reference 14.86 0.32
Total: 46.89
#include <oxstd.h>
#include <packages/oxutils/oxutils.h>
SomeFuncRet(const iN)
{
decl mRet;
mRet = ones(iN,iN);
return mRet;
}
SomeFuncRef(const iN, const adRet)
{
adRet[0] = ones(iN,iN);
}
main()
{
decl iR, mX, r, iN = 1000;
iR= 1000; // Number of repetitions
TrackTime("return");
for (r= 0; r < iR; ++r)
mX = SomeFuncRet(iN);
TrackTime("reference");
for (r= 0; r < iR; ++r)
SomeFuncRef(iN,&mX);
TrackTime(-1);
TrackReport();
}
On 11/16/07, Jurgen Doornik <[log in to unmask]> wrote:
> Dear Marek,
>
> When an argument is const, Ox does not need to make a copy (and instead just
> passes a pointer internally). Therefore, the rule of thumb is to mark all
> arguments const, unless you discover that you need it to be non-const (because
> an argument is changed inside the functions).
>
> A reference will be marginally slower, because it must be dereferenced inside
> the function (const is also less error prone, because it is useful to see from
> the function header that the arguments will be unchanged, something you cannot
> guarantee with a reference).
>
> Note that when using a reference, the argument can still be const. What is const
> is the reference, but not what it refers to.
>
> Extending Charles' program, and running it on my notebook:
> Ran 1000 times as many iterations in second and third case
> Time spent in routines
> NotConst 1.25 0.68
> Constant 0.28 0.15
> Reference 0.30 0.16
> Total: 1.82
>
> This will be slightly too optimistic for call be reference, because there is no
> actual use of the argument.
>
> Jurgen
>
> oxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxox
> oxoxoxox 5th OxMetrics user conference 2007
> oxoxoxox Cass Business School, London
> oxoxoxox 20-21 September 2007
> oxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxox
> Dr Jurgen A Doornik
> Nuffield College, Oxford OX1 1NF, UK
> tel. UK: +44-1865-278610 fax +44-1865-278621
> http://www.doornik.com
> http://www.oxmetrics.net
> oxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxox
>
>
> Marek Jarocinski wrote:
> > Dear Jurgen and ox-users,
> >
> > If I want to pass a large matrix as an argument, but do not change it
> > inside the function, what is the fastest way to do this? pass a
> > reference or use 'const'?
> >
> > Is the difference big?
> >
> >
> > According to the syntax reference, "Function definitions":
> > "All function arguments are passed by value. This means that a copy of
> > the actual object is made. For int, double, matrix and string types
> > the whole object is copied. Any changes to the copy are lost as soon
> > as the function returns. ...
> > It is good practice to label an argument const if a function doesn't
> > change the variable. This increases program clarity and enables the
> > compiler to generate more efficient code. "
> > So are arguments which are labeled as 'const' also passed by value
> > (i.e. does Ox make a copy of them?) That sounds like a lot of
> > unnecessary work. If so, what exactly is the effect of 'const'?
> >
> > Regards,
> >
> > Marek
>
|