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 threading
# Fonction simulant une tâche lourde (ex: traitement d'image, calculs intensifs)
def tache_lourde():
# Fonction qui effectue une partie du travail
def tache_lourde(part_debut, part_fin, resultat, index):
somme = 0
for _ in range(10**7): # Boucle qui prend un certain temps
for i in range(part_debut, part_fin):
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
start = time.time()
for _ in range(4): # Exécuter 4 tâches séquentiellement
tache_lourde()
somme_total = sum(range(N)) # Exécution en une seule fois
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)
resultats = [0] * THREADS # Stocker les résultats des threads
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)
thread.start()
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()
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