<jupyter_start><jupyter_text>Cython compilation<jupyter_code>%load_ext Cython
%%cython

cdef int a = 0
for i in range(10):
    a += i
print(a)
%%cython --annotate

cdef int a = 0
for i in range(10):
    a += i
print(a)
%%cython

def f(double x):
    return x**2 - x

def integrate_f(double a, double b, int N):
    cdef int i
    cdef double s, dx
    s = 0
    dx = (b-a) / N
    for i in range(N):
        s += f(a + i*dx)
    return s*dx
def fib(n):
    a, b = 0, 1
    while b < n:
        print(b, end=' ')
        a, b = b, a+b
    print()
%%time
fib(5000)
%%cython

def fib(int n):
    cdef int a, b
    a, b = 0, 1
    while b < n:
        print(b, end=' ')
        a, b = b, a+b
    print()
%%time
fib(5000)
%%cython

def primes(int nb_primes):
    cdef int n, i, len_p
    cdef int p[1000]
    if nb_primes > 1000:
        nb_primes = 1000
    
    len_p = 0
    n = 2
    while len_p < nb_primes:
        # Is n prime?
        for i in p[:len_p]:
            if n % i == 0:
                break
        
        else:
            p[len_p] = n
            len_p += 1
        n += 1
    
    # return the result in python list
    result_as_list = [prime for prime in p[:len_p]]
    return result_as_list
%%time
primes(10)
def primes(nb_primes):
    p = []
    n = 2
    while len(p) < nb_primes:
        for i in p:
            if n % i == 0:
                break
        
        else:
            p.append(n)
        
        n += 1
        
    return p
%%time
primes(10)<jupyter_output>CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 27.2 µs
{'fraction_non_alphanumeric': 0.12140575079872204, 'fraction_numerical': 0.03194888178913738, 'mean_word_length': 1.5588235294117647, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '594744', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 669, 'n_tokens_neox': 618, 'n_words': 203, 'path': 'cython/cython.ipynb', 'repo_name': 'shawnngtq/programming', 'size': '1565'}