time function problem

Diez B. Roggisch deets at nospam.web.de
Thu Nov 27 20:35:10 CET 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.

start_time = time()

<do_something>

elapsed = time() - start_time


Diez



More information about the Python-list mailing list