Skip to content
Snippets Groups Projects
Commit 9eeaebd0 authored by Rioux Jeremy's avatar Rioux Jeremy
Browse files

Merge branch 'paul' of...

parents 68aebac4 bdffaca0
No related branches found
No related tags found
No related merge requests found
Showing
with 26 additions and 9 deletions
{
"python.testing.pytestArgs": [
"."
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
\ No newline at end of file
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
File added
File added
File added
File added
File added
import time import time
import threading import threading
# Fonction simulant une tâche lourde (ex: traitement d'image, calculs intensifs) # Fonction qui effectue une partie du travail
def tache_lourde(): def tache_lourde(part_debut, part_fin, resultat, index):
somme = 0 somme = 0
for _ in range(10**7): # Boucle qui prend un certain temps for i in range(part_debut, part_fin):
somme += 1 somme += 1
return somme resultat[index] = somme # Stocker le résultat pour éviter la concurrence
# Paramètres
N = 10**7 # Taille du travail total
THREADS = 4 # Nombre de threads à utiliser
# Exécution sans multithreading # Exécution sans multithreading
start = time.time() start = time.time()
for _ in range(4): # Exécuter 4 tâches séquentiellement somme_total = sum(range(N)) # Exécution en une seule fois
tache_lourde()
end = time.time() end = time.time()
print(f"Temps sans multithreading: {end - start:.2f} secondes") print(f"Temps sans multithreading: {end - start:.2f} secondes")
# Exécution avec multithreading # Exécution avec multithreading
start = time.time() start = time.time()
threads = [] threads = []
for _ in range(4): # Lancer 4 threads en parallèle resultats = [0] * THREADS # Stocker les résultats des threads
thread = threading.Thread(target=tache_lourde) portion = N // THREADS # Taille de la charge par thread
for i in range(THREADS):
debut = i * portion
fin = N if i == THREADS - 1 else (i + 1) * portion
thread = threading.Thread(target=tache_lourde, args=(debut, fin, resultats, i))
threads.append(thread) threads.append(thread)
thread.start() thread.start()
for thread in threads: for thread in threads:
thread.join() # Attendre que tous les threads finissent thread.join() # Attendre la fin de tous les threads
somme_total_thread = sum(resultats) # Fusionner les résultats
end = time.time() end = time.time()
print(f"Temps avec multithreading: {end - start:.2f} secondes") print(f"Temps avec multithreading: {end - start:.2f} secondes")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment