Results 1 to 8 of 8
This program provides the processor execution time of the program at the end of the program.
#include <time.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
clock_t ticks;
int i=0;
...
- 12-27-2008 #1Just Joined!
- Join Date
- Dec 2008
- Posts
- 5
Time based output-output for every 10 millisec
This program provides the processor execution time of the program at the end of the program.
#include <time.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
clock_t ticks;
int i=0;
while(i<50000)
{
printf("Work work %d\n", i);
i++;
ticks = clock();
}
printf("Used %0.2f seconds of CPU time. \n", (double)ticks/CLOCKS_PER_SEC);
}
I need the output(processor execution time) for every 10 milliseconds instead of getting the output at the end of the program.How to do it?.
It helps me to draw the graph with time on x axis and processor utilisation on y axis.
expecting ur replies folks
regards
Sai Viki
- 12-27-2008 #2
if your trying to get a loop to start and stop every 10 milliseconds
try info nanosleep()
...Gerard4143
- 12-27-2008 #3
gerard4143, I suspect that he's in a compute-bound loop and wants to keep track of the compute time used during that loop. If he introduces calls to nanosleep(), all that does is to guarantee that each loop will use a certain amount of clock time. Instead, he wants to output compute (not clock) time used every time that compute time goes up by 10 milliseconds.
vivasaayi, what you need to do is two things:
- move your final printf() statement to inside the loop; and
- every time you execute that printf() statement, store the value of ticks in another variable. Let's call that value printed_ticks. Then, when it comes time to print this again, don't do the actual print unless the new value exceeds the old one by 10 milliseconds. If it doesn't, then don't print it this time around, and don't update printed_ticks either.
--
Bill
Old age and treachery will overcome youth and skill.
- 12-28-2008 #4Just Joined!
- Join Date
- Dec 2008
- Posts
- 5
wje_lf
wje_lf and gerard,
Hearty thanks for ur reply
wje_lf,Ur response is close to my requirement.Let me put the question in more clear way.
1.Suppose this program(loop) takes 15 milliseconds as processor time - the time it has used processor.
2.suppose the total time taken by program to complete is 1000 milliseconds.Because the higher priority processes use the processor along with this program.
so total time to finish the process = 1000 milliseconds
total time it has actually spent on processor = 15 milliseconds
The output for the current program shows the processor execution time for total runtime.
But I need to print the time spent on the processor for every 100 milliseconds in that 1000 milliseconds of overall runtime.
expecting ur replies folks
Regards
sai viki
- 12-28-2008 #5
Now I'm completely confused as to what you need.
--
Bill
Old age and treachery will overcome youth and skill.
- 12-28-2008 #6Just Joined!
- Join Date
- Dec 2008
- Posts
- 5
oohhh...no!
I will put it in simple terms.
the program takes 1000 milliseconds to finish overall.
the output should be like
the cpu time used by the program for first 100 ms = 15 ms
the cpu time used by the program for second 100ms = 20 ms
the cpu time used by the program for the third 100 ms = 10 ms
.
.
.
the cpu time used by the program for the tenth 100ms = 2 ms
so during the overall execution period of 1000 ms - the cpu time used by the program is (15+20+10+......+2).
Is it clear now?
actually now my program outputs the cpu time used during the whole execution period of the program...i need it like the way i have listed above
- 12-28-2008 #7
Each time you go through the loop, call gettimeofday() to help you determine whether you've printed the amount of compute time within the most recent 100 milliseconds. If you have, do nothing more. But if you haven't, then store the new time of day as the time at which you printed the amount of compute time, use clock() to get the accumulated compute time, and subtract the previously-observed amount of compute time from that figure to get the amount of compute time to report for this interval.
--
Bill
Old age and treachery will overcome youth and skill.
- 12-29-2008 #8Just Joined!
- Join Date
- Dec 2008
- Posts
- 5
wje_lf
thanks u so much

Regards
sai viki


Reply With Quote