I am running a C++ program and want to know how much time it takes. I can do it with in-built functions in the header files time.h and Windows.h but I want to use something like "time" command in UNIX. The reason is because my program is redirecting the output to a text f开发者_运维技巧ile which is 750 MB and I am in no mood of trying to open it. How can that be done in Windows 7 (DOS prompt).
Thanks
Varun
You can capturing the time before and after using echo %TIME%
, or there are some free utilities for this purpose. For example, check out the stopwatch program from this link. It only has accuracy down to the second, unfortunately,
http://www.jfitz.com/dos/index.html
Also refer to this similar question. There are some good solutions in here including a batch file that uses set /A
to parse echo %TIME%
,
Print time in a batch file (milliseconds)
I've been using this for quite a while (long enough that it was originally written for MS-DOS). It always writes the timing output to stderr, so it goes to the console even when/if standard output is directed to a file. In theory, you could write it directly to a console separate from standard error, but I've never needed it so I've never done it.
#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;
}
Note that naming the result time.exe
will not produce correct results (time
is a command built into the shell, so it'll never run). I normally name it "timer" instead.
精彩评论