Solved

display elapsed time during the program is running

Posted on 2004-04-02
17
950 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
 
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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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 opening and reading files in the C programming language.

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now