Getting milliseconds in Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mjs7231

    #1

    Getting milliseconds in Python

    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?

  • Diez B. Roggisch

    #2
    Re: Getting milliseconds in Python

    mjs7231 wrote:
    [color=blue]
    > 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?[/color]

    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.

    --
    Regards,

    Diez B. Roggisch

    Comment

    • mjs7231

      #3
      Re: Getting milliseconds in Python

      "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.

      Comment

      • John Hunter

        #4
        Re: Getting milliseconds in Python

        >>>>> "mjs7231" == mjs7231 <mjs7231@gmail. com> writes:

        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

        Comment

        • Diez B. Roggisch

          #5
          Re: Getting milliseconds in Python

          mjs7231 wrote:
          [color=blue]
          > "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.[/color]

          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.

          --
          Regards,

          Diez B. Roggisch

          Comment

          • jdonnell

            #6
            Re: Getting milliseconds in Python

            "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.

            Comment

            • Fredrik Lundh

              #7
              Re: Getting milliseconds in Python

              "mjs7231" <mjs7231@gmail. com> wrote:
              [color=blue]
              > "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.[/color]

              are you sure you know what a millisecond is?

              can you spot the milliseconds here:
              [color=blue][color=green][color=darkred]
              >>> import time
              >>> time.time()[/color][/color][/color]
              1108575508.234[color=blue][color=green][color=darkred]
              >>> time.time()[/color][/color][/color]
              1108575515.062

              or here:
              [color=blue][color=green][color=darkred]
              >>> time.clock()[/color][/color][/color]
              1.6349019714375 455[color=blue][color=green][color=darkred]
              >>> time.clock()[/color][/color][/color]
              2.2402415685960 024[color=blue][color=green][color=darkred]
              >>> time.clock()[/color][/color][/color]
              2.7715522631434 739

              </F>



              Comment

              • Brian Beck

                #8
                Re: Getting milliseconds in Python

                mjs7231 wrote:[color=blue]
                > This is no good, I am looking for milliseconds, not seconds.. as stated
                > above.[/color]

                That IS what you want.

                seconds * 100 = milliseconds

                --
                Brian Beck
                Adventurer of the First Order

                Comment

                • Fredrik Lundh

                  #9
                  Re: Getting milliseconds in Python

                  Brian Beck wrote:
                  [color=blue]
                  > That IS what you want.
                  >
                  > seconds * 100 = milliseconds[/color]

                  are you sure you know what a millisecond is?

                  (duck)



                  Comment

                  • Amand Tihon

                    #10
                    Re: Getting milliseconds in Python

                    Brian Beck wrote:[color=blue]
                    > That IS what you want.
                    >
                    > seconds * 100 = milliseconds[/color]

                    May I assume that this IS what you want ?

                    ()___
                    ()//__/)______________ ___()
                    ||(___)//#/_/#/_/#/_/#()/||
                    ||----|#| |#|_|#|_|#|_|| ||
                    ||____|_|#|_|#| _|#|_|#||/||
                    || |#|_|#|_|#|_|#| _||

                    :)

                    (credits to jgs, found on http://www.ascii-art.de/ascii/ab/bed.txt)

                    --
                    Amand Tihon

                    Comment

                    • Brian Beck

                      #11
                      Re: Getting milliseconds in Python

                      Fredrik Lundh wrote:[color=blue]
                      > Brian Beck wrote:
                      >
                      >[color=green]
                      >>That IS what you want.
                      >>
                      >>seconds * 100 = milliseconds[/color]
                      >
                      >
                      > are you sure you know what a millisecond is?
                      >
                      > (duck)[/color]

                      Touché.

                      But it was a typo.

                      --
                      Brian Beck
                      Adventurer of the First Order

                      Comment

                      • Curt

                        #12
                        Re: Getting milliseconds in Python

                        On 2005-02-16, Brian Beck <exogen@gmail.c om> wrote:[color=blue][color=green][color=darkred]
                        >>>
                        >>>seconds * 100 = milliseconds[/color]
                        >>
                        >>
                        >> are you sure you know what a millisecond is?
                        >>
                        >> (duck)[/color]
                        >
                        > Touché.
                        >
                        > But it was a typo.[/color]

                        Oh, you meant 'seconds / 100 = milliseconds'?

                        (canard)

                        Comment

                        • Brian Beck

                          #13
                          Re: Getting milliseconds in Python

                          Curt wrote:[color=blue]
                          > Oh, you meant 'seconds / 100 = milliseconds'?
                          >
                          > (canard)[/color]

                          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...

                          --
                          Brian Beck
                          Adventurer of the First Order

                          Comment

                          • Martin Christensen

                            #14
                            Re: Getting milliseconds in Python

                            -----BEGIN PGP SIGNED MESSAGE-----
                            Hash: SHA1
                            [color=blue][color=green][color=darkred]
                            >>>>> "Brian" == Brian Beck <exogen@gmail.c om> writes:[/color][/color][/color]
                            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>

                            iEYEARECAAYFAkI UxPkACgkQYu1fMm OQldXH3QCdGcw3g rK/R17kfakMBhxU1Io/
                            4ukAoJl+gysL9q/6j1G8LYPZz7NawE V+
                            =1CNL
                            -----END PGP SIGNATURE-----

                            Comment

                            • Brian Beck

                              #15
                              Re: Getting milliseconds in Python

                              Martin Christensen wrote:[color=blue]
                              > A math teacher! A math teacher! My kingdom for a math teacher!
                              >
                              > Martin[/color]

                              Man, this is the hottest topic on c.l.py since that Lazaridis guy...

                              --
                              Brian Beck
                              Adventurer of the First Order

                              Comment

                              Working...