进程计时(Windows 7 DOS提示)

Timing a Process (Windows 7 DOS Prompt)

本文关键字:DOS 提示 Windows 进程      更新时间:2023-10-16

我正在运行一个C++程序,想知道它需要多少时间。我可以使用头文件time.h和Windows.h中的内置函数来完成,但我想在UNIX中使用类似"time"命令的命令。原因是我的程序正在将输出重定向到一个750MB的文本文件,我没有心情打开它。这怎么能在Windows7(DOS提示)中完成呢。

感谢

Varun

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

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

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

批处理文件中的打印时间(毫秒)

我已经使用它很长时间了(足够长的时间,它最初是为MS-DOS编写的)。它总是将计时输出写入stderr,因此即使标准输出指向文件,它也会进入控制台。理论上,你可以把它直接写到一个独立于标准错误的控制台上,但我从来没有需要过它,所以我从来没有这样做过

#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[] = " rnt/";
    _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中的命令,因此它永远不会运行)。我通常将其命名为"计时器"。