changed progress bar implementation

This commit is contained in:
evghenii
2014-10-16 11:53:05 +02:00
parent 558d7ee1d3
commit 92377426bd
9 changed files with 61 additions and 474 deletions

View File

@@ -37,6 +37,7 @@
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <cassert>
#include <iomanip>
#include "../timing.h"
#include "sort_ispc.h"
@@ -45,26 +46,28 @@ using namespace ispc;
extern void sort_serial (int n, unsigned int code[], int order[]);
/* progress bar by Ross Hemsley;
* http://www.rosshemsley.co.uk/2011/02/creating-a-progress-bar-in-c-or-any-other-console-app/ */
static inline void progressbar (unsigned int x, unsigned int n, unsigned int w = 50)
static void progressBar(const int x, const int n, const int width = 50)
{
if (n < 100)
{
x *= 100/n;
n = 100;
}
assert(n > 1);
assert(x >= 0 && x < n);
assert(width > 10);
const float f = static_cast<float>(x)/(n-1);
const int w = static_cast<int>(f * width);
if ((x != n) && (x % (n/100) != 0)) return;
// print bar
std::string bstr("[");
for (int i = 0; i < width; i++)
bstr += i < w ? '=' : ' ';
bstr += "]";
using namespace std;
float ratio = x/(float)n;
int c = ratio * w;
// print percentage
char pstr0[32];
sprintf(pstr0, " %2d %c ", static_cast<int>(f*100.0),'%');
const std::string pstr(pstr0);
std::copy(pstr.begin(), pstr.end(), bstr.begin() + (width/2-2));
cout << setw(3) << (int)(ratio*100) << "% [";
for (int x=0; x<c; x++) cout << "=";
for (int x=c; x<w; x++) cout << " ";
cout << "]\r" << flush;
std::cout << bstr;
std::cout << (x == n-1 ? "\n" : "\r") << std::flush;
}
int main (int argc, char *argv[])
@@ -87,7 +90,7 @@ int main (int argc, char *argv[])
tISPC1 += get_elapsed_mcycles();
if (argc != 3)
progressbar (i, m);
progressBar (i, m);
}
printf("[sort ispc]:\t[%.3f] million cycles\n", tISPC1);
@@ -105,7 +108,7 @@ int main (int argc, char *argv[])
tISPC2 += get_elapsed_mcycles();
if (argc != 3)
progressbar (i, m);
progressBar (i, m);
}
printf("[sort ispc + tasks]:\t[%.3f] million cycles\n", tISPC2);
@@ -123,7 +126,7 @@ int main (int argc, char *argv[])
tSerial += get_elapsed_mcycles();
if (argc != 3)
progressbar (i, m);
progressBar (i, m);
}
printf("[sort serial]:\t\t[%.3f] million cycles\n", tSerial);