#54 displaying simulation progress and remaining time

Next_Release
wont-fix
itpp (51)
1
2013-05-23
2007-08-15
No

/*
This class is used for displaying simulation progress and remaining time

Usage example:

include "Progress.cpp"

define MAX 25

int main(void)
{
Progress pg;
pg.show();
for(int n=0;n<MAX;n++)
{
sleep(1);
pg.show(((double)(n+1))/MAX);
}
}

*/

include <iostream>

include <iomanip>

class Progress //shows simulation progress and remaining time
{
public:
void show(double percent=0.0);//show progress (percent should vary from 0 to 1)
private:
time_t start,stop;
inline void sec2human(char* rem_time, double sec);//converts seconds to human readable format as string
};

void Progress::show(double percent)
{
if(percent==0)
{
time(&start);//initialize timer
std::cout << "0.0%\n";
return;
}
char rem_time[50];//remaining time as string
time(&stop);
sec2human(rem_time, (1/percent-1)difftime(stop, start));
std::cout << std::fixed << std::setw(3) << std::setprecision(1) << 100.0
percent << "%\t" << rem_time << " remaining\n";
}

void Progress::sec2human(char rem_time, double sec)
{
int s = (int)sec;//convert to an integer value
int d = s/86400;
s -= d
86400;
int h = s/3600;
s -= h3600;
int m = s/60;
s -= m
60;
if(d>0)
sprintf(rem_time, "%d day, %d hr, %d min, %d sec", d, h, m, s);
else if(h>0)
sprintf(rem_time, "%d hr, %d min, %d sec", h, m, s);
else if(m>0)
sprintf(rem_time, "%d min, %d sec", m, s);
else
sprintf(rem_time, "%d sec", s);
}

Discussion

  • Adam Piątyszek

    Adam Piątyszek - 2007-08-24

    Logged In: YES
    user_id=1004597
    Originator: NO

    Hi Bogdan,

    In my opinion this class is too specific and very simple to implement to be included in the IT++ library. Besides, it mixes a lot of C code which should be refactored with proper C++ replacement (sprintf, char rem_time[50]). Finally, if you really wish to include this code into IT++ please consider including it within the timing.{cpp,h} files, add Doxygen documentation, and test if it is portable on as many platforms supported by IT++ as possible (especially Windows' Cygwin, MinGW/MSYS and Visual C++, Mac OS X, and Solaris SunOS).

    BR,
    /ediap

     
  • Bogdan Cristea

    Bogdan Cristea - 2007-08-24

    Logged In: YES
    user_id=1261403
    Originator: YES

    Hi Adam
    I agree that this class is very simple, but this is exactly the purpose: to monitor simulation progress with minimum load. This is why I have chosen to use mixed c and c++ code, since c code is faster. If there is some interest in this class I could try to implement it in it++.
    The idea of this class is taken from Matlab, where there is a progressbar function (submitted by Steve Hoelzer) which does the same think in a graphical manner. I found this very useful when doing long simulations.
    regards
    Bogdan

     
  • Bogdan Cristea

    Bogdan Cristea - 2007-08-31

    patch implementing Progress_Timer class

     
  • Bogdan Cristea

    Bogdan Cristea - 2007-08-31

    Logged In: YES
    user_id=1261403
    Originator: YES

    I have implemented a Progress_Timer class on top of Real_Timer class. Tests were made on Athlon64 with openSuSE 10.2 using acml 3.6.1.
    File Added: progress_timer.patch

     
  • Bogdan Cristea

    Bogdan Cristea - 2013-05-23
    • status: open --> wont-fix
    • assigned_to: Bogdan Cristea
     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks