Skip to content

Commit

Permalink
Add an option to specify time format
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
eltoder committed Nov 19, 2024
1 parent 4f75c58 commit 24a8507
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions gprof2dot.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@


MULTIPLICATION_SIGN = chr(0xd7)
TIME_FORMAT = "%.7g"


def times(x):
Expand All @@ -52,6 +53,9 @@ def times(x):
def percentage(p):
return "%.02f%%" % (p*100.0,)

def fmttime(t):
return TIME_FORMAT % t

Check warning on line 57 in gprof2dot.py

View check run for this annotation

Codecov / codecov/patch

gprof2dot.py#L57

Added line #L57 was not covered by tests

def add(a, b):
return a + b

Expand Down Expand Up @@ -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):
Expand All @@ -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

Expand Down Expand Up @@ -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 = {
Expand All @@ -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):
Expand All @@ -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)

Expand All @@ -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)

Check warning on line 199 in gprof2dot.py

View check run for this annotation

Codecov / codecov/patch

gprof2dot.py#L199

Added line #L199 was not covered by tests
else:
self.events[event] = value

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 24a8507

Please sign in to comment.