Solved

Real time in C, as in the UNIX time command

Posted on 2004-04-18
4
285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: 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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

617 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