0

I need to run a code during a specific time

for example, in Matlab I could do this easy as

k=1
Finaltime=zeros(1,300);
Finaltime(k)=0; 
Max_time=30;
tic
while(toc <= Max_time)

do somthing;
k=k+1;
FinalTime(k)= toc
end

How to I replicated this in Python.

Farouk Yahaya
  • 43
  • 1
  • 8
  • 2
    Just a precision: In matlab `tic` and `toc` do not measure the CPU time, it measure the wall-clock time. The CPU time can be measured with `cputime` (matlab). – obchardon Nov 20 '19 at 10:29
  • So how can i measure wallclock time in Python like in the above code please help – Farouk Yahaya Nov 20 '19 at 10:37
  • Related: https://stackoverflow.com/questions/5849800/what-is-the-python-equivalent-of-matlabs-tic-and-toc-functions – Daniel Dec 10 '19 at 20:22

1 Answers1

0

ok I solved my problem. Not perfect but should do.

import time
Tmax=30
k=1
initTime=time.time()
FinalTime=time.time()-initTime
while FinalTime <= Tmax: # check if timer is still less than Tmax
    for j in range(1,3):
         ...
       #DoSomething
         ...
    FinalTime=time.time()-initTime  #update timer
Farouk Yahaya
  • 43
  • 1
  • 8