473,387 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
Nov 23 '07 #1
12 3275
"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
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
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

"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
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
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

"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
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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: William Payne | last post by:
Hello, when I was writing a user-driven test program for a data structure I wrote, I encountered an annoying problem. The test program is laid out as a menu with several numbered options. The user...
5
by: Chris Mantoulidis | last post by:
Let's say I have this: std::string s1; std::cin >> s1; This will read s1 from cin until it finds a space (or a newline, whichever comes first). Okay this works. But when I want to continue...
3
by: yw | last post by:
Hi, When I use std::cin and std::cout to input some data, how can I ingore the cin by using the Enter key? For example: string sname; cout<<"Please input your name:"<<endl; cin>>sname; If...
3
by: puzzlecracker | last post by:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); I have seen it in faq - what does it do, exactly?
3
by: moleskyca1 | last post by:
In C++ FAQ (15.5), I see this code: int i = 0; while (std::cin >x) { // RIGHT! (reliable) ++i; // Work with x ... } but I don't know how can while loop end? ostream &operator >>(...) return...
8
by: junw2000 | last post by:
Below is simple code: #include <iostream> int main(){ int i; while(1){ while(!(std::cin >i)){ std::cout<<"Invalid input i: "<<i<<'\n';
8
by: Johannes Meng | last post by:
Good day, I'm experimenting with unbuffered input at the moment. To get input I basically use cin.get(). My problem are control sequences preceeded by an ESC character (i.e. up, down, f-keys et...
3
by: Ralf Goertz | last post by:
Hi, consider the following program #include <iostream> #include <fstream> using namespace std; int main(int argc, char *argv){
3
by: Alex Snast | last post by:
hello guys I need to modify the std::cin delim char from the default ' ' and '\n' characters to ',' i know that i can edit the delim in the getline command however i'd like to know if there's...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.