import numpy as np
from random import random, randint
from time import time


def fact(n):
    if n == 0:
        return 1
    return n * fact(n - 1)


def fact_rec(n):
    res = 1
    while n > 1:
        res *= n
        n -= 1
    return res


print("--- 5 000 000 calls to fact & fact_rec ---")
begin = time()
for i in range(5_000_000):
    val = randint(0, 10)
    if fact(val) != fact_rec(val):
        print("ouille")
end = time()
print(f"Duration = {int((end-begin) * 1000)} ms")

print("--- 5 000 000 list comprehensions ---")
begin = time()
for i in range(5_000_000):
    data = [randint(0, 10), randint(0, 10), randint(0, 10), randint(0, 10), randint(0, 10), randint(0, 10), randint(0, 10), randint(0, 10), randint(0, 10), randint(0, 10)]
    data2 = [x * 2 for x in data if x < 11]
    assert len(data2) == 10
    assert data[0] * 2 == data2[0]
    assert data[-1] * 2 == data2[-1]
    assert sum(data) * 2 == sum(data2)
end = time()
print(f"Duration = {int((end-begin) * 1000)} ms")

print("--- 5 000 000 list sorts ---")
begin = time()
for i in range(5_000_000):
    val = randint(0, 9)
    data = [40, 20, 50, 30, 10 + val]
    data.sort()
    res = sum(data)
    assert res == 150 + val
end = time()
print(f"Duration = {int((end-begin) * 1000)} ms")

print("--- 500 000 vector (ndarray) sorts ---")
begin = time()
for i in range(500_000):
    size = randint(5, 20)
    data = np.random.rand(size)
    data.sort()
    res = sum(data)
    assert res > 0
end = time()
print(f"Duration = {int((end-begin) * 1000)} ms")
