使用std :: cin的时间为0 [英] elapsed time 0 with std::cin
问题描述
我正在尝试使用Timer类对象测量用户输入时间。它并不像我预期的那么容易。当在计时器启动和停止之间使用std :: cin时,我
获得零经过的时间。由于某种未知的原因,时钟似乎在执行std :: cin时停止了
。
这是我的代码:
#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;
计时器T;
// for(int n = 0; n< 100000000; ++ n);
std :: cout< ;< " ;? " ;;
std :: cin> answer;
T.stop();
std :: cout<< 经过的时间: << T.elapsed()<< " \ n";
}
如果我在注释行使用循环,而不是std :: cin,那么计时器
按预期工作。我不知道这里有什么问题。
TIA
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
推荐答案
"佩卡" < pe *** @ nospam.invalidwrote in message
news:pa *************************** *@nospam.invalid ...
"pekka" <pe***@nospam.invalidwrote in message
news:pa****************************@nospam.invalid ...
我正在尝试使用Timer类对象测量用户输入时间。它并不像我预期的那么容易。当在计时器启动和停止之间使用std :: cin时,我
获得零经过的时间。由于某种未知的原因,时钟似乎在执行std :: cin时停止了
。
这是我的代码:
#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;
计时器T;
// for(int n = 0; n< 100000000; ++ n);
std :: cout< ;< " ;? " ;;
std :: cin> answer;
T.stop();
std :: cout<< 经过的时间: << T.elapsed()<< " \ n";
}
如果我在注释行使用循环,而不是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.
我运行此代码并获得数字输出,例如3.0532
我正在使用VC ++ 2003
I run this code and I get numbers output such as 3.0532
I''m using VC++ 2003
On Fri,2007年11月23日12:43:03 -0800,Jim Langston写道:
On Fri, 23 Nov 2007 12:43:03 -0800, Jim Langston wrote:
" pekka" < pe *** @ nospam.invalidwrote in message
news:pa *************************** *@nospam.invalid ...
"pekka" <pe***@nospam.invalidwrote in message
news:pa****************************@nospam.invalid ...
>我正在尝试使用Timer类对象测量用户输入时间。它并不像我预期的那么容易。当在计时器启动和停止之间使用std :: cin时,我将获得零经过的时间。由于某些未知原因,时钟似乎在执行std :: cin期间停止了。
这是我的代码:
#include< ctime>
#include< iostream>
#include< string>
课程计时器
{
clock_t start_,nticks_;
public:
Timer():nticks_(0){start(); }
~Timer(){}
void start(){start_ = clock(); }
void stop(){nticks_ = clock() - start_; } \\ / double double elapsed()const {return double(nticks_)/ CLOCKS_PER_SEC; }
};
int main()
{
std :: string answer;
定时器T;
// for(int n = 0; n <100000000; ++ n);
std :: cout<< " ;? " ;;
std :: cin>回答;
T.stop();
std :: cout<< 经过的时间: << T.elapsed()<< " \ n";
}
如果我在注释行使用循环而不是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.
我运行此代码并获得数字输出,例如3.0532
我正在使用VC ++ 2003
I run this code and I get numbers output such as 3.0532
I''m using VC++ 2003
好的。也许这是一些奇怪的平台特定问题。我正在使用
Kubuntu和g ++ 4.0.3版。
Ok. Maybe this is some strange platform specific problem. I am using
Kubuntu and g++ version 4.0.3.
周五,2007年11月23日21:46:28 + 0200 in comp.lang.c ++,pekka
< pe *** @ nospam.invalidwrote,
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_; }
> void start() { start_ = clock(); }
void stop() { nticks_ = clock() - start_; }
请注意,clock()应该记录你的
程序使用的CPU时间,而不是经过的时间。 YMMV。
Note that clock() is supposed to register CPU time used by your
program, not elapsed time. YMMV.
这篇关于使用std :: cin的时间为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!