• C

display elapsed time during the program is running

How can I display the time elapsed while I have the program running?

For example in the following format:
min:sec
01:01
10:04
24:59

Thanks

ericworldzAsked:
Who is Participating?
 
mokuleCommented:
void printtimer()
{
....
      time_t stop;
     
     stop=time(NULL);
     seconds=stop-start;
 
0
 
1ceCommented:
one way of doing it is using the system clock

#include <time.h>

clock_t start,stop;
int seconds;


.....
start=clock();
//somecode
//somecode
//somecode
stop=clock();

seconds=(stop-start)/CLOCKS_PER_SEC;

printf("%d:%d",seconds/60;seconds%60);




0
 
ericworldzAuthor Commented:
What does this mean?
start=clock();
//somecode
//somecode
//somecode
stop=clock();


I want the time to keep on displaying and counting up while the program is running.
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
timbauerCommented:
Here is a trick. Use the '\r' character.
The form feed moves the output back to the beginning of the line.

1. So at the start of the program make a global variable.
clock_t start;

2. Beginning of main initialize start with
start=clock();

3. Make a function "print_time"
void print_time(void){
  int seconds;
  clock_t now;

  now = clock();
  seconds=(now -start)/CLOCKS_PER_SEC;

  printf("\r"); //go to the beginning of the line
  printf("%d:%d",seconds/60;seconds%60);
}

4. Periodically sprinkle calls to print_time();
And it will overwrite the "time" line. With the current time.

Keep in mind you cannot output anything else.
As that will clobber your line.



0
 
mokuleCommented:
Should be

printf("%02d:%02d",seconds/60;seconds%60);

to achieve desired format.
And what about hours
0
 
mokuleCommented:
Correction.. Everybody doing the same :)

printf("%02d:%02d",seconds/60,seconds%60);
0
 
timbauerCommented:
The current solution will just try and pack minutes into a 2 digit field.
It will wrap at 99 minutes. Good idea to use the fixed width fields.

printf("\r");
printf("%010d:%02d",seconds/60,seconds%60);

This will work for > 4000 years or rather whenever clock() wraps around.
Then it will wrap around.
0
 
mokuleCommented:
I think that hours may be used, but ericworldz should decide what should be done.
0
 
ericworldzAuthor Commented:
Timbauer,
I think there's another problem with using clock(), that only gives me the CPU time the program has used and not the actual elapsed time that the program has been running.

0
 
ericworldzAuthor Commented:
This is what I have thus far:

void printtimer()
{
      int       m, s, seconds;
      char       buf[36];
      clock_t stop;
      
      sleep(1);
      stop=clock();
      seconds=(stop-start)/CLOCKS_PER_SEC;
      m = seconds/60;
      s = seconds%60;
      move(4,COLS-35);
      sprintf(buf, "TOTAL TIME: %d:%02d", m, s);
      printw(buf);
      refresh();
}
0
 
mokuleCommented:
I would do it rather like this

void printtimer()
{
     int      m, s, seconds;
     static int m_prev = -1;
     static int s_prev = -1;
     char      buf[36];
     clock_t stop;
     
/*     sleep(1);*/
     stop=clock();
     seconds=(stop-start)/CLOCKS_PER_SEC;
     m = seconds/60;
     s = seconds%60;
/* print if time changed */
     if((m != m_prev) || (s != s_prev))
        {
        move(4,COLS-35);
        sprintf(buf, "TOTAL TIME: %d:%02d", m, s);
        printw(buf);
        refresh();
        m_prev = m;
        s_prev = s;
        }
}
0
 
ericworldzAuthor Commented:
Problem using the clock() -- that only gives me the CPU time the program has used and not the actual elapsed time that the program has been running.

Is there another function that I can use to get the elapsed time that the program has been running?
0
 
mokuleCommented:
time_t time(time_t *timer);

0
 
ericworldzAuthor Commented:
mokule,

time gives me the total seconds the program has been running?
0
 
ericworldzAuthor Commented:
how do i use it with the printtimer function?
0
 
mokuleCommented:
time gives the current time, in seconds, elapsed since 00:00:00 GMT, January 1, 1970, and stores that value in the location pointed to by timer, provided that timer is not a NULL pointer.
Thus You should use it similar like clock except not to divide by a constant.
0
 
ericworldzAuthor Commented:
please clarify.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.