Progressbar in der Konsole
-
Erstmal allen eine guten Morgen!
Ich hab mich mal drangesetzt eine Progressbar zu schreiben.
Ich hab nur das Problem, dass diese sehr aufwendig ist und die Laufzeit des Programms erheblich verlängert wird.Hier erstmal mein Codefragment:
void makeBaum(Knoten **wurzel) { char buffer[60]; //buffer für progressbar buffer[0]='['; for(int i=1; i<51; i++) buffer[i]=' '; buffer[51]=']'; buffer[52]=' '; buffer[53]=' '; buffer[54]=' '; buffer[55]=' '; buffer[56]='0'; buffer[57]=' '; buffer[58]='%'; buffer[59]='\0'; long int x=GRAD/50; //Grad=1000000 long int a=1; //anzahl der eingefuegten elemente int y=1; //index für buffer int k=0; //hilfsvariable für % int z=0; //hilfsvariable für % time_t t; time(&t); srand(static_cast<unsigned int>(t)); for(long int i=0; i<GRAD; i++) { einfuegen(wurzel,rand()%UINT_MAX+1); #ifdef PROGRESSBAR for(int j=sizeof(buffer)-1; j>=0; j--) cout << "\b"; cout << buffer; a++; if(a==x/2) k++; else if(a==x) { buffer[y]='#'; y++; k++; a=0; if(k==10) { z++; k=0; buffer[55]=z+'0'; buffer[56]=k+'0'; if(z==10) { buffer[54]='1'; buffer[55]='0'; buffer[56]='0'; } } else buffer[56]=k+'0'; } else; #endif } }Vielleicht habt Ihr ein paar Verbesserungsvorschläge für mich, was die Laufzeit erheblich verringert?!
MfG
Sfuccma
-
Sowas stumpfes?
#include <iostream> #include <iomanip> class ProgressBar { public: ProgressBar(unsigned int fSteps, unsigned int fWidth) : steps(fSteps), current(0), width(fWidth) { } void step(int fValue = 1) { current += fValue; if (current > steps) current = steps; if (current < 0) current = 0; } friend std::ostream& operator<<(std::ostream&, const ProgressBar&); private: unsigned int steps; unsigned int current; unsigned int width; }; std::ostream &operator<<(std::ostream &str, const ProgressBar &bar) { int aPos = (bar.width / bar.steps) * bar.current; str << "["; for(int i = 0; i < bar.width; i++) str << (i <= aPos ? '#' : ' '); str << "] "; str << std::setw(3) << static_cast<int>((bar.current / static_cast<double>(bar.steps))*100) << "%"; return str; } int main(int argc, char* argv) { ProgressBar bar(100,100); for(int i = 0; i < 100; i++) { bar.step(); std::cout << bar << std::endl; } }
-
Hier was zum abkupfern:
http://www.boost.org/doc/libs/1_43_0/libs/timer/timer.htm#Class progress_displayMan sollte aber bedenken, dass Ausgaben auf der Konsole im Vergleich zu anderen Operationen sehr langsam sein können.
Bei zeitkritischen Berechnungen würde ich es also nicht übertreiben mit Ausgaben, die man vielliecht sowieso nicht lesen kann.