elapsed time 0 with std::cin

P

pekka

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
 
J

Jim Langston

pekka said:
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
 
P

pekka

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

David Harmon

On Fri, 23 Nov 2007 21:46:28 +0200 in comp.lang.c++, pekka
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.
 
C

Christopher Pisz

pekka said:
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.
 
P

pekka

On Fri, 23 Nov 2007 21:46:28 +0200 in comp.lang.c++, pekka


Note that clock() is supposed to register CPU time used by your
program, not elapsed time. YMMV.

Yes, that explains a lot. Thanks.
 
P

pekka

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

Christopher Pisz

pekka said:
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 QueryPerformanceTimer offer very high precision
I don't know what the Linux equivalent is, but I am sure it exists
 
J

James Kanze

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().
 
J

James Kanze

"pekka" <[email protected]> wrote in message

[...]
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).
 
J

James Kanze

If on windows QueryPerformanceTimer 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.)
 
J

Jim Langston

"pekka" <[email protected]> wrote in message

[...]
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?
 
J

James Kanze

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

Ask a Question

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

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

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top