' debug_profile.pb — Code profiling simulation
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL funcIDs(30) AS LONG
    LOCAL funcCalls(30) AS LONG
    LOCAL funcTimes(30) AS LONG
    LOCAL funcCount AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL totalCalls AS LONG
    LOCAL totalTime AS LONG
    LOCAL maxTimeFunc AS LONG
    LOCAL maxTimeVal AS LONG
    LOCAL minTimeFunc AS LONG
    LOCAL minTimeVal AS LONG
    LOCAL avgTime AS LONG
    LOCAL callCount AS LONG
    LOCAL callTime AS LONG
    LOCAL iter AS LONG
    LOCAL numFunctions AS LONG
    LOCAL pctInt AS LONG
    LOCAL nameID AS LONG
    LOCAL nameStr AS STRING

    numFunctions = 8

    PRINT "=== Code Profiler Simulation ==="
    PRINT "Functions registered: " + STR$(numFunctions)

    FOR i = 0 TO numFunctions - 1
        funcIDs(i) = i
        callCount = 10 + i * 5
        callTime = 50 + i * 30
        funcCalls(i) = callCount
        funcTimes(i) = callTime
    NEXT i
    funcCount = numFunctions

    totalCalls = 0
    totalTime = 0
    FOR i = 0 TO funcCount - 1
        totalCalls = totalCalls + funcCalls(i)
        totalTime = totalTime + funcTimes(i)
    NEXT i

    maxTimeVal = funcTimes(0)
    maxTimeFunc = 0
    minTimeVal = funcTimes(0)
    minTimeFunc = 0
    FOR i = 1 TO funcCount - 1
        IF funcTimes(i) > maxTimeVal THEN
            maxTimeVal = funcTimes(i)
            maxTimeFunc = i
        END IF
        IF funcTimes(i) < minTimeVal THEN
            minTimeVal = funcTimes(i)
            minTimeFunc = i
        END IF
    NEXT i

    PRINT "--- Profile Report ---"
    FOR i = 0 TO funcCount - 1
        nameID = funcIDs(i)
        callCount = funcCalls(i)
        callTime = funcTimes(i)
        pctInt = callTime * 100 / totalTime
        IF nameID = 0 THEN nameStr = "main"
        IF nameID = 1 THEN nameStr = "parseInput"
        IF nameID = 2 THEN nameStr = "processData"
        IF nameID = 3 THEN nameStr = "validateOutput"
        IF nameID = 4 THEN nameStr = "formatResult"
        IF nameID = 5 THEN nameStr = "writeLog"
        IF nameID = 6 THEN nameStr = "computeHash"
        IF nameID = 7 THEN nameStr = "cleanup"
        PRINT "  " + nameStr + ": " + STR$(callCount) + " calls, " + STR$(callTime) + "ms (" + STR$(pctInt) + "%)"
    NEXT i

    IF maxTimeFunc = 0 THEN nameStr = "main"
    IF maxTimeFunc = 1 THEN nameStr = "parseInput"
    IF maxTimeFunc = 2 THEN nameStr = "processData"
    IF maxTimeFunc = 3 THEN nameStr = "validateOutput"
    IF maxTimeFunc = 4 THEN nameStr = "formatResult"
    IF maxTimeFunc = 5 THEN nameStr = "writeLog"
    IF maxTimeFunc = 6 THEN nameStr = "computeHash"
    IF maxTimeFunc = 7 THEN nameStr = "cleanup"
    PRINT "Hottest: " + nameStr + " (" + STR$(maxTimeVal) + "ms)"

    IF minTimeFunc = 0 THEN nameStr = "main"
    IF minTimeFunc = 1 THEN nameStr = "parseInput"
    IF minTimeFunc = 2 THEN nameStr = "processData"
    IF minTimeFunc = 3 THEN nameStr = "validateOutput"
    IF minTimeFunc = 4 THEN nameStr = "formatResult"
    IF minTimeFunc = 5 THEN nameStr = "writeLog"
    IF minTimeFunc = 6 THEN nameStr = "computeHash"
    IF minTimeFunc = 7 THEN nameStr = "cleanup"
    PRINT "Coldest: " + nameStr + " (" + STR$(minTimeVal) + "ms)"

    PRINT "Total calls: " + STR$(totalCalls)
    PRINT "Total time: " + STR$(totalTime) + "ms"
    PRINT "=== End Profile ==="
    FUNCTION = 0
END FUNCTION
