From help-request at octave dot org Wed Feb 23 16:07:34 2005 Subject: Re: An example of poor C++ performance From: Todd Neal To: help at octave dot org Date: Wed, 23 Feb 2005 16:13:28 -0600 Steve C. Thompson wrote: > Hello Group, > > My previous posts have been leading up to this. I have successfully > converted my m-file simulation code to C++, which can then be loaded > as an .oct module. To test speed improvements, I've made a simple > example comparing the simulation with two approaches: > > 1) pure m-file (vectorized) 2) m-file/C++ hybrid > > Each approach generates about 1e9 random numbers. The pure m-file is > vectorized by crunching numbers in 1e4 blocks (which I've found to > be optimum on my machine). However, over 50,000 loops are required. > The hybrid m-file/C++ diverts the looping C++. I may be wrong, but I think that the problem is that the comparison is not quite valid. randn() is called as randn(1,v) , with v being 10000. This appears to call octave_rand::nd_array which runs in a tight loop calling a Fortran function and filling the array. > This approach only has 46 m-file loops and the rest is done in the > compiled module. For each C++ loop a couple random number are > generated with the command octave_rand::scalar(). > > I was expecting C++ implementation to outperform the pure m-file > approach, but this isn't the case. > The C++ version doesn't take advantage of octave_rand::nd_array and instead calls octave_rand::scalar which as I detailed in an earlier email calls maybe_initialize() which checks a flag, and then calls the Fortran function. You get many more function calls and returns this way including redundant calls to maybe_initialize(). I haven't tested this theory but if you wrote the m-file program in the same way, calling rand(1) for each pseudo-random number needed, it would probably take much more time than the C++ program. 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 -------------------------------------------------------------