Last active
November 9, 2018 02:20
-
-
Save rafaelhenrique/f18bbfafd3c9066f3eca3f1f024937ed to your computer and use it in GitHub Desktop.
Palestra: Performance Python - Como construir métodos e funções fast and furious!?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Qual código é mais veloz (e furioso)? | |
# Primeiro exemplo | |
grupys_infinitos = [] | |
for edicao in range(0, 10000): | |
grupys_infinitos.append(f'Grupy-SP {edicao}') | |
print(grupys_infinitos) | |
# Segundo exemplo | |
grupys_infinitos = [f'Grupy-SP {edicao}' | |
for edicao in range(0, 10000)] | |
print(grupys_infinitos) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import timeit | |
meu_codigo_1 = """ | |
grupys_infinitos = [] | |
for edicao in range(0,10000): | |
grupys_infinitos.append(f'Grupy-SP {edicao}') | |
""" | |
print(timeit.timeit(meu_codigo_1, number=10000)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import timeit | |
meu_codigo_2 = """ | |
grupys_infinitos = [f'Grupy-SP {edicao}' | |
for edicao in range(0,10000)] | |
""" | |
print(timeit.timeit(meu_codigo_2, number=10000)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# O que consome mais uma tupla ou uma lista? | |
from sys import getsizeof | |
tupla = tuple(f'Grupy-SP {edicao}' | |
for edicao in range(0, 10000)) | |
print(getsizeof(tupla), ' bytes') | |
lista = list(f'Grupy-SP {edicao}' | |
for edicao in range(0, 10000)) | |
print(getsizeof(lista), ' bytes') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# necessita previamente ter instalado a lib memory_profiler com o comando: pip install memory_profiler | |
from memory_profiler import profile | |
@profile | |
def multiplica_letra(): | |
a = 'a' * 10000000 | |
b = 'b' * 10000001 | |
del b | |
return a | |
if __name__ == '__main__': | |
multiplica_letra() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# necessita previamente ter instalado a lib memory_profiler com o comando: pip install memory_profiler | |
from memory_profiler import profile | |
@profile | |
def grupys_infinitos_01(): | |
grupys_infinitos = [] | |
for edicao in range(0, 10000): | |
grupys_infinitos.append(f'Grupy-SP {edicao}') | |
return grupys_infinitos | |
if __name__ == '__main__': | |
grupys_infinitos_01() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# necessita previamente ter instalado a lib memory_profiler com o comando: pip install memory_profiler | |
from memory_profiler import profile | |
@profile | |
def grupys_infinitos_02(): | |
return [f'Grupy-SP {edicao}' | |
for edicao in range(0, 10000)] | |
if __name__ == '__main__': | |
grupys_infinitos_02() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cProfile | |
import io | |
import pstats | |
import time | |
pr = cProfile.Profile() | |
pr.enable() | |
# [your code begin...] | |
def brasilia(): | |
time.sleep(10) | |
def fusca(): | |
time.sleep(9) | |
brasilia() | |
fusca() | |
# [your code end...] | |
pr.disable() | |
stream = io.StringIO() | |
ps = pstats.Stats(pr, stream=stream).sort_stats('cumulative') | |
ps.print_stats(10) | |
print(stream.getvalue()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# necessita previamente ter instalado a lib pytest-benchmark com o comando: pip install pytest-benchmark | |
def soma(x, y): | |
return x + y | |
def teste_soma(benchmark): | |
# realiza benchmark da função de soma | |
result = benchmark(soma, 10, 20) | |
# verifica se a soma deu o resultado esperado | |
assert result == 30 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# necessita previamente ter instalado a lib pytest-benchmark com o comando: pip install pytest-benchmark | |
def somatorio(numeros): | |
resultado = 0 | |
for numero in numeros: | |
resultado += numero | |
return resultado | |
def teste_somatorio(benchmark): | |
# realiza benchmark da função de somatorio | |
result = benchmark(somatorio, [10, 20]) | |
# verifica se a somatorio deu o resultado esperado | |
assert result == 30 | |
def teste_sum(benchmark): | |
# realiza benchmark da função de sum | |
result = benchmark(sum, [10, 20]) | |
# verifica se a sum deu o resultado esperado | |
assert result == 30 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment