From: darthshak on
I am running MATLAB R2008b.

I have a MEX-function, calc_E, which runs 499500 times in a loop. This function is called in a statement in an M-function, 'E = calc_E(D)'.

The problem is, according to the profiler, calc_E itself took 3.383 s to execute, but the statement 'E = calc_E(D)' took 11.539 s. I tried running the profiler with -detail builtin (R2008b doesnt allow any further than this), but it didn't reveal anything more. Considering that this statement is the rate determining step in my program, it is important that I optimize it.

What is the cause of this extra overhead and how do I eliminate it?
From: Jan Simon on
Dear Darthshak!

> I have a MEX-function, calc_E, which runs 499500 times in a loop. This function is called in a statement in an M-function, 'E = calc_E(D)'.
>
> The problem is, according to the profiler, calc_E itself took 3.383 s to execute, but the statement 'E = calc_E(D)' took 11.539 s. I tried running the profiler with -detail builtin (R2008b doesnt allow any further than this), but it didn't reveal anything more. Considering that this statement is the rate determining step in my program, it is important that I optimize it.

I assume that E is allocated in each run.
Does the size of E differ for each run?
Please post more code.

Jan
From: darthshak on
Nopes, the size of E is fixed in each run.

I did try to make the memory persistent, but to no avail. I seem to be encountering this issue all over the place.
From: Jan Simon on
Dear darthshak!

> Nopes, the size of E is fixed in each run.

Then try to reuse the memory reserved for E:
E(:) = calc_E(...)

> I did try to make the memory persistent, but to no avail. I seem to be encountering this issue all over the place.

"All over the place" does not explain enough to reproduce the behaviour. Please post the relevant part of the source code!

Kind regards, Jan
From: Vishaka on
Jan, sorry about forgetting to post the code.

I've figured out the problem myself though. I forgot to take into account the fact that MATLAB cant optimize away the overhead of repeatedly calling a MEX function (in this, case half a million times), as it doesnt quite know what the MEX function is doing.