NumPy – 8 – Profiling e timing

mib0

Sempre su IPython copio qui continuando da qui.

Uno scrive, probabilmente non nel modo più efficiente, sapete com’è, dice Knuth “We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil“.

But once you have your code working, it can be useful to dig into its efficiency a bit.
IPython provides access to a wide array of functionality for this kind of timing and profiling of code. Here we’ll discuss the following IPython magic commands:

  • %time: Time the execution of a single statement
  • %timeit: Time repeated execution of a single statement for more accuracy
  • %prun: Run code with the profiler
  • %lprun: Run code with the line-by-line profiler
  • %memit: Measure the memory use of a single statement
  • %mprun: Run code with the line-by-line memory profiler

The last four commands are not bundled with IPython–you’ll need to get the line_profiler and memory_profiler extensions, which we will discuss in the following sections.

Controllare i tempi di porzioni di codice con %timeit e %time

%timeit
np46
%time
np47
For %time as with %timeit, using the double-percent-sign cell magic syntax allows timing of multiline scripts:

np48

For more information on %time and %timeit, as well as their available options, use the IPython help functionality (i.e., type %time? at the IPython prompt).

Profiling script interi con %prun
Python contains a built-in code profiler (which you can read about in the Python documentation), but IPython offers a much more convenient way to use this profiler, in the form of the magic function %prun.

np49

The result is a table that indicates, in order of total time on each function call, where the execution is spending the most time. In this case, the bulk of execution time is in the list comprehension inside sum_of_lists. From here, we could start thinking about what changes we might make to improve the performance in the algorithm.
For more information on %prun, as well as its available options, use the IPython help functionality (i.e., type %prun? at the IPython prompt).

È possibile profilare singole linee con %lprun. Bisogna installare line_profiler, cosa che non faccio.
L’uso della memoria si profila con %memit e %mprun, previa installazione di memory_profiler.

Sono tutte operazioni specialistiche, da approfondire se dovessero servire, per adesso le metto tra le cose da fare 😉

:mrgreen:

Annunci
Post a comment or leave a trackback: Trackback URL.

Trackbacks

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...

%d blogger hanno fatto clic su Mi Piace per questo: