Probability of using a line of code

156 Views Asked by At

When trying to find bottlenecks in code (as Python) there are usually two possibilities for a programmer:

  • use a statistical profiling program
  • use a deterministic profiling program

Both will run the code analyzing which functions are being called the most, one will inspect every certain period of time and the other every line.

The "problem" is that the code has to be ran, which means an amount of time for each test representing a situation and the need to create those tests. My aim is to find out how to do a "static profiling" for a new programming language with performance focus.

Surely there are an infinite amount of possibilities for each case, but I believe math has the power to solve this, more than using brute force.

Now that my goal is explained:

Everything is fine when there is no recursion (Python examples)

from random import randint
a = randint(1, 3)
b = 3
if a == b:
    print('yep')
else:
    print('nop')

Here I can suppose $$ \begin{align} A & = \{ 1, 2, 3 \} \\ B & = \{ 3 \}\\ if & = \frac{| A \cap B |}{|A| \cdot |B|} = \frac13 \\ else & = \frac{| A \bigtriangleup B |}{|A| \cdot |B|} = \frac23 \\ \end{align} $$ And with recursion and without changes of possible values

from random import randint
b = 3
def example():
    a = randint(1, 3)
    if a == b:
        example()
    else:
        print('bye')
example()

I think $$ \begin{align} A & = \{ 1, 2, 3 \} \\ B & = \{ 3 \} \\ exampleInIf & = 1 \\ exampleInModule & = 1 \\ \end{align} \\ \begin{cases} example = if \cdot 1 + 1 \\ if = \frac13 \cdot example \\ else = \frac23 \cdot example \\ \end{cases} \dotso \begin{cases} example = \frac32 \\ if = \frac12 \\ else = 1 \\ \end{cases} $$ but what can I do when WITH recursion and WITH changes of possible values?

from random import randint
b = 3
a = randint(1, 3)
def example():
    if a == b:
        a += 1
        example()
    else:
        print('bye')
example()

I'll be glad if you can also help me understand how to represent other kind of statements than ==, as >, <, >=, <=, or even and, or