Solved

display elapsed time during the program is running

Posted on 2004-04-02
17
951 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dll Dynamic way c/c++ 9 145
How to creat good software interface ? 1 67
Why is compiler in oracle server ? 9 65
If a cell in a range equals "YES" return specific Test. 4 30
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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures 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.

770 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