Why Would an m-file run quickly but publishing it runs slowly?

7 views (last 30 days)
I have created a relatively short m-file that runs completely in ~10 seconds and produces the output I expect (couple of plots). However, publishing it to either a pdf or html causes it to slowdown. Very slow. It takes about 20 minutes to publish.
Are there particular functions I should look out for? I'm confused since it obviously solves very quickly.
This will probably require some discourse since I'm not going to post my code here (yet). This is a homework assignment and I don't want to post this online until it's due (later this week), so there is the ability to discuss it then.

Answers (1)

Ken Atwell
Ken Atwell on 19 Sep 2012
We'd need the code to be sure, but I'll guess that publishing is causing the JIT to not kick in some situation when normally it would kick in when running freely.
Bracketing each block of code with tic/toc will narrow down the culprit, and then maybe you can share just that one subsection here on Answers.
  2 Comments
Sean
Sean on 19 Sep 2012
Edited: Sean on 19 Sep 2012
for time = 0:delta_t:final_t
Determine current velocity
u_hat_val = u_hat_func(xi_val,eta_val);
v_hat_val = v_hat_func(xi_val,eta_val);
Determine amount to move for next timestep
delta_xi = u_hat_val*delta_t;
delta_eta = v_hat_val*delta_t;
Move particles
xi_val = xi_val + delta_xi;
eta_val = eta_val + delta_eta;
end
After tic/toc wrapping that section (using a coarse delta_t):
  • The run button gives me an average elapsed time of ~0.06 seconds
  • The publish button gives me an average elapsed time of ~9.8 seconds
The u_hat_func and v_hat_func are anonymous functions created using matlabFunction(). They're not the prettiest things in the world but they don't take that long to solve if I don't publish. And I want to use a much finer timestep in this for loop, but doing so causes even longer times.
EDIT: I just turned off the JIT and got the same time numbers seen above.
Ken Atwell
Ken Atwell on 19 Sep 2012
Turn all three "%%"'s into just "%". I believe publishing will pause to gather state whenever it encounters a "%%", so don't do this within your loop.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!