From 24a850763894998e584a279e86b3c1a73a0aa9b5 Mon Sep 17 00:00:00 2001 From: Eugene Toder Date: Mon, 18 Nov 2024 19:02:26 -0500 Subject: [PATCH] Add an option to specify time format str(float) prints 16 decimal places, which is too verbose and much more than the precision in the profiles. Print 7 digits by default and allow setting the format with --time-format. --- gprof2dot.py | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/gprof2dot.py b/gprof2dot.py index a4ab0c8..2e65620 100755 --- a/gprof2dot.py +++ b/gprof2dot.py @@ -44,6 +44,7 @@ MULTIPLICATION_SIGN = chr(0xd7) +TIME_FORMAT = "%.7g" def times(x): @@ -52,6 +53,9 @@ def times(x): def percentage(p): return "%.02f%%" % (p*100.0,) +def fmttime(t): + return TIME_FORMAT % t + def add(a, b): return a + b @@ -112,7 +116,7 @@ def __str__(self): return 'unspecified event %s' % self.event.name -class Event(object): +class Event: """Describe a kind of event, and its basic operations.""" def __init__(self, name, null, aggregator, formatter = str): @@ -124,12 +128,6 @@ def __init__(self, name, null, aggregator, formatter = str): def __repr__(self): return self.name - def __eq__(self, other): - return self is other - - def __hash__(self): - return id(self) - def null(self): return self._null @@ -159,9 +157,9 @@ def format(self, val): # Used only when totalMethod == callstacks TOTAL_SAMPLES = Event("Samples", 0, add, times) -TIME = Event("Time", 0.0, add, lambda x: '(' + str(x) + ')') +TIME = Event("Time", 0.0, add, lambda x: '(' + fmttime(x) + ')') TIME_RATIO = Event("Time ratio", 0.0, add, lambda x: '(' + percentage(x) + ')') -TOTAL_TIME = Event("Total time", 0.0, fail) +TOTAL_TIME = Event("Total time", 0.0, fail, fmttime) TOTAL_TIME_RATIO = Event("Total time ratio", 0.0, fail, percentage) labels = { @@ -175,7 +173,7 @@ def format(self, val): totalMethod = 'callratios' -class Object(object): +class Object: """Base class for all objects in profile which can store events.""" def __init__(self, events=None): @@ -184,12 +182,6 @@ def __init__(self, events=None): else: self.events = events - def __hash__(self): - return id(self) - - def __eq__(self, other): - return self is other - def __lt__(self, other): return id(self) < id(other) @@ -204,8 +196,7 @@ def __getitem__(self, event): def __setitem__(self, event, value): if value is None: - if event in self.events: - del self.events[event] + self.events.pop(event, None) else: self.events[event] = value @@ -3632,7 +3623,7 @@ def naturalJoin(values): def main(argv=sys.argv[1:]): """Main program.""" - global totalMethod + global totalMethod, TIME_FORMAT formatNames = list(formats.keys()) formatNames.sort() @@ -3697,6 +3688,10 @@ def main(argv=sys.argv[1:]): action="store_true", dest="show_samples", default=False, help="show function samples") + optparser.add_option( + '--time-format', + default=TIME_FORMAT, + help="format to use for showing time values [default: %default]") optparser.add_option( '--node-label', metavar='MEASURE', type='choice', choices=labelNames, @@ -3785,6 +3780,7 @@ def main(argv=sys.argv[1:]): theme.skew = options.theme_skew totalMethod = options.totalMethod + TIME_FORMAT = options.time_format try: Format = formats[options.format]