diff --git a/multithreading.py b/multithreading.py new file mode 100644 index 0000000000000000000000000000000000000000..a9d20f607ae682ac7803283aa489529e81e3177a --- /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")