how to convert code that uses cmp to python3

Marko Rauhamaa marko at pacujo.net
Fri Apr 8 08:52:34 EDT 2016


Steven D'Aprano <steve at pearwood.info>:

> I would be stunned if tuple comparisons with only a handful of values
> were slow enough that its worth caching their results with an
> lru_cache. But try it, and see how you go.

There are two ways your Python program can be slow:

 * You are doing something stupid like an O(exp(n)) when there is an
   O(n**2) available.

 * Python is inherently slower than C by a constant factor.

If you are doing something stupid, do refactor your code. But you
shouldn't try to make artificial optimizations to your code to make it
perform more like C. Write it in C, maybe partially, if you have to.

For example, your performance measurements might show that Python's
method calls are slow. That may be true but it is no invitation to
eliminate method calls from your code.


Marko



More information about the Python-list mailing list