【问题标题】:Running a timer while other commands are being executed c++在执行其他命令时运行计时器 C++
【发布时间】:2016-04-27 00:05:12
【问题描述】:

所以我正在尝试为一个更大的项目创建概念证明。我目前正在做一个定时测验,只有 1 个问题,你有 10 秒的时间来回答。

我真正在问什么

  • 我知道我可以通过这样做来读取用户输入

    “cin

  • 我可以通过做一个计时器

    clock_t 计时器;

    定时器 = 时钟();

    //代码

    定时器 = 时钟() - t;

  • 但是你是如何把所有这些放在一起的呢?您可以在请求输入时运行计时器吗?它似乎不会,因为 c++ 会逐行执行每个部分并等到它完成后再继续。但必须有办法!这是我想出的...

    bool Question(int Correct) {
        int Answer = 0;
        cin >> Answer;
        if (Answer == Correct) {
            return true;
        }
        else {
            return false;
        }
    }
    
    int main() {
    
    cout << "1 + 1 is: ";
    
    clock_t Timer;
    Timer = clock();
    
    bool Is_Correct = Question(2);
    
    Timer = clock() - Timer;
    
    cout << "You Answered: ";
    
    if (Is_Correct) {
        cout << "Correct!";
    }
    else {
        cout << "Wrong!";
    }
    
    cout << "\nAnd by the way, you answered the question with " << 10 - (Timer / CLOCKS_PER_SEC) << " Seconds to Spare.\n";
    
    cin.get();
    cin.get();
    
    return 0;
    }
    

对不起,间距有点乱了。

【问题讨论】:

  • 首先,这很像 C!尝试将&lt;chrono&gt; 标头用于类似时间的实用程序。
  • @alf 抱歉,我忘了说它只是一个窗口。有没有办法告诉 exe 如果在 10 秒内没有回答命令 (cin),它应该“继续”?
  • 如果您正在尝试制作更好的轮子,为什么不正确地做,并使用可视化的代码分析工具。在 HPC 社区 Tuning and Analysis Utilities (TAU) cs.uoregon.edu/research/tau/home.php 很受欢迎。看起来像完整的仪器和分析是多余的,1)它会给你你想要的时间信息以及更详细的时间,2)知道如何使用这种工具很好,3)你可能找不到确切的您正在寻找的内容,但您可能会发现一些与流程中的性能瓶颈有关的有趣内容

标签: c++ timer


【解决方案1】:

在程序等待输入时时钟继续运行,因此对输入操作进行计时(即查看花费了多长时间)没有问题。但是如果你想在 10 秒过去后中止输入操作,那么你就必须使用特定于平台的方法,例如键盘轮询。所有标准 C++ 输入操作都是阻塞的。因此,仅使用标准 C++,您可以尝试将输入操作放在它自己的执行线程中,推理异步,这就是线程的用途。但随后您会发现,在等待输入时无法终止该线程,并且(发挥创意)无法向其发布虚假输入。

不过,关于

你能在它要求输入时运行一个计时器吗?

如果你的意思是显示时间,为什么,那没问题。

你可以把它放在它自己的线程中。

但是,如果您希望每行输入多个字符,那么您可能必须使用系统特定的方法,因为使用标准库的文本显示修改工具(它基本上由 ASCII \b\r 控件)任何我能想到的方法都会在每次更改显示时间时弄乱文本光标位置,如下所示:

#include <atomic>
#include <chrono>
#include <exception>    // std::terminate
#include <iostream>
#include <iomanip>      // std::setw
#include <thread>
using namespace std;

auto fail( string const& s )
    -> bool
{ cerr << "!" << s << "\n"; terminate(); }

auto int_from( istream& stream )
    -> int
{
    int x;
    stream >> x || fail( "Input operation failed" );
    return x;
}

namespace g {
    atomic<bool>    input_completed;
}  // namespace g

void count_presentation( string const& prompt )
{
    for( int n = 1; not g::input_completed; ++n )
    {
        string const count_display = to_string( n );
        string const s = count_display + prompt.substr( count_display.length() );
        cout << "\r" << s << flush;

        using namespace std::chrono_literals;
        this_thread::sleep_for( 100ms );
    }
}

auto main()
    -> int
{
    string const prompt = string( 20, ' ' ) + "What's 6*7? ";
    auto counter = thread( count_presentation, ref( prompt ) );
    const int x = int_from( cin );
    g::input_completed = true;
    counter.join();
    cout << x << "\n";
}

【讨论】:

  • 在 Windows 中可以吗? C++11
  • @Dosisod:如果您的意思是,Windows 中有哪些系统特定的方法?然后,Windows 有一个难以正确使用的控制台窗口 API。但是您也可以在 Windows 中使用 ncurses 库。更简单的是,如果您的编译器支持它,则有一个名为 conio.h 的非标准头文件支持键盘轮询。对于 GUI,Windows 提供了计时器消息和硬线程计时器。
  • 感谢您的帮助!我找到了一个可以等待键盘输入并仍然运行计时器的线程:stackoverflow.com/questions/14192230/…
【解决方案2】:

我发现了这个Clock In C++ Console?。这很酷,它回答了你的问题。作者使用gotoxy() 移动到控制台输出的开头并覆盖当前时间。不过我不推荐using namespace std;的套路所以我编辑了代码。祝你好运,将其与您的集成

#include <iostream>
#include <ctime>
#include <windows.h>

// to move back to the beginning and output again
void gotoxy (int x, int y)
{
    COORD coord; // coordinates
    coord.X = x; coord.Y = y; // X and Y coordinates
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); // moves to the coordinates
}

int main ()  
{
    std::time_t now;
    while (true)
    {
        gotoxy (0,0);
        now = std::time(0);
        std::cout << "The time is now: " << std::ctime(&now);
        Sleep (20);
   }
    std::cin.get();
    return 0;
}

【讨论】:

    【解决方案3】:

    这是使用&lt;chrono&gt; 中的实用程序的示例。为此,您必须有权访问 C++11。

    您可以轻松地使用此模型使其适应您的代码。

    #include <iostream>
    #include <chrono>
    
    using namespace std::chrono;
    
    int main() 
    {   
        int num;
    
        // start timing
        auto start = steady_clock::now();
    
        std::cin >> num;
    
        // stop timing
        auto stop = steady_clock::now();
    
        // stop - start will give you the time that passed
        std::cout << "It took " 
                  << duration_cast<milliseconds>(stop - start).count()
                  << " milliseconds!\n";
    
        std::cin.get();
    }   
    

    这将打印从开始声明到停止声明执行之间的所有时间。
    duration_cast中可以使用secondsmicroseconds等。

    【讨论】:

    • 是的,所以我可以知道您输入输入所需的时间。但是有没有办法让程序停止等待输入而只是说“时间到了!” ?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    相关资源
    最近更新 更多