Solved

Real time in C, as in the UNIX time command

Posted on 2004-04-18
4
271 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 125 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

Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
stack 22 172
Using unmanaged DLL from managed application 14 295
How to organize data in excel ? 2 115
Problem to save 10 174
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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

829 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