From e7b70717ddbba571931d24a00cdfa6beb3f1ceb1 Mon Sep 17 00:00:00 2001 From: Paul Chevalier <paul.chevailer@student-cs.fr> Date: Wed, 12 Mar 2025 14:51:08 +0100 Subject: [PATCH] multi --- multithreading.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 multithreading.py diff --git a/multithreading.py b/multithreading.py new file mode 100644 index 0000000..a9d20f6 --- /dev/null +++ b/multithreading.py @@ -0,0 +1,29 @@ +import time +import threading + +# Fonction simulant une tâche lourde (ex: traitement d'image, calculs intensifs) +def tache_lourde(): + somme = 0 + for _ in range(10**7): # Boucle qui prend un certain temps + somme += 1 + return somme + +# Exécution sans multithreading +start = time.time() +for _ in range(4): # Exécuter 4 tâches séquentiellement + tache_lourde() +end = time.time() +print(f"Temps sans multithreading: {end - start:.2f} secondes") + +# Exécution avec multithreading +start = time.time() +threads = [] +for _ in range(4): # Lancer 4 threads en parallèle + thread = threading.Thread(target=tache_lourde) + threads.append(thread) + thread.start() + +for thread in threads: + thread.join() # Attendre que tous les threads finissent +end = time.time() +print(f"Temps avec multithreading: {end - start:.2f} secondes") -- GitLab