【问题标题】:Timing a Process (Windows 7 DOS Prompt)为进程计时(Windows 7 DOS 提示符)
【发布时间】:2011-09-03 19:00:15
【问题描述】:

我正在运行一个 C++ 程序,想知道它需要多少时间。我可以使用头文件 time.h 和 Windows.h 中的内置函数来做到这一点,但我想在 UNIX 中使用类似“time”命令的东西。原因是我的程序将输出重定向到一个 750 MB 的文本文件,而我没有心情尝试打开它。如何在 Windows 7(DOS 提示符)中完成。

谢谢

瓦伦

【问题讨论】:

    标签: c++ time command dos


    【解决方案1】:

    我已经使用了很长一段时间(足够长,它最初是为 MS-DOS 编写的)。它总是将计时输出写入标准错误,因此即使/如果标准输出被定向到文件,它也会进入控制台。理论上,您可以将其直接写入与标准错误分开的控制台,但我从来不需要它,所以我从来没有这样做过。

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <process.h>
    #include <malloc.h>
    #include <string.h>
    #if defined(WIN32) || defined(_WIN32)
    #include <windows.h>
    #else
    #include <dos.h>
    #endif
    
    int main(void) {
    
        clock_t start, end;
        char *cmd;
    #ifdef WIN32
        char *cmd_line = strdup(GetCommandLine());
    #else
        char *cmd_line = 0x80;
    #endif
        int seconds, tenths;
        char seps[] = " \r\n\t/";
    
        _heapmin();
    
        strtok(cmd_line, seps);
    
        cmd_line = strtok(NULL, "");
    
        start = clock();
        system(cmd_line);
        end = clock();
    
        cmd = strtok(cmd_line, seps );
    
        seconds = (end-start) / CLOCKS_PER_SEC;
        tenths = (end-start) / ( CLOCKS_PER_SEC / 10 );
        tenths -= seconds * 10;
    
        fprintf(stderr, "\n`%s' took %d.%d seconds.\n", cmd, seconds, tenths);
        return 0;
    }
    

    请注意,将结果命名为time.exe不会产生正确的结果(time 是内置于 shell 中的命令,因此它永远不会运行)。我通常将其命名为“计时器”。

    【讨论】:

      【解决方案2】:

      您可以捕获使用echo %TIME% 之前和之后的时间,或者有一些免费的实用程序可以用于此目的。例如,从此链接查看秒表程序。不幸的是,它的精度只有秒,

      http://www.jfitz.com/dos/index.html

      也可以参考这个类似的问题。这里有一些很好的解决方案,包括使用set /A 解析echo %TIME% 的批处理文件,

      Print time in a batch file (milliseconds)

      【讨论】:

        猜你喜欢
        • 2011-01-09
        • 2012-09-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-25
        • 1970-01-01
        • 2014-08-30
        相关资源
        最近更新 更多