help-octave
[Top][All Lists]
Advanced

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

Re: octave_rand::scalar() v. randn


From: Todd Neal
Subject: Re: octave_rand::scalar() v. randn
Date: Tue, 22 Feb 2005 21:28:33 -0600
User-agent: Mozilla Thunderbird 1.0+ (X11/20050218)

Quentin Spencer wrote:
Steve C. Thompson wrote:
I've ask about this yesterday, but I will simplify the question.

Say you write a C++ program using the Octave headers (then make it into
an .oct module) which returns a vector of 1e7 Gaussian distributed
numbers.  Comparing the speed of this to randn(1,1e7), I've found randn
to be much much faster than calling octave_rand::scalar() many times.
I noticed in your original post that you used a floating point value to count to 1e7. I'm sure this won't change things too much, but an integer comparison will always be faster than a floating point one.

I will be adding more to this later.  I've converted a simple simulation
into C++, made the .oct file, and compared the performance to a
vectorized m-file-only simulation.  The pure m-file simulation runs
faster!  Try to explain that.
One of octave's bottlenecks is the parser, but a short m-file that just calls randn with a large size doesn't require much parsing, and makes one function call to generate the random numbers. Your C file calls a the random number generator 1e7 times, with all of the overhead that such a function call generates.



Woudn't the following code be more equivalent to rand(1,1e7) ? On my machine, this version was approximately only twice as slow as calling randn(1,1e7).

#include <octave/oct.h>
#include <octave/oct-rand.h>

DEFUN_DLD (slow, args, ,
  "This is slow")
{
  octave_value_list retval;
  Matrix a = octave_rand::matrix(1,1e7);
  retval(0)=a;
  return retval;
}


octave_rand::matrix is a function that uses a macro to create a tight loop calling a fortran function and setting values in the matrix.


If you call octave_rand::scalar() 1e7 times then you get:
1e7 function calls to scalar();
1e7 function calls to maybe_initialize(); (Each involves a comparison)
1e7 function calls to Fortran
1e7 returns from scalar();

whereas if you call octave_rand::matrix() 1 time then you get:
1 function call to matrix()
1 function call to maybe_initialize();
1e7 function calls to Fortran
1 return from matrix();



Todd



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

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