"""Code Profiling Class

profile.Profile sub-class using pygame.time.get_ticks.

The standard profile module uses the python timer; this code
replaces it with the pygame.time timer. Note that, as with the
standard profiler, machine-specific calibration may result in
more accurate measurements - check the profile.py module
documentation for more information.
"""
__author__ = "Kevin Turner"
__copyright__ = "Copyright (c) 2001 Kevin Turner"
__license__ = "Public Domain"
__version__ = "1.0"



import profile, pstats
import pygame

def f8_i(x):
    """using an integer clock, so lose the floating-point formatting."""

    return string.rjust(`x`,8)

pstats.f8 = f8_i

class PygameProfile(profile.Profile):
    """Profile sub-class using pygame.time.get_ticks.

    Seems to be pretty fast and precise.
    """

    def __init__(self):
        profile.Profile.__init__(self, pygame.time.get_ticks)

    def trace_dispatch_i(self, frame, event, arg):
        t = self.timer() - self.t # - 0 # Calibration constant
        # (but this needs no calibration constant on my machine)

        if self.dispatch[event](frame,t):
            self.t = self.timer()
        else:
            self.t = self.timer() - t # put back unrecorded delta
        return


def example():
    pr=PygameProfile()
    pr.run('function_to_profile(with, "args", as_needed)')
    pr.dump_stats('my_function.prof')

    # To display:

    # Also could be pstats.Stats(pr), if you still have the
    # Profile object around at this point.
    s = pstats.Stats('my_function.prof')

    s.strip_dirs().sort_stats('time')
    s.print_stats()
