From 0a4fd9f5a719779ce105240b6d2dae2e1f3ec73e Mon Sep 17 00:00:00 2001 From: Guillaume Chazarain Date: Sat, 29 Aug 2009 15:16:49 +0200 Subject: [PATCH] Faster ui.human_size() --- iotop/ui.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/iotop/ui.py b/iotop/ui.py index ad22a05..f636be7 100644 --- a/iotop/ui.py +++ b/iotop/ui.py @@ -1,6 +1,7 @@ import curses import errno import locale +import math import optparse import os import pwd @@ -19,14 +20,10 @@ from iotop.version import VERSION UNITS = ['B', 'K', 'M', 'G', 'T', 'P', 'E'] def human_size(size): - for i in xrange(len(UNITS) - 1, 0, -1): - base = 1 << (10 * i) - if 2 * base < size: - break - else: - i = 0 - base = 1 - return '%.2f %s' % ((float(size) / base), UNITS[i]) + if not size: + return '0.00 B' + expo = int(math.log(size / 2, 2) / 10) + return '%.2f %s' % ((float(size) / (1 << (10 * expo))), UNITS[expo]) def format_size(options, bytes): if options.kilobytes: -- 2.11.4.GIT