Solved

display elapsed time during the program is running

Posted on 2004-04-02
17
952 Views
Last Modified: 2010-04-15
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

0
Comment
Question by:ericworldz
  • 7
  • 7
  • 2
  • +1
17 Comments
 
LVL 3

Expert Comment

by:1ce
ID: 10743999
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
 

Author Comment

by:ericworldz
ID: 10744235
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
 
LVL 2

Expert Comment

by:timbauer
ID: 10745051
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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
LVL 17

Expert Comment

by:mokule
ID: 10745629
Should be

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

to achieve desired format.
And what about hours
0
 
LVL 17

Expert Comment

by:mokule
ID: 10745640
Correction.. Everybody doing the same :)

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

Expert Comment

by:timbauer
ID: 10749060
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
 
LVL 17

Expert Comment

by:mokule
ID: 10749163
I think that hours may be used, but ericworldz should decide what should be done.
0
 

Author Comment

by:ericworldz
ID: 10756728
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
 

Author Comment

by:ericworldz
ID: 10756737
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
 
LVL 17

Expert Comment

by:mokule
ID: 10756803
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
 

Author Comment

by:ericworldz
ID: 10757130
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
 
LVL 17

Expert Comment

by:mokule
ID: 10757265
time_t time(time_t *timer);

0
 

Author Comment

by:ericworldz
ID: 10757651
mokule,

time gives me the total seconds the program has been running?
0
 

Author Comment

by:ericworldz
ID: 10757667
how do i use it with the printtimer function?
0
 
LVL 17

Expert Comment

by:mokule
ID: 10758033
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
 

Author Comment

by:ericworldz
ID: 10758069
please clarify.
0
 
LVL 17

Accepted Solution

by:
mokule earned 50 total points
ID: 10758448
void printtimer()
{
....
      time_t stop;
     
     stop=time(NULL);
     seconds=stop-start;
 
0

Featured Post

Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

Question has a verified solution.

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

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…
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 and use structures in the C programming language.
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.

828 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