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 authored and jrfonseca committed Nov 22, 2024
1 parent 4f75c58 commit d5f801c
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions gprof2dot.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@


MULTIPLICATION_SIGN = chr(0xd7)
timeFormat = "%.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 timeFormat % t

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 Down Expand Up @@ -3632,7 +3624,7 @@ def naturalJoin(values):
def main(argv=sys.argv[1:]):
"""Main program."""

global totalMethod
global totalMethod, timeFormat

formatNames = list(formats.keys())
formatNames.sort()
Expand Down Expand Up @@ -3697,6 +3689,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=timeFormat,
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 +3781,7 @@ def main(argv=sys.argv[1:]):
theme.skew = options.theme_skew

totalMethod = options.totalMethod
timeFormat = options.time_format

try:
Format = formats[options.format]
Expand Down

0 comments on commit d5f801c

Please sign in to comment.