time.time()

Peter Otten __peter__ at web.de
Sat Jan 24 22:55:19 CET 2004


Bart Nessux wrote:

> Terry Carroll wrote:
> 
>> It sounds like you're thinking time.time() does something else, which
>> you're trying to do.  What is that?  There might be another function for
>> you.
> 
> I should have been more clear... here's the code:

That's not only clearer - that's different :-)

> x = time.time()
> fp = os.popen("some_process", "r")
> fp.read()
> fp.close()
> print (time.time()-x/60)/60
> 
> If fp runs for many hours (in my case it does), I want time.time() to
> return the time it runs in seconds which I would then divide by 60 to
> get the total amount of minutes which I would then divide by 60 to get
> the total amount of hours that the process has ran... does that make
> sense? I don't need to be super precise, just within 1 minute or so of
> the actual amount of time that the process required to run.

>>> start = time()
>>> start
1074980579.623781
>>> sleep(7200)
>>> duration = time() - start 
>>> print "duration in hours:", duration/60/60
duration in hours: 2.0
>>> (time() - start) / 60 / 60
2.0

In the last expression, the parentheses are necessary, because - has lower
precedence than /, i. e. otherwise you would calculate time() -
(start/60/60). Note how I introduced an additional "duration" variable. If
a calculation does not yield the expected result, it is often helpful to
inspect some intermediate values.

Peter

PS: I cheated with my sleep() and time() functions:

>>> def time():
...     global _time
...     try:
...             return _time
...     except NameError:
...             import time
...             _time = time.time()
...     return _time
...
>>> def sleep(s):
...     global _time
...     try:
...             _time += s
...     except NameError:
...             import time
...             _time = time.time() + s




More information about the Python-list mailing list