0
$\begingroup$

I want to find the computational time (CPU time) of my code.

When I use TimeUsed or Timing commands they give the CPU time of only one cell. But my program contain many expressions including loops, is there any way to find CPU time of more than one expressions?

Or is there any way to find the starting time of computation and ending time of computation.


Thanks blochwave for giving me a detailed answer, but I have already used all these methods and none of them has worked well, it is clearly written in mathematica "get help" file that the Timing and AbsoluteTiming calculates the time of ONE expression only , and when I use in one cell with your suggestion. it gives me time of my code 0.0156 and using on all other codes the result is same 0.0156, no change with other codes, it means it is giving the time of only one expression as i have all my codes have first expression same but their other are different and complicated expressions, I have also used ClearSystemCache so to avoid the the memorized values but getting same result.

$\endgroup$
  • 1
    $\begingroup$ There should be a better way but I assume that starttime = AbsoluteTime[];...;endtime = AbsoluteTime[]; endtime-starttime would give an idea. $\endgroup$ – Öskå Jan 7 '15 at 12:11
  • 1
    $\begingroup$ If everything is in a single cell, you can do AbsoluteTiming[expr1;expr2;expr3;] $\endgroup$ – dr.blochwave Jan 7 '15 at 12:21
  • 1
    $\begingroup$ Have you seen this? (7768). I found that I (sometimes?) need to copy my entire code and wrap it in RuntimeTools`Profile for this to work properly, but it should let you know where the time is being spent if you get it working. $\endgroup$ – Mr.Wizard Jan 9 '15 at 9:15
  • $\begingroup$ You might need to clarify - "calculates the time of ONE expression only" - reference.wolfram.com/language/ref/AbsoluteTiming.html says that, to me, it calculates the time of anything within the brackets. Look at the first example - it's doing 3 different things and timing the whole thing... $\endgroup$ – dr.blochwave Jan 9 '15 at 11:44
3
$\begingroup$

This is one solution, encapsulating all your expressions in the form: AbsoluteTiming[expr1;expr2;].

AbsoluteTiming[
  a = Range[123456];
  Pause[1];
  Total[a]
]

(* 1.01503 seconds, returns 7620753696 *)

Needs["GeneralUtilities`"]
AccurateTiming[
  a = Range[123456];
  Pause[1];
  Total[a]
]

(* 1.001246 seconds *)

Also works fine with Timing[] for just the CPU time.

Timing[
  a = Range[123456];
  Pause[1];
  Total[a]
]

(* 0.016002 seconds, returns 7620753696 *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.