##// END OF EJS Templates
time(it) magic: format timespans up to hours in human readable form...
Jan Schulz -
Show More
@@ -675,7 +675,7 b' python-profiler package from non-free.""")'
675 del sys.modules[main_mod_name]
675 del sys.modules[main_mod_name]
676
676
677 return stats
677 return stats
678
678
679 @skip_doctest
679 @skip_doctest
680 @line_cell_magic
680 @line_cell_magic
681 def timeit(self, line='', cell=None):
681 def timeit(self, line='', cell=None):
@@ -746,31 +746,6 b' python-profiler package from non-free.""")'
746 those from %timeit."""
746 those from %timeit."""
747
747
748 import timeit
748 import timeit
749 import math
750
751 # XXX: Unfortunately the unicode 'micro' symbol can cause problems in
752 # certain terminals. Until we figure out a robust way of
753 # auto-detecting if the terminal can deal with it, use plain 'us' for
754 # microseconds. I am really NOT happy about disabling the proper
755 # 'micro' prefix, but crashing is worse... If anyone knows what the
756 # right solution for this is, I'm all ears...
757 #
758 # Note: using
759 #
760 # s = u'\xb5'
761 # s.encode(sys.getdefaultencoding())
762 #
763 # is not sufficient, as I've seen terminals where that fails but
764 # print s
765 #
766 # succeeds
767 #
768 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
769
770 #units = [u"s", u"ms",u'\xb5',"ns"]
771 units = [u"s", u"ms",u'us',"ns"]
772
773 scaling = [1, 1e3, 1e6, 1e9]
774
749
775 opts, stmt = self.parse_options(line,'n:r:tcp:',
750 opts, stmt = self.parse_options(line,'n:r:tcp:',
776 posix=False, strict=False)
751 posix=False, strict=False)
@@ -854,16 +829,8 b' python-profiler package from non-free.""")'
854
829
855 best = min(timer.repeat(repeat, number)) / number
830 best = min(timer.repeat(repeat, number)) / number
856
831
857 if best > 0.0 and best < 1000.0:
832 print u"%d loops, best of %d: %s per loop" % (number, repeat,
858 order = min(-int(math.floor(math.log10(best)) // 3), 3)
833 _format_time(best, precision))
859 elif best >= 1000.0:
860 order = 0
861 else:
862 order = 3
863 print u"%d loops, best of %d: %.*g %s per loop" % (number, repeat,
864 precision,
865 best * scaling[order],
866 units[order])
867 if tc > tc_min:
834 if tc > tc_min:
868 print "Compiler time: %.2f s" % tc
835 print "Compiler time: %.2f s" % tc
869
836
@@ -966,13 +933,13 b' python-profiler package from non-free.""")'
966 cpu_user = end[0]-st[0]
933 cpu_user = end[0]-st[0]
967 cpu_sys = end[1]-st[1]
934 cpu_sys = end[1]-st[1]
968 cpu_tot = cpu_user+cpu_sys
935 cpu_tot = cpu_user+cpu_sys
969 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
936 print "CPU times: user %s, sys: %s, total: %s" % \
970 (cpu_user,cpu_sys,cpu_tot)
937 (_format_time(cpu_user),_format_time(cpu_sys),_format_time(cpu_tot))
971 print "Wall time: %.2f s" % wall_time
938 print "Wall time: %s" % _format_time(wall_time)
972 if tc > tc_min:
939 if tc > tc_min:
973 print "Compiler : %.2f s" % tc
940 print "Compiler : %s" % _format_time(tc)
974 if tp > tp_min:
941 if tp > tp_min:
975 print "Parser : %.2f s" % tp
942 print "Parser : %s" % _format_time(tp)
976 return out
943 return out
977
944
978 @skip_doctest
945 @skip_doctest
@@ -1091,3 +1058,43 b' def parse_breakpoint(text, current_file):'
1091 return current_file, int(text)
1058 return current_file, int(text)
1092 else:
1059 else:
1093 return text[:colon], int(text[colon+1:])
1060 return text[:colon], int(text[colon+1:])
1061
1062 def _format_time(timespan, precision=3):
1063 """Formats the timespan in a human readable form"""
1064 import math
1065 # XXX: Unfortunately the unicode 'micro' symbol can cause problems in
1066 # certain terminals. Until we figure out a robust way of
1067 # auto-detecting if the terminal can deal with it, use plain 'us' for
1068 # microseconds. I am really NOT happy about disabling the proper
1069 # 'micro' prefix, but crashing is worse... If anyone knows what the
1070 # right solution for this is, I'm all ears...
1071 #
1072 # Note: using
1073 #
1074 # s = u'\xb5'
1075 # s.encode(sys.getdefaultencoding())
1076 #
1077 # is not sufficient, as I've seen terminals where that fails but
1078 # print s
1079 #
1080 # succeeds
1081 #
1082 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
1083
1084 #units = [u'h', u'min', u"s", u"ms",u'\xb5',"ns"]
1085 units = [u'h', u'min', u"s", u"ms",u'us',"ns"]
1086 scaling = [1./3600, 1./60, 1, 1e3, 1e6, 1e9]
1087
1088 if timespan > 0.0 and timespan < 60.0:
1089 order = min(-int(math.floor(math.log10(timespan)) // 3), 3)+2
1090 elif timespan >= 3660.0:
1091 # hours
1092 order = 0
1093 elif timespan >= 60.0:
1094 # minutes
1095 order = 1
1096 else:
1097 order = 5
1098
1099 ret = u"%.*g %s" % (precision, timespan * scaling[order], units[order])
1100 return ret
General Comments 0
You need to be logged in to leave comments. Login now