Python, dice Guido

Sì lo so che ho millemila cose da finire ma ho trovato questo su G+, viene da Guido, quel Guido (krrkwido):

Some patterns for fast Python. Know any others?

  • Avoid overengineering datastructures. Tuples are better than objects (try namedtuple too though). Prefer simple fields over getter/setter functions.
  • Built-in datatypes are your friends. Use more numbers, strings, tuples, lists, sets, dicts. Also check out the collections library, esp. deque.
  • Be suspicious of function/method calls; creating a stack frame is expensive.
  • Don’t write Java (or C++, or Javascript, …) in Python.
  • Are you sure it’s too slow? Profile before optimizing!
  • The universal speed-up is rewriting small bits of code in C. Do this only when all else fails.

Naturalmente commenti, +1 e condivisioni sono più che numerabili, ecco i primi:
Sindri Avaruus: I like decorators. They can do some nifty tricks :)

Krzysztof Klinikowski: PyPy e Numba

Michael Foord: Understand the performance characteristics of basic operations on the builtin types. For example checking for membership in a list is O(N) but for a set or dictionary it is O(1). Adding lists and tuples creates new objects, inserting to the left of a list is O(N) whilst append is O(1) (use a deque instead). And so on.

Robert Collins: Don’t use threads in Python (and don’t use multiprocessing either, its about 3 times slower than something like 0mq or even just shelling out). +1 on +Michael Foord for knowing the big O of the things you are working with.
Also, understand your memory footprint: avoiding circular references can prevent large (transient) memory bloat occuring, and memory bloat hurts performance a lot (not just because gc.collect gets slower, but it can trigger paging)
I’ve written some heinous code using basic datatypes. I’d take that one off the list:)

Mmmh! mi sa che conviene leggere tutto il post, lo trovate qui
Guido van Rossum – Sept 10, 2012. 9:41 PM  -  Public.

Di mio vorrei aggiungere che gli oggetti (classi e metodi) si dimostrano utili solo quando il programma raggiunge una certa dimensione e cessa di essere un semplice script. E poi sono della vecchia scuola: terminale e linea di comando.

Quando il BDFL interviene con le sue parole di saggezza tanti nerd / geek si trovano pronti a partecipare. La stessa cosa non capita negli altri linguaggi che uso o che vorrei usare. Per fortuna: dove troverei il tempo, devo già recuperare quello perso con questo post ;-)

About these ads
Posta un commento o usa questo indirizzo per il trackback.

Trackback

Lascia un Commento

Fill in your details below or click an icon to log in:

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 )

Connessione a %s...

Iscriviti

Ricevi al tuo indirizzo email tutti i nuovi post del sito.

Unisciti agli altri 37 follower

%d bloggers like this: