clock() function

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

    #16
    Re: clock() function

    Dan.Pop@cern.ch (Dan Pop) wrote in message news:<bm3j15$mu 7$6@sunnews.cer n.ch>...[color=blue]
    > In <3f852bd2$0$274 72$91cee783@new sreader02.highw ay.telekom.at> "Robert Stankowic" <pcdoktor@netwa y.at> writes:
    >
    >[color=green]
    > >Just one afterthought:
    > >I could imagine, that the return value of clock() wraps if clock()
    > >internally uses a counter of type clock_t, but is -1 if clock() internally
    > >uses a type with a higher range.
    > >But that is just a wild guess..
    > >Anyone a more precise statement?[/color]
    >
    > The words: "the implementation' s best approximation" effectively mean that
    > an implementation can return *anything* from a clock() call, including a
    > negative value, without having its conformance affected.
    >
    > I would hope that an implementation as described by the OP (i.e. most
    > Unix implementations ) constantly returns -1 after 36 minutes of CPU time,[/color]

    No! Why would you hope that? To teach those of us who use
    wraparounds a lesson?) In fact every system I know of use
    wraparounds! This admittedly doesn't include all that many
    systems but at least the common ones. It would be interesting
    to hear about systems that don't do wraparounds (and at the
    same time have a small clock type).

    Given that one needs clock timings greater than 36 mins, how should
    one achieve that? You could go for a non-C solution. That might get
    messy though if you code for many platforms? (I have never chosen
    that approach so I wouldn't know.) Or you could assume that clock()
    works in the usual and IMO useful way, which IMO includes
    wraparounds when they make sense, and use that to make some C
    library that is fairly portable.
    [color=blue]
    > but I wouldn't bet on that.[/color]

    I bet that there is a fair amount of C code using that clock() do
    wrap around on systems where that has been common practice.
    [color=blue]
    > Especially after reading the Linux man page:
    >
    > BUGS
    > ...
    > Note that the time can wrap around. On a 32bit system
    > where CLOCKS_PER_SEC equals 1000000 this function will
    > return the same value approximately every 72 minutes.
    >
    > I've never needed to use clock() for intervals longer than a few minutes,
    > so I've never tested its behaviour on overflow on any implementation.
    >
    > Most implementations provide better functions for this purpose, e.g. the
    > Unix times (although it still uses clock_t, the conversion factor is no
    > longer CLOCKS_PER_SEC, but sysconf(_SC_CLK _TCK), which has a reasonable
    > value, reflecting the real resolution of these values).[/color]

    But clock() with wraparounds is adequate, at least if you don't take
    into account the nuisance of coding some library to take care of the
    wraparounds and clock timings, which only needs to be done once
    anyway.


    Daniel Vallstrom

    [color=blue]
    >
    > Dan[/color]

    Comment

    • Keith Thompson

      #17
      Re: clock() function

      Pushkar Pradhan <pushkar@gri.ms state.edu> writes:[color=blue]
      > I finally decided to replace clock() since it was causing so much confusion.
      > I have replaced it with system specific gettimeofday() call which has
      > enough resolution (microsecs) I think.[/color]

      <OT>

      Note that gettimeofday() gives you the actual wall clock time, whereas
      clock() gives you the amount of CPU time used by your program. On any
      system with concurrent processes, these can be very different.

      As long as you're being system-specific, take a look at the times()
      function.

      </OT>

      --
      Keith Thompson (The_Other_Keit h) kst@cts.com <http://www.ghoti.net/~kst>
      San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
      Schroedinger does Shakespeare: "To be *and* not to be"

      Comment

      • Dan Pop

        #18
        Re: clock() function

        In <171aec1a.03100 91139.4248a36e@ posting.google. com> Daniel.Vallstro m@safelogic.se (Daniel Vallstrom) writes:
        [color=blue]
        >Dan.Pop@cern.c h (Dan Pop) wrote in message news:<bm3j15$mu 7$6@sunnews.cer n.ch>...[color=green]
        >> In <3f852bd2$0$274 72$91cee783@new sreader02.highw ay.telekom.at> "Robert Stankowic" <pcdoktor@netwa y.at> writes:
        >>
        >>[color=darkred]
        >> >Just one afterthought:
        >> >I could imagine, that the return value of clock() wraps if clock()
        >> >internally uses a counter of type clock_t, but is -1 if clock() internally
        >> >uses a type with a higher range.
        >> >But that is just a wild guess..
        >> >Anyone a more precise statement?[/color]
        >>
        >> The words: "the implementation' s best approximation" effectively mean that
        >> an implementation can return *anything* from a clock() call, including a
        >> negative value, without having its conformance affected.
        >>
        >> I would hope that an implementation as described by the OP (i.e. most
        >> Unix implementations ) constantly returns -1 after 36 minutes of CPU time,[/color]
        >
        >No! Why would you hope that?[/color]

        Because this is what the standard actually says:

        If the processor time used is not available or its value cannot
        ^^^^^^^^^^^^^^^ ^^^^
        be represented, the function returns the value (clock_t)(-1)
        ^^^^^^^^^^^^^^

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

        Comment

        • Mike Wahler

          #19
          Re: clock() function


          "Dan Pop" <Dan.Pop@cern.c h> wrote in message
          news:bm65t9$ekm $5@sunnews.cern .ch...[color=blue]
          > In <171aec1a.03100 91139.4248a36e@ posting.google. com>[/color]
          Daniel.Vallstro m@safelogic.se (Daniel Vallstrom) writes:[color=blue]
          >[color=green]
          > >Dan.Pop@cern.c h (Dan Pop) wrote in message[/color][/color]
          news:<bm3j15$mu 7$6@sunnews.cer n.ch>...[color=blue][color=green][color=darkred]
          > >> In <3f852bd2$0$274 72$91cee783@new sreader02.highw ay.telekom.at> "Robert[/color][/color][/color]
          Stankowic" <pcdoktor@netwa y.at> writes:[color=blue][color=green][color=darkred]
          > >>
          > >>
          > >> >Just one afterthought:
          > >> >I could imagine, that the return value of clock() wraps if clock()
          > >> >internally uses a counter of type clock_t, but is -1 if clock()[/color][/color][/color]
          internally[color=blue][color=green][color=darkred]
          > >> >uses a type with a higher range.
          > >> >But that is just a wild guess..
          > >> >Anyone a more precise statement?
          > >>
          > >> The words: "the implementation' s best approximation" effectively mean[/color][/color][/color]
          that[color=blue][color=green][color=darkred]
          > >> an implementation can return *anything* from a clock() call, including[/color][/color][/color]
          a[color=blue][color=green][color=darkred]
          > >> negative value, without having its conformance affected.
          > >>
          > >> I would hope that an implementation as described by the OP (i.e. most
          > >> Unix implementations ) constantly returns -1 after 36 minutes of CPU[/color][/color][/color]
          time,[color=blue][color=green]
          > >
          > >No! Why would you hope that?[/color]
          >
          > Because this is what the standard actually says:
          >
          > If the processor time used is not available or its value cannot
          > ^^^^^^^^^^^^^^^ ^^^^
          > be represented, the function returns the value (clock_t)(-1)
          > ^^^^^^^^^^^^^^[/color]

          Should I understand this to mean that the 'wrapping'
          to zero described by the documentation posted by OP
          is not an allowed 'implementation-defined' component
          of 'clock()' behavior? "Zero" might not be of any
          practical use, but perhaps it *could* be called
          "best approximation" rather than "not representable".

          Are saying that a 'clock()' which "wraps" to zero is
          definitely not standard conforming?

          -Mike


          Comment

          • Keith Thompson

            #20
            Re: clock() function

            "Mike Wahler" <mkwahler@mkwah ler.net> writes:[color=blue]
            > "Dan Pop" <Dan.Pop@cern.c h> wrote in message
            > news:bm65t9$ekm $5@sunnews.cern .ch...[/color]
            [...][color=blue][color=green]
            > > Because this is what the standard actually says:
            > >
            > > If the processor time used is not available or its value cannot
            > > ^^^^^^^^^^^^^^^ ^^^^
            > > be represented, the function returns the value (clock_t)(-1)
            > > ^^^^^^^^^^^^^^[/color]
            >
            > Should I understand this to mean that the 'wrapping'
            > to zero described by the documentation posted by OP
            > is not an allowed 'implementation-defined' component
            > of 'clock()' behavior? "Zero" might not be of any
            > practical use, but perhaps it *could* be called
            > "best approximation" rather than "not representable".
            >
            > Are saying that a 'clock()' which "wraps" to zero is
            > definitely not standard conforming?[/color]

            On one system I tested yesterday (Solaris 8), clock_t is a 32-bit
            signed integer type. The values returned by the clock() function wrap
            around to negative values. That's far more useful than returning -1
            after 2147 seconds, but it looks like it violates the standard.

            Probably be best solution in this case would be to make clock_t a
            64-bit type (as it already is on some platforms).

            --
            Keith Thompson (The_Other_Keit h) kst@cts.com <http://www.ghoti.net/~kst>
            San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
            Schroedinger does Shakespeare: "To be *and* not to be"

            Comment

            • Daniel Vallstrom

              #21
              Re: clock() function

              Dan.Pop@cern.ch (Dan Pop) wrote in message news:<bm65t9$ek m$5@sunnews.cer n.ch>...[color=blue]
              > In <171aec1a.03100 91139.4248a36e@ posting.google. com> Daniel.Vallstro m@safelogic.se (Daniel Vallstrom) writes:
              >[color=green]
              > >Dan.Pop@cern.c h (Dan Pop) wrote in message news:<bm3j15$mu 7$6@sunnews.cer n.ch>...[color=darkred]
              > >> The words: "the implementation' s best approximation" effectively mean that
              > >> an implementation can return *anything* from a clock() call, including a
              > >> negative value, without having its conformance affected.
              > >>
              > >> I would hope that an implementation as described by the OP (i.e. most
              > >> Unix implementations ) constantly returns -1 after 36 minutes of CPU time,[/color]
              > >
              > >No! Why would you hope that?[/color]
              >
              > Because this is what the standard actually says:
              >
              > If the processor time used is not available or its value cannot
              > ^^^^^^^^^^^^^^^ ^^^^
              > be represented, the function returns the value (clock_t)(-1)
              > ^^^^^^^^^^^^^^[/color]

              Yeah, from our favorite legalistic point of view that would be the
              thing to hope for. I forgot;p

              Daniel Vallstrom
              [color=blue]
              >
              > Dan[/color]

              Comment

              • Jeffrey D. Smith

                #22
                Re: clock() function

                "Keith Thompson" <kst@cts.com> wrote in message news:lzu16gkdad .fsf@cts.com...[color=blue]
                > "Mike Wahler" <mkwahler@mkwah ler.net> writes:[color=green]
                > > "Dan Pop" <Dan.Pop@cern.c h> wrote in message
                > > news:bm65t9$ekm $5@sunnews.cern .ch...[/color]
                > [...][color=green][color=darkred]
                > > > Because this is what the standard actually says:
                > > >
                > > > If the processor time used is not available or its value cannot
                > > > ^^^^^^^^^^^^^^^ ^^^^
                > > > be represented, the function returns the value (clock_t)(-1)
                > > > ^^^^^^^^^^^^^^[/color]
                > >
                > > Should I understand this to mean that the 'wrapping'
                > > to zero described by the documentation posted by OP
                > > is not an allowed 'implementation-defined' component
                > > of 'clock()' behavior? "Zero" might not be of any
                > > practical use, but perhaps it *could* be called
                > > "best approximation" rather than "not representable".
                > >
                > > Are saying that a 'clock()' which "wraps" to zero is
                > > definitely not standard conforming?[/color]
                >
                > On one system I tested yesterday (Solaris 8), clock_t is a 32-bit
                > signed integer type. The values returned by the clock() function wrap
                > around to negative values. That's far more useful than returning -1
                > after 2147 seconds, but it looks like it violates the standard.[/color]

                As long as it doesn't wrap to (clock_t)(-1), then it is fine. You'll
                likely want to cast it to unsigned of the same size. If you detect
                a clock() result of (clock_t)(-1), then all bets are off.

                For a 32-bit clock_t type, you are stuck with 32-bit resolution
                unless there is also a non-standard library call than can return
                the number of wrap-arounds since the last call to clock(). (Thus,
                effectively increasing the size to 64-bits.)
                [color=blue]
                > Probably be best solution in this case would be to make clock_t a
                > 64-bit type (as it already is on some platforms).[/color]

                That is probably a better solution that would conform
                to the standard.


                Comment

                • Daniel Vallstrom

                  #23
                  Re: clock() function

                  "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message news:<64Chb.491 5$av5.1685@news read3.news.pas. earthlink.net>. ..[color=blue]
                  > "Dan Pop" <Dan.Pop@cern.c h> wrote in message
                  > news:bm65t9$ekm $5@sunnews.cern .ch...[color=green]
                  > > In <171aec1a.03100 91139.4248a36e@ posting.google. com>[/color]
                  > Daniel.Vallstro m@safelogic.se (Daniel Vallstrom) writes:[color=green]
                  > >[color=darkred]
                  > > >Dan.Pop@cern.c h (Dan Pop) wrote in message[/color][/color]
                  > news:<bm3j15$mu 7$6@sunnews.cer n.ch>...[color=green][color=darkred]
                  > > >> The words: "the implementation' s best approximation" effectively mean[/color][/color]
                  > that[color=green][color=darkred]
                  > > >> an implementation can return *anything* from a clock() call, including[/color][/color]
                  > a[color=green][color=darkred]
                  > > >> negative value, without having its conformance affected.
                  > > >>
                  > > >> I would hope that an implementation as described by the OP (i.e. most
                  > > >> Unix implementations ) constantly returns -1 after 36 minutes of CPU[/color][/color]
                  > time,[color=green][color=darkred]
                  > > >
                  > > >No! Why would you hope that?[/color]
                  > >
                  > > Because this is what the standard actually says:
                  > >
                  > > If the processor time used is not available or its value cannot
                  > > ^^^^^^^^^^^^^^^ ^^^^
                  > > be represented, the function returns the value (clock_t)(-1)
                  > > ^^^^^^^^^^^^^^[/color]
                  >
                  > Should I understand this to mean that the 'wrapping'
                  > to zero described by the documentation posted by OP
                  > is not an allowed 'implementation-defined' component
                  > of 'clock()' behavior?[/color]

                  No. Read the whole clock() description. Dan even explicitly say
                  in the text you quote above that almost anything goes since it
                  can be argued that it's "the implementation' s best approximation".
                  He just *hopes* that implementations return -1 since a literal
                  reading perhaps suggests that. An other approach, and IMO better,
                  would be to note that wraparounds could be legal, that they are
                  more useful and common practice, and therefor use wraparounds.

                  Compare e.g. with the issue if implementations are allowed to
                  return memory back to the OS (which IMO they certainly are).
                  You have to make up your own mind.
                  [color=blue]
                  > "Zero" might not be of any
                  > practical use, but perhaps it *could* be called
                  > "best approximation" rather than "not representable".
                  >
                  > Are saying that a 'clock()' which "wraps" to zero is
                  > definitely not standard conforming?[/color]

                  The clock type is often signed and then it would wrap to e.g. -2^31.
                  Anyway, I bet that most or all of the compilers you consider
                  conforming do wraparounds.


                  Daniel Vallstrom

                  [color=blue]
                  >
                  > -Mike[/color]

                  Comment

                  • Dan Pop

                    #24
                    Re: clock() function

                    In <171aec1a.03101 01307.76e48ec8@ posting.google. com> Daniel.Vallstro m@safelogic.se (Daniel Vallstrom) writes:
                    [color=blue]
                    >Dan.Pop@cern.c h (Dan Pop) wrote in message news:<bm65t9$ek m$5@sunnews.cer n.ch>...[color=green]
                    >> In <171aec1a.03100 91139.4248a36e@ posting.google. com> Daniel.Vallstro m@safelogic.se (Daniel Vallstrom) writes:
                    >>[color=darkred]
                    >> >Dan.Pop@cern.c h (Dan Pop) wrote in message news:<bm3j15$mu 7$6@sunnews.cer n.ch>...
                    >> >> The words: "the implementation' s best approximation" effectively mean that
                    >> >> an implementation can return *anything* from a clock() call, including a
                    >> >> negative value, without having its conformance affected.
                    >> >>
                    >> >> I would hope that an implementation as described by the OP (i.e. most
                    >> >> Unix implementations ) constantly returns -1 after 36 minutes of CPU time,
                    >> >
                    >> >No! Why would you hope that?[/color]
                    >>
                    >> Because this is what the standard actually says:
                    >>
                    >> If the processor time used is not available or its value cannot
                    >> ^^^^^^^^^^^^^^^ ^^^^
                    >> be represented, the function returns the value (clock_t)(-1)
                    >> ^^^^^^^^^^^^^^[/color]
                    >
                    >Yeah, from our favorite legalistic point of view that would be the
                    >thing to hope for. I forgot;p[/color]

                    It's also from a practical point of view. Imagine that the second clock_t
                    value you obtain is greater than the first one: how can you tell whether
                    wraparound occured and how many times between the two clock_t calls?

                    What is better: no answer or a hopelessly wrong answer?

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

                    Comment

                    • Daniel Vallstrom

                      #25
                      Re: clock() function

                      Dan.Pop@cern.ch (Dan Pop) wrote in message news:<bme8sn$js h$5@sunnews.cer n.ch>...[color=blue]
                      > In <171aec1a.03101 01307.76e48ec8@ posting.google. com> Daniel.Vallstro m@safelogic.se (Daniel Vallstrom) writes:
                      >[color=green]
                      > >Dan.Pop@cern.c h (Dan Pop) wrote in message news:<bm65t9$ek m$5@sunnews.cer n.ch>...[color=darkred]
                      > >> In <171aec1a.03100 91139.4248a36e@ posting.google. com> Daniel.Vallstro m@safelogic.se (Daniel Vallstrom) writes:
                      > >>
                      > >> >Dan.Pop@cern.c h (Dan Pop) wrote in message news:<bm3j15$mu 7$6@sunnews.cer n.ch>...
                      > >> >> The words: "the implementation' s best approximation" effectively mean that
                      > >> >> an implementation can return *anything* from a clock() call, including a
                      > >> >> negative value, without having its conformance affected.
                      > >> >>
                      > >> >> I would hope that an implementation as described by the OP (i.e. most
                      > >> >> Unix implementations ) constantly returns -1 after 36 minutes of CPU time,
                      > >> >
                      > >> >No! Why would you hope that?
                      > >>
                      > >> Because this is what the standard actually says:
                      > >>
                      > >> If the processor time used is not available or its value cannot
                      > >> ^^^^^^^^^^^^^^^ ^^^^
                      > >> be represented, the function returns the value (clock_t)(-1)
                      > >> ^^^^^^^^^^^^^^[/color]
                      > >
                      > >Yeah, from our favorite legalistic point of view that would be the
                      > >thing to hope for. I forgot;p[/color]
                      >
                      > It's also from a practical point of view. Imagine that the second clock_t
                      > value you obtain is greater than the first one: how can you tell whether
                      > wraparound occured and how many times between the two clock_t calls?[/color]

                      By requiring that clock calls are made frequent enough. In the OP's
                      case he would have to call the clock time-out library function at
                      least every 36 or 72 minutes.
                      [color=blue]
                      > What is better: no answer or a hopelessly wrong answer?[/color]

                      No answer is much better than a possibly wrong answer if the risk of
                      answering wrong is high enough. But that's not the case here. In all
                      programs I have used wraparounds in, there is no real risk of missing
                      a (relevant) wraparound.

                      Though, perhaps the implementors of clock did wraparounds because it
                      was the easiest solution rather than something thought through. And
                      then the standard writers felt that they had to allow the practice?


                      Daniel Vallstrom

                      [color=blue]
                      > Dan[/color]

                      Comment

                      • Dan Pop

                        #26
                        Re: clock() function

                        In <171aec1a.03101 30940.5bcde3dd@ posting.google. com> Daniel.Vallstro m@safelogic.se (Daniel Vallstrom) writes:
                        [color=blue]
                        >Dan.Pop@cern.c h (Dan Pop) wrote in message news:<bme8sn$js h$5@sunnews.cer n.ch>...[color=green]
                        >> In <171aec1a.03101 01307.76e48ec8@ posting.google. com> Daniel.Vallstro m@safelogic.se (Daniel Vallstrom) writes:
                        >>[color=darkred]
                        >> >Dan.Pop@cern.c h (Dan Pop) wrote in message news:<bm65t9$ek m$5@sunnews.cer n.ch>...
                        >> >> In <171aec1a.03100 91139.4248a36e@ posting.google. com> Daniel.Vallstro m@safelogic.se (Daniel Vallstrom) writes:
                        >> >>
                        >> >> >Dan.Pop@cern.c h (Dan Pop) wrote in message news:<bm3j15$mu 7$6@sunnews.cer n.ch>...
                        >> >> >> The words: "the implementation' s best approximation" effectively mean that
                        >> >> >> an implementation can return *anything* from a clock() call, including a
                        >> >> >> negative value, without having its conformance affected.
                        >> >> >>
                        >> >> >> I would hope that an implementation as described by the OP (i.e. most
                        >> >> >> Unix implementations ) constantly returns -1 after 36 minutes of CPU time,
                        >> >> >
                        >> >> >No! Why would you hope that?
                        >> >>
                        >> >> Because this is what the standard actually says:
                        >> >>
                        >> >> If the processor time used is not available or its value cannot
                        >> >> ^^^^^^^^^^^^^^^ ^^^^
                        >> >> be represented, the function returns the value (clock_t)(-1)
                        >> >> ^^^^^^^^^^^^^^
                        >> >
                        >> >Yeah, from our favorite legalistic point of view that would be the
                        >> >thing to hope for. I forgot;p[/color]
                        >>
                        >> It's also from a practical point of view. Imagine that the second clock_t
                        >> value you obtain is greater than the first one: how can you tell whether
                        >> wraparound occured and how many times between the two clock_t calls?[/color]
                        >
                        >By requiring that clock calls are made frequent enough. In the OP's
                        >case he would have to call the clock time-out library function at
                        >least every 36 or 72 minutes.[/color]

                        How frequent is "frequent enough" in a *portable* program? Is the
                        standard preventing an implementation from wrapping around after 1 second?
                        After 1 millisecond?
                        [color=blue][color=green]
                        >> What is better: no answer or a hopelessly wrong answer?[/color]
                        >
                        >No answer is much better than a possibly wrong answer if the risk of
                        >answering wrong is high enough. But that's not the case here. In all
                        >programs I have used wraparounds in, there is no real risk of missing
                        >a (relevant) wraparound.[/color]

                        Is this a property of the programs (see my remark above) or of the
                        actual implementations you have used?
                        [color=blue]
                        >Though, perhaps the implementors of clock did wraparounds because it
                        >was the easiest solution rather than something thought through. And
                        >then the standard writers felt that they had to allow the practice?[/color]

                        The C standard certainly tried to accomodate existing practice, but it
                        also did its best to discourage wraparounds.

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

                        Comment

                        • Daniel Vallstrom

                          #27
                          Re: clock() function

                          Dan.Pop@cern.ch (Dan Pop) wrote in message news:<bmepg0$pb n$2@sunnews.cer n.ch>...[color=blue]
                          > In <171aec1a.03101 30940.5bcde3dd@ posting.google. com> Daniel.Vallstro m@safelogic.se (Daniel Vallstrom) writes:
                          >[color=green]
                          > >Dan.Pop@cern.c h (Dan Pop) wrote in message news:<bme8sn$js h$5@sunnews.cer n.ch>...[color=darkred]
                          > >> In <171aec1a.03101 01307.76e48ec8@ posting.google. com> Daniel.Vallstro m@safelogic.se (Daniel Vallstrom) writes:
                          > >>
                          > >> >Dan.Pop@cern.c h (Dan Pop) wrote in message news:<bm65t9$ek m$5@sunnews.cer n.ch>...
                          > >> >> In <171aec1a.03100 91139.4248a36e@ posting.google. com> Daniel.Vallstro m@safelogic.se (Daniel Vallstrom) writes:
                          > >> >>
                          > >> >> >Dan.Pop@cern.c h (Dan Pop) wrote in message news:<bm3j15$mu 7$6@sunnews.cer n.ch>...
                          > >> >> >> The words: "the implementation' s best approximation" effectively mean that
                          > >> >> >> an implementation can return *anything* from a clock() call, including a
                          > >> >> >> negative value, without having its conformance affected.
                          > >> >> >>
                          > >> >> >> I would hope that an implementation as described by the OP (i.e. most
                          > >> >> >> Unix implementations ) constantly returns -1 after 36 minutes of CPU time,
                          > >> >> >
                          > >> >> >No! Why would you hope that?
                          > >> >>
                          > >> >> Because this is what the standard actually says:
                          > >> >>
                          > >> >> If the processor time used is not available or its value cannot
                          > >> >> ^^^^^^^^^^^^^^^ ^^^^
                          > >> >> be represented, the function returns the value (clock_t)(-1)
                          > >> >> ^^^^^^^^^^^^^^
                          > >> >
                          > >> >Yeah, from our favorite legalistic point of view that would be the
                          > >> >thing to hope for. I forgot;p
                          > >>
                          > >> It's also from a practical point of view. Imagine that the second clock_t
                          > >> value you obtain is greater than the first one: how can you tell whether
                          > >> wraparound occured and how many times between the two clock_t calls?[/color]
                          > >
                          > >By requiring that clock calls are made frequent enough. In the OP's
                          > >case he would have to call the clock time-out library function at
                          > >least every 36 or 72 minutes.[/color]
                          >
                          > How frequent is "frequent enough" in a *portable* program? Is the
                          > standard preventing an implementation from wrapping around after 1 second?
                          > After 1 millisecond?[/color]

                          I have only claimed that it's "fairly portable" (which might
                          be a contradiction in terms). What do you propose if someone
                          needs large clock timings? I could try some non-C solution
                          which I don't know how to do and which probably would be
                          system specific and limited? Or I could fairly easily code
                          some C module to keep track of the wraparounds and which
                          will work on all systems I'm coding for.
                          [color=blue]
                          >[color=green][color=darkred]
                          > >> What is better: no answer or a hopelessly wrong answer?[/color]
                          > >
                          > >No answer is much better than a possibly wrong answer if the risk of
                          > >answering wrong is high enough. But that's not the case here. In all
                          > >programs I have used wraparounds in, there is no real risk of missing
                          > >a (relevant) wraparound.[/color]
                          >
                          > Is this a property of the programs (see my remark above) or of the
                          > actual implementations you have used?[/color]

                          Both. From the C compiler/system you need that it really
                          uses wraparounds and that the wraparounds don't occur too
                          frequently. Then you have to check the clock frequently
                          enough in the code that needs the timers. It's also
                          dependent on the input to the program.

                          This obviously isn't a perfect solution but for me at
                          least it seemed better than the alternative --- but
                          that might be because I'm too ignorant of what the
                          alternative would be.


                          Daniel Vallstrom


                          [color=blue]
                          >[color=green]
                          > >Though, perhaps the implementors of clock did wraparounds because it
                          > >was the easiest solution rather than something thought through. And
                          > >then the standard writers felt that they had to allow the practice?[/color]
                          >
                          > The C standard certainly tried to accomodate existing practice, but it
                          > also did its best to discourage wraparounds.
                          >
                          > Dan[/color]

                          Comment

                          • Dan Pop

                            #28
                            Re: clock() function

                            In <171aec1a.03101 31616.4e259f61@ posting.google. com> Daniel.Vallstro m@safelogic.se (Daniel Vallstrom) writes:
                            [color=blue]
                            >Dan.Pop@cern.c h (Dan Pop) wrote in message news:<bmepg0$pb n$2@sunnews.cer n.ch>...[color=green]
                            >>
                            >> How frequent is "frequent enough" in a *portable* program? Is the
                            >> standard preventing an implementation from wrapping around after 1 second?
                            >> After 1 millisecond?[/color]
                            >
                            >I have only claimed that it's "fairly portable" (which might
                            >be a contradiction in terms). What do you propose if someone
                            >needs large clock timings?[/color]

                            The best solution is to remove the need for large clock timings. It has
                            always worked for me.
                            [color=blue]
                            >I could try some non-C solution
                            >which I don't know how to do and which probably would be
                            >system specific and limited?[/color]

                            Your own solution is implementation-specific and limited, even if it only
                            uses standard library functions.

                            Yes, when the standard library provides no satisfactory solution, there
                            is nothing wrong with using an implementation-specific solution.
                            [color=blue]
                            >Or I could fairly easily code
                            >some C module to keep track of the wraparounds and which
                            >will work on all systems I'm coding for.[/color]

                            This being comp.lang.c, a solution is considered portable when it relies
                            only on that the C specification guarantees. Or the C specification
                            not only doesn't guarantee wraparounds, it even does its best to outlaw
                            them.

                            Furthermore, if you detect wraparound, how do you handle it in a
                            *portable* way, without knowing the range and signedness of clock_t?
                            [color=blue][color=green]
                            >> Is this a property of the programs (see my remark above) or of the
                            >> actual implementations you have used?[/color]
                            >
                            >Both. From the C compiler/system you need that it really
                            >uses wraparounds and that the wraparounds don't occur too
                            >frequently.[/color]

                            You need more than that. You also need the range of clock_t, which
                            neither the standard nor the implementation specifies (in a portable way).
                            [color=blue]
                            >Then you have to check the clock frequently
                            >enough in the code that needs the timers.[/color]

                            Frequent checks alter the measurement, because they consume CPU time,
                            too.
                            [color=blue]
                            >This obviously isn't a perfect solution but for me at
                            >least it seemed better than the alternative --- but
                            >that might be because I'm too ignorant of what the
                            >alternative would be.[/color]

                            The best alternative is to reduce the time interval being measured to
                            a reasonable value (usually something between 1 second and 10 minutes).
                            If this is really not possible, implementations usually provide timing
                            tools without the shortcomings of clock().

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

                            Comment

                            • Daniel Vallstrom

                              #29
                              Re: clock() function

                              Dan.Pop@cern.ch (Dan Pop) wrote in message news:<bmgnnl$h6 i$4@sunnews.cer n.ch>...[color=blue]
                              > In <171aec1a.03101 31616.4e259f61@ posting.google. com> Daniel.Vallstro m@safelogic.se (Daniel Vallstrom) writes:
                              >[color=green]
                              > >Dan.Pop@cern.c h (Dan Pop) wrote in message news:<bmepg0$pb n$2@sunnews.cer n.ch>...[color=darkred]
                              > >>
                              > >> How frequent is "frequent enough" in a *portable* program? Is the
                              > >> standard preventing an implementation from wrapping around after 1 second?
                              > >> After 1 millisecond?[/color]
                              > >
                              > >I have only claimed that it's "fairly portable" (which might
                              > >be a contradiction in terms). What do you propose if someone
                              > >needs large clock timings?[/color]
                              >
                              > The best solution is to remove the need for large clock timings. It has
                              > always worked for me.[/color]

                              Sometimes that might not be possible though. E.g. I use large clock
                              timings in a SAT solver. The solver might get hard problems to solve and a
                              large time limit. More importantly one might want to try different solver
                              strategies a certain amount of time dependent on the total time limit.

                              [color=blue][color=green]
                              > >I could try some non-C solution
                              > >which I don't know how to do and which probably would be
                              > >system specific and limited?[/color]
                              >
                              > Your own solution is implementation-specific and limited, even if it only
                              > uses standard library functions.[/color]

                              Agreed all along.

                              [color=blue]
                              > Yes, when the standard library provides no satisfactory solution, there
                              > is nothing wrong with using an implementation-specific solution.[/color]

                              But the question is what is best, the C or non-C solution. As you
                              have pointed out the C solution makes many shady assumptions about
                              various things not guaranteed by the standard. But it might still be
                              preferable over a non-C solution if you for every actual effort to
                              support long clock timings for some system in a non-C way note that
                              the C solution would have worked just fine again.

                              I'm not necessarily advocating the C solution, since it's unsafe. But
                              if you can live with the unsafeness, the C solution might be much
                              easier than a non-C one, especially once you have some code for
                              handling long clock times.

                              [color=blue][color=green]
                              > >Or I could fairly easily code
                              > >some C module to keep track of the wraparounds and which
                              > >will work on all systems I'm coding for.[/color]
                              >
                              > This being comp.lang.c, a solution is considered portable when it relies
                              > only on that the C specification guarantees. Or the C specification
                              > not only doesn't guarantee wraparounds, it even does its best to outlaw
                              > them.
                              >
                              > Furthermore, if you detect wraparound, how do you handle it in a
                              > *portable* way, without knowing the range and signedness of clock_t?[/color]

                              I'm not of course. IIRC I just tested for some typical cases and handled
                              them. Actually probably only the common 32 bits type, for both signed and
                              unsigned. For other clock_t types, the clock timing would only be accurate
                              until a wrap around occurs.

                              You see, it's getting worse for every question you ask;)

                              [color=blue][color=green][color=darkred]
                              > >> Is this a property of the programs (see my remark above) or of the
                              > >> actual implementations you have used?[/color]
                              > >
                              > >Both. From the C compiler/system you need that it really
                              > >uses wraparounds and that the wraparounds don't occur too
                              > >frequently.[/color]
                              >
                              > You need more than that. You also need the range of clock_t, which
                              > neither the standard nor the implementation specifies (in a portable way).[/color]

                              Yup.

                              [color=blue][color=green]
                              > >Then you have to check the clock frequently
                              > >enough in the code that needs the timers.[/color]
                              >
                              > Frequent checks alter the measurement, because they consume CPU time,
                              > too.[/color]

                              'Frequently enough' doesn't mean frequent. Typically just at least once
                              every 32 minutes. One would of course profile the program and make
                              sure that the clock checks take an insignificant amount of time.


                              Daniel Vallstrom

                              [color=blue]
                              >[color=green]
                              > >This obviously isn't a perfect solution but for me at
                              > >least it seemed better than the alternative --- but
                              > >that might be because I'm too ignorant of what the
                              > >alternative would be.[/color]
                              >
                              > The best alternative is to reduce the time interval being measured to
                              > a reasonable value (usually something between 1 second and 10 minutes).
                              > If this is really not possible, implementations usually provide timing
                              > tools without the shortcomings of clock().
                              >
                              > Dan[/color]

                              Comment

                              • goose

                                #30
                                Re: clock() function

                                Dan.Pop@cern.ch (Dan Pop) wrote in message news:<bm429u$ab h$2@sunnews.cer n.ch>...[color=blue]
                                > In <ff82ae1b.03100 90714.68dd1bf@p osting.google.c om> ruse@webmail.co .za (goose) writes:
                                >[color=green]
                                > >yes. if you want more than that at the resolution that clock()
                                > >offers, just write your own timekeeping function, using a combination
                                > >of clock() and time(). I've done this before, though i haven't the faintest
                                > >clue what I've done with the source, if you have a bash at it, you'll find
                                > >that its simple enough[/color]
                                >
                                > I'm afraid that this is not possible at all (in a portable manner) because
                                > there is no connection whatsoever between the times returned by these two
                                > functions.[/color]

                                no, there isn't. but it is possible, at the start of the program,
                                to *establish* a point of reference for the 2 tmies returned by
                                these functions. once you have a base reference, and you know
                                how many times clock() wraps around in a certain time interval,
                                (say, once every 36 mins), then you can always use time(), check
                                if the return minus the last return of time() is greater than
                                the wraparound time, and then use clock() to get the finer granularity.

                                its not clean, its not neat, but it is possible, no ?
                                [color=blue]
                                >
                                > Feel free to prove me wrong, by posting your code ;-)
                                >[/color]

                                if i still had it, i would :-)

                                of course, as it was written a long time ago, it might have
                                used a platform function anyway, or made assumptions about
                                the platform. if i had the time, i'd try to write it again.

                                goose,

                                Comment

                                Working...