What is the right way to perform performance tests in Python?

Asked

Viewed 365 times

3

In answering that question Python syntax both accepts "+" and "," in the "print" command()"? :

Pergunta de referência

I submitted the following reply:

Caso sua intenção seja imprimir apenas uma linha, ambos os métodos não possuem diferença. 

Porém se estiver fazendo print em massa dentro de laço, 
por exemplo ao imprimir um relatório, imprimindo uma tabela 
ou imprimindo um planilha. E desempenho é o que importa,
a maneira como você faz a concatenação dos dados pode ser
a diferença entre imprimir os dados no intervalo do cafezinho
ou deixar seus dados sendo impressos durante o almoço.

Fiz o teste de desempenho com alguns tipos de concatenação 
para saber qual é mais rápido:
import time

#Concatenar usando o operador +
def função1():
  nome = "Júlia"
  print("Olá " + nome)

#Concatenar usando o próprio print()
def função2():
  nome = "Júlia"
  print("Olá ",nome)

#Concatenar usando formatação de string
def função3():
  nome = "Júlia"
  print("Olá %s" % nome)

#Concatenar usando o método join()
def função4():
  nome = "Júlia"
  print("Olá %s".join([nome]))

#Aguarda 5s para estabilização dos processos de inicialização
time.sleep(5)

#Mede o desempenho da função1()
inicio = time.time()
função1()
print("Concatenado com + levou    %s segundos." % (time.time() - inicio))

#Mede o desempenho da função2()
start_time = time.time()
função2()
print("Concatenado com , levou    %s segundos." % (time.time() - inicio))

#Mede o desempenho da função3()
start_time = time.time()
função3()
print("Concatenado com %% levou    %s segundos." % (time.time() - inicio))

#Mede o desempenho da função3()
start_time = time.time()
função4()
print("Concatenado com join levou %s segundos." % (time.time() - inicio))
Os resultados foram esses:

Olá Júlia
Concatenado com + levou    0.00017952919006347656 segundos.
Olá  Júlia
Concatenado com , levou    0.0002543926239013672 segundos.
Olá Júlia
Concatenado com % levou    0.0002880096435546875 segundos.
Olá Júlia
Concatenado com join levou 0.0003311634063720703 segundos.

I was informed in the comments that this is not the correct way to measure performance in python. So here’s the question-title, what’s the right way to perform Python performance tests?

  • 1

    When it is something of the general language, it is better to use the tag "Python" - "python3.x" it can be a complementary tag, and not be used in the great friendship of the times, since this is the standard version of the language.

  • To make it clear, what performance are we talking about? Just runtime or would like to evaluate resource utilization, number of operations, etc?

  • @Woss, It would be interesting to know how to make a complete evaluation of a code fragment.

  • @Woss I’ve been researching the modules dis . andtimeit, I was just going to read the profile

  • Dude, the test you’re trying to perform on this print issue is irrelevant, why do I say this? Let’s go in part, the + (plus) joins the strings, the , (comma) pass separate arguments where the print use say the *args to receive a list of variables and then play and align in the print, the rest is string manipulation so it takes longer, summarizing, are for different use that in your case has the same output, it is only a question of understanding what each one does

  • I’ve been told this @Guilhermefrançadeoliveira, the question is which is the right way to do a python performance test. Another thing is not print being spanned are the concatenation forms of strings.

  • Yes, so I just left a comment to try to clarify any kind of doubt, but not an answer, in a matter of performance already complicates a little to answer, as @Woss said above, What performance..., because for each goal you can have an answer and a different result, are things that can even depend on your machine, Running time? use of memory? among others. there is not a general, and if you want everything, you would have to collect each information separately and then use a regression in order to find a media between everything.

Show 2 more comments

1 answer

4


Essentially you have two quick ways to test the performance of each method.

You can choose to run each function within the function timeit.():

def foo():
    # ...

duration = timeit.timeit(foo, number=1000)

If you can use IPython, you have access to the function %timeit:

In [2]: %timeit função1()
773 µs ± 46.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [3]: %timeit função2()
278 µs ± 3.91 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [4]: %timeit função3()
135 µs ± 763 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [5]: %timeit função4()
126 µs ± 1.31 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
  • 2

    One question I raised in the other answer was the independence of the tests. Doing them together will give a different result than running the separate tests. What can you say about this?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.