Skip to content

Commit 88f0988

Browse files
committed
Don't reinvent the timedelta formatting wheel
1 parent 3403a99 commit 88f0988

File tree

1 file changed

+1
-29
lines changed

1 file changed

+1
-29
lines changed

autotime.py

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import print_function
22

33
import time
4+
from IPython.core.magics.execution import _format_time as format_delta
45

56

67
class LineWatcher(object):
@@ -23,35 +24,6 @@ def stop(self):
2324
assert diff > 0
2425
print('time: %s' % format_delta(diff))
2526

26-
27-
def format_delta(num, suffix='s', threshold=1000):
28-
"""Format the result of a timing in different units depending on a
29-
threshold number of base units.
30-
31-
Parameters
32-
----------
33-
num : float
34-
suffix : str
35-
threshold : int
36-
37-
Returns
38-
-------
39-
fmt : str
40-
A pretty formatted amount of time
41-
42-
Notes
43-
-----
44-
* Based on http://stackoverflow.com/a/1094933/564538
45-
"""
46-
base_unit = 1000000000
47-
num *= base_unit # smallest unit is 1 / 1000000000 of the biggest unit
48-
for unit in ['n', 'u', 'm', '']:
49-
if num < threshold:
50-
return '%3.2f %s%s' % (num, unit, suffix)
51-
num /= threshold
52-
return '%.2f %s' % (num, suffix)
53-
54-
5527
timer = LineWatcher()
5628

5729

0 commit comments

Comments
 (0)