# cPy / mPy - demonstrating profiling using a decorator
# Copyright by Anthony Vogelaar, Perth Scotland
# rev 0.10  2026-09-03  initial release

from	time	import	sleep_ms, ticks_us, ticks_diff

# ================= profiling ========================

# profiling part of code using simple delta t measurement
def test1():
    t1 = ticks_us()
    sleep_ms(2)
    t2 = ticks_us()
    print(ticks_diff(t2, t1), 'uSec')
    
    
#test1()

# =========== profiling a function ====================
    
# profiling a function using simple delta t measurement
def dly2():
    sleep_ms(2)


def test2():
    t1 = ticks_us()
    dly2()
    t2 = ticks_us()
    print(ticks_diff(t2, t1), 'uSec')
    
    
#test2()

# =========== profiling by function ==================

# a decorator function for profiling non parameter functions
def printDuration(f_test):
    t1 = ticks_us()
    res = f_test()
    t2 = ticks_us()
    print(ticks_diff(t2, t1), 'uSec')
    return res


def test3():
    printDuration(dly2)
    
    
#test3()

# =========== profiling by decorator ==================

# a decorator function for profiling functions
def printDuration(f_test):

    def fut(*args, **kwargs):
        t1 = ticks_us()
        result = f_test(*args, **kwargs)
        t2 = ticks_us() - 200
        print(ticks_diff(t2, t1), 'uSec')
        return result
    
    return fut


@printDuration
def test4(dly):
    sleep_ms(dly)


#test4(2)

# ======================= end =========================
    