Solved

display elapsed time during the program is running

Posted on 2004-04-02
17
949 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
posix semaphore deadlock 13 108
Connecting Native C / C++ to SQL Server 11 267
Problem to save 10 122
C++ :Change value from  DisableCMD registry 4 14
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

760 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

19 Experts available now in Live!

Get 1:1 Help Now