[Tutor] Using the time module to extract a semi-random number
Kent Johnson
kent37 at tds.net
Fri Sep 18 13:01:48 CEST 2009
On Thu, Sep 17, 2009 at 4:59 PM, Katt <the_only_katala at verizon.net> wrote:
>
> ----- Original Message ----- From: "Kent Johnson" <kent37 at tds.net>
>> time.localtime().tm_sec will give you the number of >seconds as an
>> integer without any conversions.
>
> Thank you for your quick response. I didn't expect to get a response for a
> couple of days.
>
> As you know from the others that responded there are a couple of ways that
> it could be done. I am curious as to which one is more efficient.
>
> Your solution was:
> number = time.localtime().tm_sec
>
> The other solutions were
> number = int(time.strftime("%S")
> number = int(time.strftime("%S"(time.localtime()))
>
> Is it that the program could access yours faster than the others? Or is it
> that there are many ways to accomplish things in python and no one is better
> than the other?
They will all be fast enough that it doesn't matter. My guess is that
if you timed them (with the timeit module) my solution would be faster
because it doesn't require converting the time to a string and back to
an integer, but that is just a guess.
The second two are equivalent because strftime() uses localtime() as
the time if it is not provided.
You should use whichever one you feel is more readable and expressive.
Kent
PS Please Reply All to reply to the list.
More information about the Tutor
mailing list