进程计时(Windows 7 DOS提示)
Timing a Process (Windows 7 DOS Prompt)
我正在运行一个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中的命令,因此它永远不会运行)。我通常将其命名为"计时器"。
相关文章:
- 有没有办法提示用户使用哪种数据类型作为模板 c++
- 如何使用递归循环我的代码(当用户输入无效输入时,它会再次提示他们)?
- 提示存档文件中的动态库依赖项
- 如何确保用户在一行上提示输入一次时输入名字和姓氏?
- 在 Golang 中等C++结构中提供代码提示/标记
- 为什么 std::cin 不提示用户输入?
- 如何使用打印到 PDF 打印机在 MFC 和 CView 中以编程方式打印而不提示输入文件名?
- 如何在动态创建的CMFCToolbar的工具提示中添加描述?
- 寻找解决这个动态编程问题的提示
- 添加带有 QSyntaxHighlight 和 QTextCharFormat 的工具提示
- 编写一个程序,提示用户输入一个整数,然后输出数字的单个数字和数字的总和
- 编译以下重载<<需要什么enable_if或其他提示?
- 防止Xcode 9.4无限期索引的提示
- 地图插入(const_iterator提示,值)用法
- 如何重现 Boost 进程文档提示的死锁?
- 为什么我的代码在 c++ 中无限循环.我的代码需要反复提示用户
- CScrollView 中的跟踪工具提示?
- C++:跳过第一个用户输入提示
- 提示编译器浮点向量计数可以被 8 整除?
- 进程计时(Windows 7 DOS提示)