elapsed time 0 with std::cin

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

    elapsed time 0 with std::cin

    I'm trying to measure user input time with my Timer class object. It isn't
    as easy as I expected. When using std::cin between timer start and stop, I
    get zero elapsed time. For some unknown reason, the clock seems to stop
    ticking during execution of std::cin.

    Here's my code:

    #include <ctime>
    #include <iostream>
    #include <string>

    class Timer
    {
    clock_t start_, nticks_;

    public:
    Timer() : nticks_(0) { start(); }
    ~Timer() {}

    void start() { start_ = clock(); }
    void stop() { nticks_ = clock() - start_; }
    double elapsed() const { return double(nticks_) / CLOCKS_PER_SEC; }
    };

    int main()
    {
    std::string answer;
    Timer T;
    // for (int n=0; n<100000000; ++n);
    std::cout << "? ";
    std::cin >answer;
    T.stop();
    std::cout << "time elapsed: " << T.elapsed() << "\n";
    }


    If I use the loop at the commented line, instead of std::cin, the timer
    works as expected. I can't figure out what's wrong here.

    TIA


  • Jim Langston

    #2
    Re: elapsed time 0 with std::cin

    "pekka" <pekka@nospam.i nvalidwrote in message
    news:pan.2007.1 1.23.19.46.27.5 72718@nospam.in valid...
    I'm trying to measure user input time with my Timer class object. It isn't
    as easy as I expected. When using std::cin between timer start and stop, I
    get zero elapsed time. For some unknown reason, the clock seems to stop
    ticking during execution of std::cin.
    >
    Here's my code:
    >
    #include <ctime>
    #include <iostream>
    #include <string>
    >
    class Timer
    {
    clock_t start_, nticks_;
    >
    public:
    Timer() : nticks_(0) { start(); }
    ~Timer() {}
    >
    void start() { start_ = clock(); }
    void stop() { nticks_ = clock() - start_; }
    double elapsed() const { return double(nticks_) / CLOCKS_PER_SEC; }
    };
    >
    int main()
    {
    std::string answer;
    Timer T;
    // for (int n=0; n<100000000; ++n);
    std::cout << "? ";
    std::cin >answer;
    T.stop();
    std::cout << "time elapsed: " << T.elapsed() << "\n";
    }
    >
    >
    If I use the loop at the commented line, instead of std::cin, the timer
    works as expected. I can't figure out what's wrong here.
    I run this code and I get numbers output such as 3.0532

    I'm using VC++ 2003


    Comment

    • pekka

      #3
      Re: elapsed time 0 with std::cin

      On Fri, 23 Nov 2007 12:43:03 -0800, Jim Langston wrote:
      "pekka" <pekka@nospam.i nvalidwrote in message
      news:pan.2007.1 1.23.19.46.27.5 72718@nospam.in valid...
      >I'm trying to measure user input time with my Timer class object. It isn't
      >as easy as I expected. When using std::cin between timer start and stop, I
      >get zero elapsed time. For some unknown reason, the clock seems to stop
      >ticking during execution of std::cin.
      >>
      >Here's my code:
      >>
      >#include <ctime>
      >#include <iostream>
      >#include <string>
      >>
      >class Timer
      >{
      >clock_t start_, nticks_;
      >>
      >public:
      >Timer() : nticks_(0) { start(); }
      >~Timer() {}
      >>
      >void start() { start_ = clock(); }
      >void stop() { nticks_ = clock() - start_; }
      >double elapsed() const { return double(nticks_) / CLOCKS_PER_SEC; }
      >};
      >>
      >int main()
      >{
      >std::string answer;
      >Timer T;
      >// for (int n=0; n<100000000; ++n);
      >std::cout << "? ";
      >std::cin >answer;
      >T.stop();
      >std::cout << "time elapsed: " << T.elapsed() << "\n";
      >}
      >>
      >>
      >If I use the loop at the commented line, instead of std::cin, the timer
      >works as expected. I can't figure out what's wrong here.
      >
      I run this code and I get numbers output such as 3.0532
      >
      I'm using VC++ 2003
      Ok. Maybe this is some strange platform specific problem. I am using
      Kubuntu and g++ version 4.0.3.



      Comment

      • David Harmon

        #4
        Re: elapsed time 0 with std::cin

        On Fri, 23 Nov 2007 21:46:28 +0200 in comp.lang.c++, pekka
        <pekka@nospam.i nvalidwrote,
        > void start() { start_ = clock(); }
        > void stop() { nticks_ = clock() - start_; }
        Note that clock() is supposed to register CPU time used by your
        program, not elapsed time. YMMV.

        Comment

        • Christopher Pisz

          #5
          Re: elapsed time 0 with std::cin


          "pekka" <pekka@nospam.i nvalidwrote in message
          news:pan.2007.1 1.23.19.46.27.5 72718@nospam.in valid...
          I'm trying to measure user input time with my Timer class object. It isn't
          as easy as I expected. When using std::cin between timer start and stop, I
          get zero elapsed time. For some unknown reason, the clock seems to stop
          ticking during execution of std::cin.
          >
          Here's my code:
          >
          #include <ctime>
          #include <iostream>
          #include <string>
          >
          class Timer
          {
          clock_t start_, nticks_;
          >
          public:
          Timer() : nticks_(0) { start(); }
          ~Timer() {}
          >
          void start() { start_ = clock(); }
          void stop() { nticks_ = clock() - start_; }
          double elapsed() const { return double(nticks_) / CLOCKS_PER_SEC; }
          };
          >
          int main()
          {
          std::string answer;
          Timer T;
          // for (int n=0; n<100000000; ++n);
          std::cout << "? ";
          std::cin >answer;
          T.stop();
          std::cout << "time elapsed: " << T.elapsed() << "\n";
          }
          >
          >
          If I use the loop at the commented line, instead of std::cin, the timer
          works as expected. I can't figure out what's wrong here.
          >
          TIA
          >
          >
          If you are looking to do a timer, you are better off using a OS specific
          timer. The ctime family is really only good for precisions of 1 second.
          Things like input can happen more often than 1 second. I am fairly certain
          that *nix offers some form of high precision timer and I know that MS does.
          You'll have to google around a bit.



          Comment

          • pekka

            #6
            Re: elapsed time 0 with std::cin

            On Fri, 23 Nov 2007 13:22:52 -0800, David Harmon wrote:
            On Fri, 23 Nov 2007 21:46:28 +0200 in comp.lang.c++, pekka
            <pekka@nospam.i nvalidwrote,
            >> void start() { start_ = clock(); }
            >> void stop() { nticks_ = clock() - start_; }
            >
            Note that clock() is supposed to register CPU time used by your
            program, not elapsed time. YMMV.
            Yes, that explains a lot. Thanks.

            Comment

            • pekka

              #7
              Re: elapsed time 0 with std::cin

              On Fri, 23 Nov 2007 21:16:06 -0600, Christopher Pisz wrote:
              >
              If you are looking to do a timer, you are better off using a OS specific
              timer. The ctime family is really only good for precisions of 1 second.
              Things like input can happen more often than 1 second. I am fairly certain
              that *nix offers some form of high precision timer and I know that MS does.
              You'll have to google around a bit.
              Most C++ timers I've seen so far are based on the same idea as my code
              (e.g. boost::timer), but I'll keep on searching.


              Comment

              • Christopher Pisz

                #8
                Re: elapsed time 0 with std::cin


                "pekka" <pekka@nospam.i nvalidwrote in message
                news:pan.2007.1 1.24.11.53.24.4 12744@nospam.in valid...
                On Fri, 23 Nov 2007 21:16:06 -0600, Christopher Pisz wrote:
                >
                >>
                >If you are looking to do a timer, you are better off using a OS specific
                >timer. The ctime family is really only good for precisions of 1 second.
                >Things like input can happen more often than 1 second. I am fairly
                >certain
                >that *nix offers some form of high precision timer and I know that MS
                >does.
                >You'll have to google around a bit.
                >
                Most C++ timers I've seen so far are based on the same idea as my code
                (e.g. boost::timer), but I'll keep on searching.
                If on windows QueryPerformanc eTimer offer very high precision
                I don't know what the Linux equivalent is, but I am sure it exists


                Comment

                • James Kanze

                  #9
                  Re: elapsed time 0 with std::cin

                  On Nov 23, 8:46 pm, pekka <pe...@nospam.i nvalidwrote:
                  I'm trying to measure user input time with my Timer class
                  object. It isn't as easy as I expected. When using std::cin
                  between timer start and stop, I get zero elapsed time. For
                  some unknown reason, the clock seems to stop ticking during
                  execution of std::cin.
                  Here's my code:
                  #include <ctime>
                  #include <iostream>
                  #include <string>
                  class Timer
                  {
                  clock_t start_, nticks_;
                  public:
                  Timer() : nticks_(0) { start(); }
                  ~Timer() {}
                  void start() { start_ = clock(); }
                  void stop() { nticks_ = clock() - start_; }
                  double elapsed() const { return double(nticks_) / CLOCKS_PER_SEC; }
                  };
                  int main()
                  {
                  std::string answer;
                  Timer T;
                  // for (int n=0; n<100000000; ++n);
                  std::cout << "? ";
                  std::cin >answer;
                  T.stop();
                  std::cout << "time elapsed: " << T.elapsed() << "\n";
                  }
                  If I use the loop at the commented line, instead of std::cin,
                  the timer works as expected. I can't figure out what's wrong
                  here.
                  According to the language standard, clock() is supposed to give
                  the systems best estimate of the CPU time used by the
                  application between successive calls to the function. If you're
                  program is waiting for keyboard input, it's not using the CPU,
                  so the value returned by clock() shouldn't increase.

                  If you want wall clock time, you should use time().

                  --
                  James Kanze (GABI Software) email:james.kan ze@gmail.com
                  Conseils en informatique orientée objet/
                  Beratung in objektorientier ter Datenverarbeitu ng
                  9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                  Comment

                  • James Kanze

                    #10
                    Re: elapsed time 0 with std::cin

                    On Nov 23, 9:43 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
                    "pekka" <pe...@nospam.i nvalidwrote in message
                    [...]
                    Here's my code:
                    #include <ctime>
                    #include <iostream>
                    #include <string>
                    class Timer
                    {
                    clock_t start_, nticks_;
                    public:
                    Timer() : nticks_(0) { start(); }
                    ~Timer() {}
                    void start() { start_ = clock(); }
                    void stop() { nticks_ = clock() - start_; }
                    double elapsed() const { return double(nticks_) / CLOCKS_PER_SEC; }
                    };
                    int main()
                    {
                    std::string answer;
                    Timer T;
                    // for (int n=0; n<100000000; ++n);
                    std::cout << "? ";
                    std::cin >answer;
                    T.stop();
                    std::cout << "time elapsed: " << T.elapsed() << "\n";
                    }
                    If I use the loop at the commented line, instead of
                    std::cin, the timer works as expected. I can't figure out
                    what's wrong here.
                    I run this code and I get numbers output such as 3.0532
                    I'm using VC++ 2003
                    That's a known bug in VC++ (or maybe the Windows runtime
                    libraries---I'm not sure at what level it occurs). If you get
                    anything but 0 for a keyboard wait, you're implementation is
                    incorrect (or the underlying system simply doesn't keep track of
                    the information).

                    --
                    James Kanze (GABI Software) email:james.kan ze@gmail.com
                    Conseils en informatique orientée objet/
                    Beratung in objektorientier ter Datenverarbeitu ng
                    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                    Comment

                    • James Kanze

                      #11
                      Re: elapsed time 0 with std::cin

                      On Nov 24, 4:29 pm, "Christophe r Pisz" <some...@somewh ere.netwrote:
                      "pekka" <pe...@nospam.i nvalidwrote in message
                      news:pan.2007.1 1.24.11.53.24.4 12744@nospam.in valid...
                      On Fri, 23 Nov 2007 21:16:06 -0600, Christopher Pisz wrote:
                      If you are looking to do a timer, you are better off using
                      a OS specific timer. The ctime family is really only good
                      for precisions of 1 second. Things like input can happen
                      more often than 1 second. I am fairly certain that *nix
                      offers some form of high precision timer and I know that MS
                      does. You'll have to google around a bit.
                      Most C++ timers I've seen so far are based on the same idea
                      as my code (e.g. boost::timer), but I'll keep on searching.
                      If on windows QueryPerformanc eTimer offer very high precision
                      I don't know what the Linux equivalent is, but I am sure it
                      exists
                      Posix required clock() to have a granularity of one microsecond.
                      From a QoI point of view, I would expect clock() to give the
                      maximum precision available, up to that granularity. (IIRC,
                      Windows requires clock() to have a granularity of 1 millisecond.
                      On the other hand, at least with VC++, the function doesn't
                      work, so it doesn't matter.)

                      --
                      James Kanze (GABI Software) email:james.kan ze@gmail.com
                      Conseils en informatique orientée objet/
                      Beratung in objektorientier ter Datenverarbeitu ng
                      9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                      Comment

                      • Jim Langston

                        #12
                        Re: elapsed time 0 with std::cin

                        "James Kanze" <james.kanze@gm ail.comwrote in message
                        news:ef3426bb-2220-430c-ae95-6b5f109d0423@j2 0g2000hsi.googl egroups.com...
                        On Nov 23, 9:43 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
                        "pekka" <pe...@nospam.i nvalidwrote in message
                        [...]
                        Here's my code:
                        #include <ctime>
                        #include <iostream>
                        #include <string>
                        class Timer
                        {
                        clock_t start_, nticks_;
                        public:
                        Timer() : nticks_(0) { start(); }
                        ~Timer() {}
                        void start() { start_ = clock(); }
                        void stop() { nticks_ = clock() - start_; }
                        double elapsed() const { return double(nticks_) / CLOCKS_PER_SEC; }
                        };
                        int main()
                        {
                        std::string answer;
                        Timer T;
                        // for (int n=0; n<100000000; ++n);
                        std::cout << "? ";
                        std::cin >answer;
                        T.stop();
                        std::cout << "time elapsed: " << T.elapsed() << "\n";
                        }
                        If I use the loop at the commented line, instead of
                        std::cin, the timer works as expected. I can't figure out
                        what's wrong here.
                        I run this code and I get numbers output such as 3.0532
                        I'm using VC++ 2003
                        That's a known bug in VC++ (or maybe the Windows runtime
                        libraries---I'm not sure at what level it occurs). If you get
                        anything but 0 for a keyboard wait, you're implementation is
                        incorrect (or the underlying system simply doesn't keep track of
                        the information).

                        --------------

                        MSDN for clock function says this:
                        ANSI 4.12.2.1 The era for the clock function
                        The clock function's era begins (with a value of 0) when the C program
                        starts to execute. It returns times measured in 1/CLOCKS_PER_SEC (which
                        equals 1/1000 for Microsoft C).

                        I take it that there's supposed to be more to it than that then?


                        Comment

                        • James Kanze

                          #13
                          Re: elapsed time 0 with std::cin

                          On Nov 25, 10:23 am, "Jim Langston" <tazmas...@rock etmail.comwrote :
                          "James Kanze" <james.ka...@gm ail.comwrote in message
                          news:ef3426bb-2220-430c-ae95-6b5f109d0423@j2 0g2000hsi.googl egroups.com...
                          On Nov 23, 9:43 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
                          "pekka" <pe...@nospam.i nvalidwrote in message
                          [...]
                          Here's my code:
                          #include <ctime>
                          #include <iostream>
                          #include <string>
                          class Timer
                          {
                          clock_t start_, nticks_;
                          public:
                          Timer() : nticks_(0) { start(); }
                          ~Timer() {}
                          void start() { start_ = clock(); }
                          void stop() { nticks_ = clock() - start_; }
                          double elapsed() const { return double(nticks_) / CLOCKS_PER_SEC; }
                          };
                          int main()
                          {
                          std::string answer;
                          Timer T;
                          // for (int n=0; n<100000000; ++n);
                          std::cout << "? ";
                          std::cin >answer;
                          T.stop();
                          std::cout << "time elapsed: " << T.elapsed() << "\n";
                          }
                          If I use the loop at the commented line, instead of
                          std::cin, the timer works as expected. I can't figure out
                          what's wrong here.
                          I run this code and I get numbers output such as 3.0532
                          I'm using VC++ 2003
                          That's a known bug in VC++ (or maybe the Windows runtime
                          libraries---I'm not sure at what level it occurs). If you get
                          anything but 0 for a keyboard wait, you're implementation is
                          incorrect (or the underlying system simply doesn't keep track of
                          the information).
                          MSDN for clock function says this:
                          ANSI 4.12.2.1 The era for the clock function
                          The clock function's era begins (with a value of 0) when the C program
                          starts to execute. It returns times measured in 1/CLOCKS_PER_SEC (which
                          equals 1/1000 for Microsoft C).
                          I take it that there's supposed to be more to it than that then?
                          From ISO 9899 (included by reference in the C++ standard): "The
                          clock function returns the implementation' s best approximation
                          to the processor time used by the program since the beginning of
                          an implementation-defined era related only to the program
                          invocation." Processor time, not wall clock time.

                          If I want wall clock time, the standard function is time(), not
                          clock().

                          --
                          James Kanze (GABI Software) email:james.kan ze@gmail.com
                          Conseils en informatique orientée objet/
                          Beratung in objektorientier ter Datenverarbeitu ng
                          9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                          Comment

                          Working...