Milliseconds.....

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

    Milliseconds.....

    Can I operate on milliseconds in C? Is there a standard library where I
    could retreive the system time in a better resolution than seconds.....? The
    time_t gives me only the time like 12:32:11....... ..
    Any ideas.....?

    Best Regards
    Terry


  • Allan Bruce

    #2
    Re: Milliseconds... ..


    "Terry Andersen" <terr@sea.com > wrote in message
    news:bgqteb$9oo $1@news.net.uni-c.dk...[color=blue]
    > Can I operate on milliseconds in C? Is there a standard library where I
    > could retreive the system time in a better resolution than seconds.....?[/color]
    The[color=blue]
    > time_t gives me only the time like 12:32:11....... ..
    > Any ideas.....?
    >
    > Best Regards
    > Terry
    >
    >[/color]

    have a look at clock()
    I think its part of the standard for millisecond resolution
    Allan


    Comment

    • Dan Pop

      #3
      Re: Milliseconds... ..

      In <bgqteb$9oo$1@n ews.net.uni-c.dk> "Terry Andersen" <terr@sea.com > writes:
      [color=blue]
      >Can I operate on milliseconds in C?[/color]

      Maybe. The C standard doesn't specify the resolution of its time
      functions, does it?
      [color=blue]
      >Is there a standard library where I
      >could retreive the system time in a better resolution than seconds.....?[/color]

      The standard library doesn't guarantee any resolution for its time
      functions.
      [color=blue]
      >The time_t gives me only the time like 12:32:11....... ..[/color]

      That's a property of your implementation.
      [color=blue]
      >Any ideas.....?[/color]

      Everything is implementation-specific in this area. If the standard
      library functions aren't good enough for your purpose, try to check what
      extensions your implementation provides.

      On Unix systems, gettimeofday works with microsecond resolution
      (if the underlying hardware can actually support such a resolution).

      Dan
      --
      Dan Pop
      DESY Zeuthen, RZ group
      Email: Dan.Pop@ifh.de

      Comment

      • Allan Bruce

        #4
        Re: Milliseconds... ..


        "Richard Bos" <rlb@hoekstra-uitgeverij.nl> wrote in message
        news:3f3102d9.1 815877061@news. nl.net...[color=blue]
        > "Allan Bruce" <allanmb@TAKEAW AYf2s.com> wrote:
        >[color=green]
        > > "Terry Andersen" <terr@sea.com > wrote in message
        > > news:bgqteb$9oo $1@news.net.uni-c.dk...[color=darkred]
        > > > Can I operate on milliseconds in C? Is there a standard library where[/color][/color][/color]
        I[color=blue][color=green][color=darkred]
        > > > could retreive the system time in a better resolution than[/color][/color][/color]
        seconds.....?[color=blue][color=green][color=darkred]
        > > > The time_t gives me only the time like 12:32:11....... ..
        > > > Any ideas.....?[/color]
        > >
        > > have a look at clock()
        > > I think its part of the standard for millisecond resolution[/color]
        >
        > Not even close. clock() returns the processor time used by the program,
        > not the current clock time;[/color]

        You are correct, it has nothing to do with the system time at all.
        [color=blue]
        > moreover, you have no guarantee whatsoever
        > about the precision of its return value.[/color]

        Is this strictly true? The time.h header also specifies CLOCKS_PER_SEC which
        one can use in order to find out the time in seconds. If CLOCKS_PER_SEC is
        1000 then we know that the resolution is microsecond.
        [color=blue]
        >
        > Richard[/color]


        Comment

        • Eric G.

          #5
          Re: Milliseconds... ..

          Terry Andersen <terr@sea.com > wrote:
          [color=blue]
          > Can I operate on milliseconds in C? Is there a standard library where I
          > could retreive the system time in a better resolution than seconds.....? The
          > time_t gives me only the time like 12:32:11....... ..
          > Any ideas.....?[/color]

          You can use C and operate on milliseconds. However, the details of this
          would be specific to your machine.

          It is possible the timing functions built into your machine, should they
          support resolving to milliseconds, are not very accurate, in which case
          you will need to look for a 3rd party solution.



          --
          == Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
          "Therefore the considerations of the intelligent always include both
          benefit and harm." - Sun Tzu
          == Insults, like violence, are the last refuge of the incompetent... ===

          Comment

          • Richard Bos

            #6
            Re: Milliseconds... ..

            "Allan Bruce" <allanmb@TAKEAW AYf2s.com> wrote:
            [color=blue]
            > "Richard Bos" <rlb@hoekstra-uitgeverij.nl> wrote in message
            > news:3f3102d9.1 815877061@news. nl.net...[color=green]
            > > Not even close. clock() returns the processor time used by the program,
            > > not the current clock time;[/color]
            >
            > You are correct, it has nothing to do with the system time at all.
            >[color=green]
            > > moreover, you have no guarantee whatsoever
            > > about the precision of its return value.[/color]
            >
            > Is this strictly true? The time.h header also specifies CLOCKS_PER_SEC which
            > one can use in order to find out the time in seconds. If CLOCKS_PER_SEC is
            > 1000 then we know that the resolution is microsecond.[/color]

            Yes, this is strictly true, and no, we don't know that. A system that
            defines CLOCKS_PER_SEC as 1000, but increases clock() by 10000 every ten
            seconds is very poor quality, but legal. More realistically, a system
            that defines CLOCKS_PER_SEC as 182, and increases clock() by 10 18.2
            times per second is also legal.

            Richard

            Comment

            • Eric Sosman

              #7
              Re: Milliseconds... ..

              Allan Bruce wrote:[color=blue]
              >
              > "Richard Bos" <rlb@hoekstra-uitgeverij.nl> wrote in message
              > news:3f3102d9.1 815877061@news. nl.net...[color=green]
              > > "Allan Bruce" <allanmb@TAKEAW AYf2s.com> wrote:
              > >[color=darkred]
              > > > "Terry Andersen" <terr@sea.com > wrote in message
              > > > news:bgqteb$9oo $1@news.net.uni-c.dk...
              > > > > Can I operate on milliseconds in C? Is there a standard library where[/color][/color]
              > I[color=green][color=darkred]
              > > > > could retreive the system time in a better resolution than[/color][/color]
              > seconds.....?[color=green][color=darkred]
              > > > > The time_t gives me only the time like 12:32:11....... ..
              > > > > Any ideas.....?
              > > >
              > > > have a look at clock()
              > > > I think its part of the standard for millisecond resolution[/color]
              > >
              > > Not even close. clock() returns the processor time used by the program,
              > > not the current clock time;[/color]
              >
              > You are correct, it has nothing to do with the system time at all.
              >[color=green]
              > > moreover, you have no guarantee whatsoever
              > > about the precision of its return value.[/color]
              >
              > Is this strictly true? The time.h header also specifies CLOCKS_PER_SEC which
              > one can use in order to find out the time in seconds. If CLOCKS_PER_SEC is
              > 1000 then we know that the resolution is microsecond.[/color]

              You mean "millisecon d," but it doesn't really matter:
              CLOCKS_PER_SEC tells us the units in which `clock_t' expresses
              its value, but not the accuracy with which that value is
              measured.

              ("Huh?")

              One light-year is the distance a photon travels in one
              year in an undisturbed vacuum. The "units" program available
              on many Unix systems tells me that this distance is 9.460528e+15
              meters. Does that mean that the length of the light-year is
              known to an accuracy of plus-or-minus half a meter? Of course
              not: it just means that the meter is one of the standard units
              in which length is expressed.

              On the system I'm using at the moment, CLOCKS_PER_SEC is
              one million, meaning that `clock_t' values are expressed to
              a precision of one microsecond. But the underlying hardware
              clock ticks at 100Hz, so clock() cannot actually measure an
              interval shorter than ten milliseconds.

              Thought experiment: Express your age as a `clock_t' value
              (ignoring possible overflow), using CLOCKS_PER_SEC as it's
              defined on your favorite platform. Do you believe the answer?

              Precision is one thing, accuracy is another.

              --
              Eric.Sosman@sun .com

              Comment

              • Dan Pop

                #8
                Re: Milliseconds... ..

                In <bgr0pn$ik5$1@n ews.freedom2sur f.net> "Allan Bruce" <allanmb@TAKEAW AYf2s.com> writes:

                [color=blue]
                >"Richard Bos" <rlb@hoekstra-uitgeverij.nl> wrote in message
                >news:3f3102d9. 1815877061@news .nl.net...
                >[color=green]
                >> moreover, you have no guarantee whatsoever
                >> about the precision of its return value.[/color]
                >
                >Is this strictly true?[/color]

                Yup!
                [color=blue]
                >The time.h header also specifies CLOCKS_PER_SEC which
                >one can use in order to find out the time in seconds. If CLOCKS_PER_SEC is
                >1000 then we know that the resolution is microsecond.[/color]

                Nope, you don't!

                fangorn:~/tmp 2298> cat clockres.c
                #include <stdio.h>
                #include <time.h>

                int main()
                {
                clock_t t1 = clock(), t2;
                while ((t2 = clock()) == t1) ;
                printf("CLOCKS_ PER_SEC: %.0f resolution: %f sec\n",
                (double)CLOCKS_ PER_SEC, (t2 - t1) / (double)CLOCKS_ PER_SEC);
                return 0;
                }
                fangorn:~/tmp 2299> gcc clockres.c
                fangorn:~/tmp 2300> ./a.out
                CLOCKS_PER_SEC: 1000000 resolution: 0.010000 sec

                As you can see, CLOCKS_PER_SEC is merely a conversion factor, it provides
                no indication WRT the resolution of the clock() function. The resolution
                can only be determined at run time, as shown above.

                Dan
                --
                Dan Pop
                DESY Zeuthen, RZ group
                Email: Dan.Pop@ifh.de

                Comment

                • E. Robert Tisdale

                  #9
                  Re: Milliseconds... ..

                  Terry Andersen wrote:
                  [color=blue]
                  > Can I operate on milliseconds in C?[/color]

                  There are no standard C library functions
                  which guarantee resolution down to mulliseconds.
                  [color=blue]
                  > Is there a standard library where I could retrieve the system time
                  > in a better resolution than seconds?[/color]

                  clock_t clock(void);

                  returns CPU time in seconds/CLOCKS_PER_SEC
                  but the resolution may be much coarser.
                  Usually, there is no more than one system clock
                  that the operating system uses to update system timers
                  which may be updated no more frequently than
                  the operating system changes context.
                  If your time sharing operating has time slices of 0.1 seconds,
                  the resolution may be no better that 100 milliseconds.
                  [color=blue]
                  > The time_t gives me only the time like 12:32:11....... ..
                  >
                  > Any ideas.....?[/color]

                  Search your operating system documentation
                  for details of timers available to you.

                  Comment

                  • Dan Pop

                    #10
                    Re: Milliseconds... ..

                    In <3F3129DA.30407 00@jpl.nasa.gov > "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > writes:
                    [color=blue]
                    >Terry Andersen wrote:
                    >[color=green]
                    >> Can I operate on milliseconds in C?[/color]
                    >
                    >There are no standard C library functions
                    >which guarantee resolution down to mulliseconds.
                    >[color=green]
                    >> Is there a standard library where I could retrieve the system time
                    >> in a better resolution than seconds?[/color]
                    >
                    > clock_t clock(void);
                    >
                    >returns CPU time in seconds/CLOCKS_PER_SEC
                    >but the resolution may be much coarser.
                    >Usually, there is no more than one system clock
                    >that the operating system uses to update system timers
                    >which may be updated no more frequently than
                    >the operating system changes context.[/color]

                    This may be true for clock() but shouldn't affect time(), which could
                    provide the same resolution as the hardware implementing the system's
                    real time clock (which is slightly less than one microsecond on normal
                    PC's). The only reason this doesn't happen in practice is that most
                    implementations provide POSIX semantics for time_t. The standard C
                    difftime's resolution is theoretically limited only be the representation
                    of double precision values.

                    Dan
                    --
                    Dan Pop
                    DESY Zeuthen, RZ group
                    Email: Dan.Pop@ifh.de

                    Comment

                    Working...