# profile.Profile sub-class using pygame.time.get_ticks.
# by Kevin.  Public domain.  Have fun.

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()
