Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Real time in C, as in the UNIX time command

Posted on 2004-04-18
4
Medium Priority
?
300 Views
Last Modified: 2010-04-15
Hi !

I have to measure the execution time of several functions in a C program I wrote.

What I have now is this:

struct tms StartTime;
struct tms EndTime;
float StartTimeSeconds;
float EndTimeSeconds;

times(&StartTime);
...
times(&EndTime);
StartTimeSeconds = (float)StartTime.tms_utime/(float)CLOCKS_PER_SEC;
EndTimeSeconds = (float)EndTime.tms_utime/(float)CLOCKS_PER_SEC;
float t_total;
t_total = EndTimeSeconds-StartTimeSeconds;

I executed the program and the time of execution displayed was:

Execution time: 0.000066 seconds

I executed the UNIX time comand (as in "time program") and the result was:

Execution time: 0.000066 seconds
real    0m8.336s
user    0m0.660s
sys     0m0.240s

I could get to the user time by shifting the "." (sorry my english, hope you understand), but that's not what I want, I want the real time, even though I know that it takes in account the whole system and not just my program.

By changing the time field from tms_utime to tms_stime I can get the value of sys, but I can't get the real time
(by the way the "real" time was really about 8 secs).

How can this be achieved?

Thanks in advance

0
Comment
Question by:b_loco
  • 2
  • 2
4 Comments
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 10853217
Use long time(&long) twice, once at the start, one at the end of the program. Granularity: 1 second :( There might be more sophisticated calls, depending on the actual Unix version you have.
0
 
LVL 2

Author Comment

by:b_loco
ID: 10853248
I didn't quite understand your reply

you mean instead of :
times(&StarTime);

I should put:
long time(&long);

About the granularity, if i understood correctly, that wouldn't be a problem because the program is supposed to take more than 5 secs.
And I do not wish to make my program dependant to the OS so the more general the better. If required I would make it UNIX dependant, but I would like to avoid it.
0
 
LVL 46

Accepted Solution

by:
Sjef Bosman earned 500 total points
ID: 10853284
I used to do it like this:

time_t tbefore, tafter; /* time_t = long */

.
time(&tbefore)
.
/* here your program */
.
time(&tafter)
printf("time used: %ld", (long)(tafter-tbefore))
0
 
LVL 2

Author Comment

by:b_loco
ID: 10853315
That incredibly simple solution solved it

Thank you :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question