time function problem

MRAB google at mrabarnett.plus.com
Thu Nov 27 14:43:18 EST 2008


willie wrote:
> My code:
> 
> from time import time
> def leibniz(terms):
> 
> 
>     acc = 0.0
>     num = 4.0 # numerator value remains constant in the series
>     den = 1
>     count = 0
>     start_time = 0.0
>     for aterm in range(terms):
>         nextterm = num/den  * (-1)**aterm # (-1) allows fractions to
> alternate
>                                         #between a + and a - value
>         acc = acc + nextterm
> 
>         den = den + 2   #denominator increments by 2
>         count = count + 1
>         #print(count,acc)
> 
> 
>         print("Time elapsed: %f"%( time()- start_time))
>     return acc
> 
> The result I get is --  Time elapsed: 1227812482.390000 but I want to
> know the real time this code needed to  run. Using a term value of
> 1000 I can see from the begining to end the actual time lapse is a
> little over 6 seconds. How do I get that value (6 + secs approx) as my
> time lapse.
> 
time() gives you the current time in seconds since the Epoch (1 Jan 
1970). You should set start_time to time() instead of 0.0 at the start.

Note that this will give you the elapsed time, not the CPU time. If you 
want the CPU (processing) time then use clock().



More information about the Python-list mailing list