Getting milliseconds in Python

M

mjs7231

I am trying to record how long an operation takes, but can't seem to
find a function that will allow me to record the timestamp in
milliseconds, maybe I am looking in the wrong place?
 
D

Diez B. Roggisch

mjs7231 said:
I am trying to record how long an operation takes, but can't seem to
find a function that will allow me to record the timestamp in
milliseconds, maybe I am looking in the wrong place?

I have no idea where you look - but the time-module has IMHO a descriptive
enough name - so look there and be a happy camper.
 
J

John Hunter

mjs7231> This is no good, I am looking for milliseconds, not
mjs7231> seconds.. as stated above.

Well seconds/1000.0 = millseconds -- or are you worries about floating
point error?

7 >>> from datetime import datetime
8 >>> dt = datetime.now()
9 >>> dt.microsecond
Out[9]: 20222

Converting to milliseconds is left as an exercise for the reader...

See also the timeit module...


JDH
 
M

mjs7231

"Return the time as a floating point number expressed in seconds since
the epoch, in UTC. Note that even though the time is always returned as
a floating point number, not all systems provide time with a better
precision than 1 second. While this function normally returns
non-decreasing values, it can return a lower value than a previous call
if the system clock has been set back between the two calls. "

This is no good, I am looking for milliseconds, not seconds.. as stated
above.
 
D

Diez B. Roggisch

mjs7231 said:
"Return the time as a floating point number expressed in seconds since
the epoch, in UTC. Note that even though the time is always returned as
a floating point number, not all systems provide time with a better
precision than 1 second. While this function normally returns
non-decreasing values, it can return a lower value than a previous call
if the system clock has been set back between the two calls. "

This is no good, I am looking for milliseconds, not seconds.. as stated
above.

If your system _can_ provide better accuracy than seconds, it is returned as
fraction of a second. That is the whole point the result of time being a
float and not an int.
 
J

jdonnell

"This is no good, I am looking for milliseconds, not seconds.. as
stated
above. "

The docs are not very clear. I had the same issue when I was trying to
do the same thing, but the time and datetime modules return
milliseconds on my linux machines.
 
F

Fredrik Lundh

mjs7231 said:
"Return the time as a floating point number expressed in seconds since
the epoch, in UTC. Note that even though the time is always returned as
a floating point number, not all systems provide time with a better
precision than 1 second. While this function normally returns
non-decreasing values, it can return a lower value than a previous call
if the system clock has been set back between the two calls. "

This is no good, I am looking for milliseconds, not seconds.. as stated
above.

are you sure you know what a millisecond is?

can you spot the milliseconds here:
1108575515.062

or here:
2.7715522631434739

</F>
 
B

Brian Beck

Curt said:
Oh, you meant 'seconds / 100 = milliseconds'?

(canard)

I assume you're suggesting that there are two typos in my original post
(the * and the 100)...

Despite a millisecond being a thousandth of a second, given the number
of seconds provided by the time module, he does have to *multiply* by a
thousand to get the number of milliseconds.

2 seconds * 1000 = 2000 milliseconds

So, aside from the 100 in the original post, it may look misleading, but
that is what he would need to do...
 
M

Martin Christensen

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Brian> Despite a millisecond being a thousandth of a second [...]

A math teacher! A math teacher! My kingdom for a math teacher!

Martin

- --
Homepage: http://www.cs.auc.dk/~factotum/
GPG public key: http://www.cs.auc.dk/~factotum/gpgkey.txt
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)
Comment: Using Mailcrypt+GnuPG <http://www.gnupg.org>

iEYEARECAAYFAkIUxPkACgkQYu1fMmOQldXH3QCdGcw3grK/R17kfakMBhxU1Io/
4ukAoJl+gysL9q/6j1G8LYPZz7NawEV+
=1CNL
-----END PGP SIGNATURE-----
 
M

Martin Christensen

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Brian> Man, this is the hottest topic on c.l.py since that Lazaridis
Brian> guy...

.... which was really the point of my joke, even if it did belly flop
somewhat. This whole discussions brought to mind a cartoon where a
group of doctors were performing open heart surgery. One of them says,
"Okay, how many of us believe that the heart has _four_ chambers?,"
and a few of the others raise their hands. I intended it as a 'let's
call in the professors to determine if 2+2=4', but, well... :)

Martin

- --
Homepage: http://www.cs.auc.dk/~factotum/
GPG public key: http://www.cs.auc.dk/~factotum/gpgkey.txt
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)
Comment: Using Mailcrypt+GnuPG <http://www.gnupg.org>

iEYEARECAAYFAkIUyzAACgkQYu1fMmOQldWVdACdHLxtUi4TtFCl0ZW6wf65Hu9M
ioMAoMWAHrkS5lhQw5V0cJXVO1nk76MO
=6MXl
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top