-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
93 lines (76 loc) · 2.57 KB
/
main.py
File metadata and controls
93 lines (76 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import pandas
import time
import multiprocessing
import threading
FILE_PATH = "dados-inep-enade.csv"
def calcular_media_por_sexo(dataframe):
""""
Calcula a média das notas por sexo em um DataFrame.
"""
return dataframe.groupby("TP_SEXO")["MEDIA_NOTAS"].mean()
def dividir_dataframe(dataframe, n):
"""
Divide um DataFrame em 'n' partes quase iguais.
Retorna uma lista com os pedaços.
"""
partes = []
total_linhas = len(dataframe)
linhas_por_parte = total_linhas // n
for i in range(n):
inicio = i * linhas_por_parte
if i == n - 1:
fim = total_linhas
else:
fim = (i + 1) * linhas_por_parte
parte = dataframe.iloc[inicio:fim]
partes.append(parte)
return partes
def abordagem_processos():
""""
Abordagem usando múltiplos processos para calcular a média das notas por sexo.
"""
partes = dividir_dataframe(data, 4)
with multiprocessing.Pool(processes=4) as pool:
resultados = pool.map(calcular_media_por_sexo, partes)
return pandas.concat(resultados).groupby(level=0).mean()
def abordagem_threads():
""""
Abordagem usando múltiplas threads para calcular a média das notas por sexo.
"""
partes = dividir_dataframe(data, 4)
resultados = []
lock = threading.Lock()
def worker(dataframe):
resultado = calcular_media_por_sexo(dataframe)
with lock:
resultados.append(resultado)
threads = []
for parte in partes:
thread = threading.Thread(target=worker, args=(parte,))
threads.append(thread)
for t in threads:
t.start()
for t in threads:
t.join()
return pandas.concat(resultados).groupby(level=0).mean()
if __name__ == "__main__":
data = pandas.read_csv(FILE_PATH)
inicio_proc = time.time()
media_proc = abordagem_processos()
tempo_proc = time.time() - inicio_proc
inicio_thread = time.time()
media_thread = abordagem_threads()
tempo_thread = time.time() - inicio_thread
melhor = "processos" if tempo_proc < tempo_thread else "threads"
media_geral = (media_proc + media_thread) / 2
sexo_melhor = media_geral.idxmax()
melhor_media = media_geral.max()
print("\n--- RESULTADOS ---")
print("Médias (processos):")
print(media_proc)
print("\nMédias (threads):")
print(media_thread)
print(f"\nTempo (processos): {tempo_proc:.3f} s")
print(f"Tempo (threads): {tempo_thread:.3f} s")
print(f"Melhor desempenho: {melhor}")
print(f"Sexo com melhor média: {sexo_melhor} ({melhor_media:.2f})")