Solved

Real time in C, as in the UNIX time command

Posted on 2004-04-18
4
268 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
Comment Utility
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
Comment Utility
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 125 total points
Comment Utility
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
Comment Utility
That incredibly simple solution solved it

Thank you :)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now