By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,654 Members | 1,816 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,654 IT Pros & Developers. It's quick & easy.

elapsed time 0 with std::cin

P: n/a
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
Nov 23 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a
"pekka" <pe***@nospam.invalidwrote in message
news:pa****************************@nospam.invalid ...
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
Nov 23 '07 #2

P: n/a
On Fri, 23 Nov 2007 12:43:03 -0800, Jim Langston wrote:
"pekka" <pe***@nospam.invalidwrote in message
news:pa****************************@nospam.invalid ...
>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.

Nov 23 '07 #3

P: n/a
On Fri, 23 Nov 2007 21:46:28 +0200 in comp.lang.c++, pekka
<pe***@nospam.invalidwrote,
> 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.

Nov 23 '07 #4

P: n/a

"pekka" <pe***@nospam.invalidwrote in message
news:pa****************************@nospam.invalid ...
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.

Nov 24 '07 #5

P: n/a
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
<pe***@nospam.invalidwrote,
>> 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.

Nov 24 '07 #6

P: n/a
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.
Nov 24 '07 #7

P: n/a

"pekka" <pe***@nospam.invalidwrote in message
news:pa****************************@nospam.invalid ...
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 QueryPerformanceTimer offer very high precision
I don't know what the Linux equivalent is, but I am sure it exists
Nov 24 '07 #8

P: n/a
On Nov 23, 8:46 pm, pekka <pe...@nospam.invalidwrote:
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:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 24 '07 #9

P: n/a
On Nov 23, 9:43 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"pekka" <pe...@nospam.invalidwrote 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:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 24 '07 #10

P: n/a
On Nov 24, 4:29 pm, "Christopher Pisz" <some...@somewhere.netwrote:
"pekka" <pe...@nospam.invalidwrote in message
news:pa****************************@nospam.invalid ...
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 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.)

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

Nov 24 '07 #11

P: n/a
"James Kanze" <ja*********@gmail.comwrote in message
news:ef**********************************@j20g2000 hsi.googlegroups.com...
On Nov 23, 9:43 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"pekka" <pe...@nospam.invalidwrote 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?
Nov 25 '07 #12

P: n/a
On Nov 25, 10:23 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"James Kanze" <james.ka...@gmail.comwrote in message
news:ef**********************************@j20g2000 hsi.googlegroups.com...
On Nov 23, 9:43 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"pekka" <pe...@nospam.invalidwrote 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:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 25 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.