(header='--------------')
with _stime('testing') as o:
        do_something()
        o.t1
        do_something()
        o.t2
with _stime('testing') as o:
        do_something()
        o.t1
        do_something()
        o.t2
with _stime ( testing ) as o : do_something () o . t1 do_something () o . t2
def _stime(header='--------------'):
    """
    with _stime('testing') as o:
        do_something()
        o.t1
        do_something()
        o.t2
    """
    _trace(_get_caller(3), header)

    class ll:

        def __init__(self):
            self.values = []
            self.start = time.clock()

        def __getattr__(self, name):
            self.L(name)

        def L(self, name):
            self.values.append((name, time.clock()))
            if len(self.values) == 1:
                _trace(
                    ' - {}: {:.6}'.format(o.values[0][0], o.values[0][1] - o.start))
            else:
                t1, t2 = o.values[-2:]
                _trace('{} - {}: {:.6}'.format(t1[0], t2[0], t2[1] - t1[1]))

    o = ll()
    yield o
    o.fin = time.clock()

    if o.values:
        _trace('{} - : {:.6}'.format(o.values[-1][0], o.fin - o.values[-1][1]))

    _trace('total: {:.6}'.format(o.fin - o.start))
def _stime ( header = '--------------' ) : _trace ( _get_caller ( 3 ) , header ) class ll : def __init__ ( self ) : self . values = [ ] self . start = time . clock ( ) def __getattr__ ( self , name ) : self . L ( name ) def L ( self , name ) : self . values . append ( ( name , time . clock ( ) ) ) if len ( self . values ) == 1 : _trace ( ' - {}: {:.6}' . format ( o . values [ 0 ] [ 0 ] , o . values [ 0 ] [ 1 ] - o . start ) ) else : t1 , t2 = o . values [ - 2 : ] _trace ( '{} - {}: {:.6}' . format ( t1 [ 0 ] , t2 [ 0 ] , t2 [ 1 ] - t1 [ 1 ] ) ) o = ll ( ) yield o o . fin = time . clock ( ) if o . values : _trace ( '{} - : {:.6}' . format ( o . values [ - 1 ] [ 0 ] , o . fin - o . values [ - 1 ] [ 1 ] ) ) _trace ( 'total: {:.6}' . format ( o . fin - o . start ) )