Загрузка данных


import math
import time
import sympy as sp
from concurrent.futures import ThreadPoolExecutor


def f(x):
    return math.cos(x) * math.cos(x / 2) * math.cos(x / 3)


a = 0
b = math.pi
n = 9048
h = (b - a) / n


def sympy_method():
    x = sp.symbols('x')
    function = sp.cos(x) * sp.cos(x / 2) * sp.cos(x / 3)

    result = sp.integrate(function, (x, 0, sp.pi))
    return result, float(result.evalf())



def single_thread_method():
    total = 0.0

    for i in range(1, n + 1):
        total += f(a + i * h)

    return total * h



def calculate_part(start, end):
    total = 0.0

    for i in range(start + 1, end + 1):
        total += f(a + i * h)

    return total


def multi_thread_method():
    workers = 4
    chunk_size = math.ceil(n / workers)

    chunks = [
        (start, min(start + chunk_size, n))
        for start in range(0, n, chunk_size)
    ]

    with ThreadPoolExecutor(max_workers=workers) as executor:
        partial_sums = list(
            executor.map(
                lambda chunk: calculate_part(*chunk),
                chunks
            )
        )

    return sum(partial_sums) * h



start = time.perf_counter()
exact_expression, sympy_result = sympy_method()
sympy_time = time.perf_counter() - start

start = time.perf_counter()
single_result = single_thread_method()
single_time = time.perf_counter() - start

start = time.perf_counter()
multi_result = multi_thread_method()
multi_time = time.perf_counter() - start