input for testprofile py and testcprofile py important this stuff is touchy if you modify anything above the test class you ll have to regenerate the stats by running the two test files all numbers in the expected output are relevant if you change the formatting of pstats please don t just regenerate the expected output without checking very carefully that not a single number has changed in order to have reproducible time we simulate a timer in the global variable ticks which represents simulated time in milliseconds we can t use a helper function increment the timer since it would be included in the profile and would appear to consume all the time 1 call 1000 ticks total 270 ticks local 730 ticks in subfunctions 23 calls total 170 ticks total 150 ticks local 3 primitive calls 130 20 and 20 ticks total including 116 17 17 ticks local 20 calls 1 tick local 2 calls 300 ticks total 20 ticks local 260 ticks in subfunctions 4 calls 30 ticks total 29 ticks local 1 tick in subfunctions 8 calls 50 ticks local 39 ticks local 11 ticks in subfunctions 8 calls 10 ticks total 8 ticks local 2 ticks in subfunctions 28 calls 1 tick local in order to have reproducible time we simulate a timer in the global variable ticks which represents simulated time in milliseconds we can t use a helper function increment the timer since it would be included in the profile and would appear to consume all the time 1 call 1000 ticks total 270 ticks local 730 ticks in subfunctions 300 300 130 23 calls total 170 ticks total 150 ticks local 3 primitive calls 130 20 and 20 ticks total including 116 17 17 ticks local 20 calls 1 tick local 2 calls 300 ticks total 20 ticks local 260 ticks in subfunctions 30 30 50 50 50 70 4 calls 30 ticks total 29 ticks local 1 tick in subfunctions 1 0 0 50 20 8 calls 50 ticks local 39 ticks local 11 ticks in subfunctions 1 10 8 calls 10 ticks total 8 ticks local 2 ticks in subfunctions 0 1 x 2 3 x 2 28 calls 1 tick local
import sys





TICKS = 42000

def timer():
    return TICKS

def testfunc():
    
    
    global TICKS
    TICKS += 99
    helper()                            
    helper()                            
    TICKS += 171
    factorial(14)                       

def factorial(n):
    
    
    
    
    global TICKS
    if n > 0:
        TICKS += n
        return mul(n, factorial(n-1))
    else:
        TICKS += 11
        return 1

def mul(a, b):
    
    
    global TICKS
    TICKS += 1
    return a * b

def helper():
    
    
    global TICKS
    TICKS += 1
    helper1()                           
    TICKS += 2
    helper1()                           
    TICKS += 6
    helper2()                           
    TICKS += 3
    helper2()                           
    TICKS += 2
    helper2()                           
    TICKS += 5
    helper2_indirect()                  
    TICKS += 1

def helper1():
    
    
    global TICKS
    TICKS += 10
    hasattr(C(), "foo")                 
    TICKS += 19
    lst = []
    lst.append(42)                      
    sys.exception()                     

def helper2_indirect():
    helper2()                           
    factorial(3)                        

def helper2():
    
    
    global TICKS
    TICKS += 11
    hasattr(C(), "bar")                 
    TICKS += 13
    subhelper()                         
    TICKS += 15

def subhelper():
    
    
    global TICKS
    TICKS += 2
    for i in range(2):                  
        try:
            C().foo                     
        except AttributeError:
            TICKS += 3                  

class C:
    def __getattr__(self, name):
        
        
        global TICKS
        TICKS += 1
        raise AttributeError