Ways to improve this clock algorithm?

Paul Rubin http
Mon Sep 15 17:33:16 EDT 2003


jacobsmail at gmx.net (Jacob H) writes:

> Hello all,
> 
> I'm close to being a novice programmer and I never was good at math.
> So I'm curious to know ways in which this following code can be made
> more efficient, more elegant. The code just calculates and displays
> elapsed wall clock seconds since initialization.
> 
> class ExampleTimer:
>     def __init__(self):
>         self.start = time.clock()
> 
>     def run(self):
>         while 1:
>             now = time.clock()
>             allseconds = int(now) = int(self.start)

I think you mean

              allseconds = int(now) - int(self.start)

Now you can say
            
              hours = allseconds // 3600
              minutes = (allseconds // 60) % 60
              seconds = allseconds % 60

instead of counting up to allseconds.  You could also use the divmod
function for a fancier way to do the same thing, but the above is
straightforward enough.




More information about the Python-list mailing list