Since measurement is done in bytes, use a capital B as unit. Also, stepping by 1024 from one magnitude to the next is more common. This is indicated by the 'i' in between the magnitude and unit symbols. --- examples/async_speed.c | 32 +++++++++++--------------------- examples/speed.c | 32 +++++++++++--------------------- 2 files changed, 22 insertions(+), 42 deletions(-)
diff --git a/examples/async_speed.c b/examples/async_speed.c index 679219b..940d923 100644 --- a/examples/async_speed.c +++ b/examples/async_speed.c @@ -43,29 +43,19 @@ static void alarm_handler(int signo) pfd.events = POLLIN; } +static char *units[] = { "", "Ki", "Mi", "Gi", "Ti", 0}; + static void value2human(double bytes, double time, double* data, double* speed,char* metric) { - if (bytes > 1000 && bytes < 1000*1000) { - *data = ((double)bytes)/1000; - *speed = *data/time; - strcpy(metric, "Kb"); - return; - } else if (bytes >= 1000*1000 && bytes < 1000*1000*1000) { - *data = ((double)bytes)/(1000*1000); - *speed = *data/time; - strcpy(metric, "Mb"); - return; - } else if (bytes >= 1000*1000*1000) { - *data = ((double)bytes)/(1000*1000*1000); - *speed = *data/time; - strcpy(metric, "Gb"); - return; - } else { - *data = (double)bytes; - *speed = *data/time; - strcpy(metric, "bytes"); - return; - } + int unit = 0; + + *data = bytes; + while (*data > 1024 && units[unit + 1]) { + *data /= 1024; + unit++; + } + *speed = *data / time; + sprintf(metric, "%sB", units[unit]); } diff --git a/examples/speed.c b/examples/speed.c index 998772d..63218a9 100644 --- a/examples/speed.c +++ b/examples/speed.c @@ -39,29 +39,19 @@ static void alarm_handler(int signo) must_finish = 1; } +static char *units[] = { "", "Ki", "Mi", "Gi", "Ti", 0}; + static void value2human(double bytes, double time, double* data, double* speed,char* metric) { - if (bytes > 1000 && bytes < 1000*1000) { - *data = ((double)bytes)/1000; - *speed = *data/time; - strcpy(metric, "Kb"); - return; - } else if (bytes >= 1000*1000 && bytes < 1000*1000*1000) { - *data = ((double)bytes)/(1000*1000); - *speed = *data/time; - strcpy(metric, "Mb"); - return; - } else if (bytes >= 1000*1000*1000) { - *data = ((double)bytes)/(1000*1000*1000); - *speed = *data/time; - strcpy(metric, "Gb"); - return; - } else { - *data = (double)bytes; - *speed = *data/time; - strcpy(metric, "bytes"); - return; - } + int unit = 0; + + *data = bytes; + while (*data > 1024 && units[unit + 1]) { + *data /= 1024; + unit++; + } + *speed = *data / time; + sprintf(metric, "%sB", units[unit]); } -- 1.7.3.2 _______________________________________________ Cryptodev-linux-devel mailing list Cryptodev-linux-devel@gna.org https://mail.gna.org/listinfo/cryptodev-linux-devel