Question: How to measure CPU time used by a computation and return the result and the time used?

Hello.

Two related questions: I'd like to measure the CPU time used by a computation. Should I use with(CodeTools)? or use  time[real]? What would be the most accurate way to measure the CPU time used by one call, say dsolve()?

Also, How would one return both the result of the computation and the time used in one call so that one does not have to make two separate calls: to obtain the result and to obtain the CPU time?

Example:

restart;
with(CodeTools):
ode:=2*a*x^3*y(x)^3+2*x*y(x)+diff(y(x), x);
result:=Usage(dsolve(ode=0,y(x)),output='cputime');

Gives result:=0.141 while

restart;
ode:=2*a*x^3*y(x)^3+2*x*y(x)+diff(y(x), x);
result:=time[real](dsolve(ode=0,y(x)));

Gives result:= 0.145

But in both cases, I have to make another call to get the result of the DSolve in this example. result:=dsolve(ode=0,y(x)); which I'd like to avoid if possible. 

I am using Maple 18

 

Please Wait...