github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/examples/adhoc/adhoc-spy.py (about)

     1  #!/usr/bin/env python
     2  from random import randint
     3  from math import sqrt
     4  
     5  
     6  def is_prime(n):
     7      for i in range(2, n):
     8          if i * i > n:
     9              return True
    10          if n % i == 0:
    11              return False
    12      return False
    13  
    14  
    15  def slow(n):
    16      sum = 0
    17      for i in range(1, n+1):
    18          sum += i
    19      return sum
    20  
    21  
    22  def fast(n):
    23      sum = 0
    24      root = int(sqrt(n))
    25      for a in range(1, n+1, root):
    26          b = min(a + root - 1, n)
    27          sum += (b - a + 1) * (a + b) // 2
    28      return sum
    29  
    30  
    31  def run():
    32      base = randint(1, 10**6)
    33      for i in range(2*10**6):
    34          [fast, slow][is_prime(base + i)](randint(1, 10**4))
    35  
    36  
    37  if __name__ == '__main__':
    38      run()