2
$\begingroup$

Let $Tx=\frac{x}{2}$ for all $x\in[0,1]$. Let $x_{0}\in[0,1]$ and set an iterative sequence $\{x_{n}\}$ by the method $x_{n+1}=Tx_{n}$. Now if $x_{0}=0.8$, then I get a convergent sequence towards the unique fixed point of $T$ as follows. But I need the CPU time in this case. How I set the CPU time or where I can check the CPU time for method.

T[x_] := T[x] = (x/2);
x[0] = 0.9;
x[n_] := x[n] = T[x[n - 1]];
NumberForm[{Table[x[i], {i, 0, 6}]}, 9]
{{0.9, 0.45, 0.225, 0.1125, 0.05625, 0.028125, 0.0140625, 0.00703125}}
$\endgroup$
0

1 Answer 1

2
$\begingroup$

How I set the CPU time or where I can check the CPU time for method

One way is to use TimeUsed[] and at the end subtract the times. Something like this

t0 = TimeUsed[];
T[x_] := T[x] = (x/2);
x[0] = 0.9;
x[n_] := x[n] = T[x[n - 1]];
Quiet@NumberForm[{Table[x[i], {i, 0, 100000}]}, 9];
Print["time used in CPU seconds is ", TimeUsed[] - t0]

Which gives

Mathematica graphics

enter image description here

If you want clock time used (not CPU time), then you can use AbsoluteTiming but note this measures time elapsed, not CPU time. This are not the same. So it depends on what you want.

$\endgroup$
2
  • $\begingroup$ Thanks dear @Nasser thanks for you helpful answer. However with every click, the value changes for CPU time. For example, 0.25, 0.281 etc in my CPU $\endgroup$ Commented Feb 28, 2023 at 18:33
  • $\begingroup$ @JunaidAhmad you need to reset t0 (initial time) each time where you want to measure the CPU time used. $\endgroup$
    – Nasser
    Commented Feb 28, 2023 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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