Python comparisons speed depends from the result

Published at Aug. 18, 2010 | Tagged with: , , ,

Recently I decided to check whether "less than or equal"(<=) is slower than "bigger"(>) and I was surprised from the result. In my case "bigger" was slower. I was amazed, according to the simple logic in the case of less then or equal we need one or two operation(for example we first check for equality and the for then for "is lower"), so I asked at stackoverflow what causes this and here is the answer.

The speed of the operation does not depends from the operation itself but from the result. When the result is false it is calculated faster the if it is true. You can see the disassembler trace here.

Have you found other operations when python acts "strange"?