Time ./a.out equivalent in Windows

  • Thread starter Karthigan Srinivasan
  • Start date
K

Karthigan Srinivasan

Is there an equivant method of

'time ./a.out'

in Windows to measure execution time for a program?

Best Regards,
Karthigan.
 
K

Keith Thompson

Karthigan Srinivasan said:
Is there an equivant method of

'time ./a.out'

in Windows to measure execution time for a program?

We don't know. Well, some people here might happen to know, but this
isn't the place to ask, since your question really isn't about the C
programming language.

Try one of the comp.os.ms-windows.* or microsoft.* newsgroups.
 
B

Bartc

Karthigan Srinivasan said:
Is there an equivant method of

'time ./a.out'

in Windows to measure execution time for a program?

I put together the C program below which vaguely works, but you might get
ideas on how to do it properly. (I've never used command parameters in C
before).

--
Bart

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

int main(int n,char **cmds)
{char cmdstring[260];
int t;
int i,m;
char **p;

if (n<=1)
{if (n==1) printf("Usage: %s filename\n",*cmds);
return EXIT_FAILURE;
};

++cmds; /* first param of interest, should be exe name */

/* must check all params combined fit into cmdstring (you can ignore this if
not posting your code!) */
m=0;
p=cmds;
for (i=2; i<=n; ++i) m+=(strlen(*p++)+1);
if (m>sizeof(cmdstring)) return EXIT_FAILURE;

/* All the trouble the system went to to separate the params, now we need to
join them all together again with spaces between */
/* Hint: if you can use Winmain() instead, the params are already joined up
*/

strcpy(cmdstring,*cmds);
for (i=3; i<=n; ++i)
{strcat(cmdstring," ");
strcat(cmdstring,*(++cmds));
};

printf("Executing %s ...\n",cmdstring);

t=clock();

system(cmdstring);

t=clock()-t;

printf("Execution time: %d msec",t);

return EXIT_SUCCESS;
}
 
S

santosh

Bartc wrote:

<snip>

I think you should divide the difference by CLOCKS_PER_SEC to get the
result in seconds. As such your program's result is always zero. This
is because you are strong a return value of type clock_t into an int.
Use a clock_t variable.

Also you could use malloc for concatenating the command arguments to
deal with large number of/long strings.
 
S

santosh

Bartc wrote:

<snip>

Since your program is paused by the operating system when the command
given to *system* is executing, both the return values from clock will
be the same, and thus the difference will always be zero.

You need to use functions beyond the C standard like POSIX *times*
function in sys/times.h to do this properly.
 
B

Bartc

santosh said:
Bartc wrote:

<snip>

Since your program is paused by the operating system when the command
given to *system* is executing, both the return values from clock will
be the same, and thus the difference will always be zero.

That's a good point. clock() should give the execution time of the program
doing the timing.

Nevertheless, it seemed to work! Or at least gave sensible results.

Maybe something to with Windows, and the OP wanted this on Windows.
 
S

santosh

Bartc said:
That's a good point. clock() should give the execution time of the
program doing the timing.

Nevertheless, it seemed to work! Or at least gave sensible results.

Maybe something to with Windows, and the OP wanted this on Windows.

Here is a more robust version of 'time'. Still not as good as the UNIX
version of course. This code is NOT standard C; it depends on
*gettimeofday* , which is specific to POSIX systems.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>

int timediff(struct timeval *diff, struct timeval *end, struct timeval
*begin);

int main(int argc, char **argv)
{
struct timeval start, end, diff;
int ctr;
size_t cmdlen = 0;
char *cmd;
char *usage = "Program COMMAND [COMMAND ARGS] [...]\n";

if (!system(NULL)) {
puts("No command interpreter!");
return EXIT_FAILURE;
}
if (argc > 1) {
for (ctr = 1; ctr < argc; ctr++) cmdlen += strlen(argv[ctr]);
cmdlen += 1;
if ((cmd = calloc(cmdlen, sizeof *cmd)) == NULL) {
puts("Memory allocation failed.");
return EXIT_FAILURE;
}
else for (ctr = 1; ctr < argc; ctr++) strcat(cmd, argv[ctr]);
}
else fputs(usage, stderr);

gettimeofday(&start, NULL);
if (system(cmd) == -1) {
puts("system() failed.");
free(cmd);
return EXIT_FAILURE;
}
gettimeofday(&end, NULL);
timediff(&diff, &end, &start);
printf("Start:\t%lds %ldmsec\nEnd:\t%lds %ldmsec\nDiff.
\t%lds %ldmsec\n",
start.tv_sec, start.tv_usec, end.tv_sec, end.tv_usec,
diff.tv_sec,
diff.tv_usec);
return 0;
}

int timediff(struct timeval *result, struct timeval *x, struct timeval
*y)
/* Code below is copied from glibc documentation */
{
/* Perform the carry for the later subtraction by updating Y. */
if (x->tv_usec < y->tv_usec) {
int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
y->tv_usec -= 1000000 * nsec;
y->tv_sec += nsec;
}
if (x->tv_usec - y->tv_usec > 1000000) {
int nsec = (x->tv_usec - y->tv_usec) / 1000000;
y->tv_usec += 1000000 * nsec;
y->tv_sec -= nsec;
}

/* Compute the time remaining to wait.
`tv_usec' is certainly positive. */
result->tv_sec = x->tv_sec - y->tv_sec;
result->tv_usec = x->tv_usec - y->tv_usec;

/* Return 1 if result is negative. */
return x->tv_sec < y->tv_sec;
}
 
S

santosh

santosh wrote:

<snip>

Well it is horribly broken in the sense that it will collapse all it's
arguments without preserving a space between them. A quick workaround
is to enclose all the arguments to the program inside a pair of double
quotes. Fixing it is left to the OP. :)
 
B

Bartc

santosh said:
Here is a more robust version of 'time'. Still not as good as the UNIX
version of course. This code is NOT standard C; it depends on
*gettimeofday* , which is specific to POSIX systems.

#include <stdio.h> ....
gettimeofday(&start, NULL);
....

Yes, that's pretty comprehensive, compared to my poor effort.

I see that you're measuring elapsed time, rather than execution time, which
I suppose is a good alternative (I sometimes just use my watch).
 
K

Kaz Kylheku

That's a good point. clock() should give the execution time of the program
doing the timing.

Nevertheless, it seemed to work! Or at least gave sensible results.

Maybe something to with Windows, and the OP wanted this on Windows.

Microsoft's clock function is not ISO C conforming. It is documented
as returning wall time:

``The elapsed wall-clock time since the start of the process (elapsed
time in seconds times CLOCKS_PER_SEC). If the amount of elapsed time
is unavailable, the function returns -1, cast as a clock_t.'' [MSDN].

I haven't worked in Windows-land for quite a few years now, but I seem
to recall that clock is based on GetTickCount.

ISO C requires it to return ``the best approximation to the processor
time''.

GetTickCount is not an approximation of processor time at all, let
alone the best one available on Windows. The Windows API provides
GetThreadTimes and GetProcessTimes functions. These should be used as
the basis of for implementing clock. Then you would see the clock
freeze while an external command executes.

The proper way to measure the processor time used by that command is
to launch it with CreateProcess. You wait for it to finish using
WaitForSingleObject on the process handle, collect the termination
status using GetExitCodeEx, collect the times using GetProcessTimes
and then close both the process and thread handle.

MSDN has shows a sample Win32 program which launches another program
taken from its command command line: exactly what is needed as the
skeleton for a time utility. You just have to insert the code to check
the termination status and times. (You probably don't want to report
the times for a failed process, or else report that the process failed
and pass the failed status up to the caller).

Look up the CreateProcess function and follow the link to the
examples.
 
S

santosh

Bartc said:
That's a good point. clock() should give the execution time of the
program doing the timing.

It does. Trouble is, the execution of our program is suspended when the
argument to system is running. As far as clock is concerned, no time
has elapsed between the call to system and it's return.

To stick to pure standard C we will have to use the time function, whose
resolution is usually isn't sufficient for use in a "time" command.

<snip>
 
S

santosh

Bartc said:
...

Yes, that's pretty comprehensive, compared to my poor effort.

I see that you're measuring elapsed time, rather than execution time,
which I suppose is a good alternative (I sometimes just use my watch).

Which is the wrong method in multitasking systems. I don't know what I
was thinking.
 
K

Keith Thompson

Bartc said:
I put together the C program below which vaguely works, but you might get
ideas on how to do it properly. (I've never used command parameters in C
before).
[snip]

The Unix/Linux "time" command cannot be implemented in purely standard
C. There are various versions of the "time" command, some of them
built into various shells. It typically reports the wall-clock time,
the user-mode CPU time, the system-mode CPU time, and the percentage
of time spent in CPU time; it may also report various I/O statistics.
 
K

Kenny McCormack

Bartc said:
I put together the C program below which vaguely works, but you might get
ideas on how to do it properly. (I've never used command parameters in C
before).
[snip]

The Unix/Linux "time" command cannot be implemented in purely standard C.

True, given that the C standard doesn't admit of any kind of
multi-processing, so the whole concept is moot.

You, of all people, ought to know that.
 
K

Kenny McCormack

Kaz Kylheku said:
GetTickCount ...

Off topic. Not portable. Cant discuss it here. Blah, blah, blah.
alone the best one available on Windows. The Windows API provides
GetThreadTimes ...

Off topic. Not portable. Cant discuss it here. Blah, blah, blah.
and GetProcessTimes ...

Off topic. Not portable. Cant discuss it here. Blah, blah, blah.
to launch it with CreateProcess...

Off topic. Not portable. Cant discuss it here. Blah, blah, blah.
WaitForSingleObject ...

Off topic. Not portable. Cant discuss it here. Blah, blah, blah.
status using GetExitCodeEx...

Off topic. Not portable. Cant discuss it here. Blah, blah, blah.
collect the times using GetProcessTimes ...

Off topic. Not portable. Cant discuss it here. Blah, blah, blah.
and then close both the process and thread handle(s).

Off topic. Not portable. Cant discuss it here. Blah, blah, blah.

Well done. Your post is completely OT.
 
J

jacob navia

Karthigan said:
Is there an equivant method of

'time ./a.out'

in Windows to measure execution time for a program?

Best Regards,
Karthigan.

#include <windows.h>
#include <stdio.h>
int main(int argc,char *argv[])
{
int ms;

if (argc < 2) {
printf(
"Usage: %s \"command\" (enclosed in double quotes)\n",
argv[0]);
return -1;
}
ms = GetTickCount();
system(argv[1]);
ms = GetTickCount() - ms;
printf("\n'%s'\nexecuted in %d ms\n",argv[1],ms);
return 0;
}
 
A

Antoninus Twink

True, given that the C standard doesn't admit of any kind of
multi-processing, so the whole concept is moot.

You, of all people, ought to know that.

But you of all people ought to know that the usual rules of topicality
don't apply to The Clique, who are free to discuss novels, electric
wiring, American politics and anything else they like.

That's the Principle of Hypocrisy that everyone around here holds dear.
 
B

Bartc

#include <windows.h> ....
"Usage: %s \"command\" (enclosed in double quotes)\n", argv[0]);

If you're going to go this far, you might as well do this and get rid of the
double quotes:

#include <stdio.h>
#include <windows.h>

int STDCALL WinMain(HINSTANCE x,HINSTANCE y,char* cmdline,int z)
{int ms;

if (*cmdline==0) return EXIT_FAILURE;

ms = GetTickCount();
system(cmdline);
ms = GetTickCount() - ms;
printf("\n'%s'\nexecuted in %d ms\n",cmdline,ms);
return 0;
}


(Clearly, non-portable outside of Windows. And quite a non-standard way of
defining main().)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top