On Thu, Jun 2, 2016 at 1:06 PM, Jeroen Demeyer <jdemeyer@cage.ugent.be> wrote:
On 2016-06-02 21:12, Robert Bradshaw wrote:
"if isinstance(x, (int,long))", which is parsed as "if PyInt_Check(x) or PyLong_Check(x)" is slightly slower than "if PyInt_Check(x) || PyLong_Check(x)".
That is very surprising. How much slower?
With GCC 4.9.3, roughly 4% for a simple loop like
for x in L: if isinstance(x, (int, long)): return True
I wonder if this is due to gcc's expectations about the likelyhood of truth values of the subexpressions in a logical or vs. an if...
I doubt it. It's just the optimizer which optimizes arithmetic better than if/goto.
I'm able to replicate the 4% slowdown in that example. However, if I replace L with a list of ints (rather than a list of non-ints) and change the return to an integer increment the isinstance version goes to 4% faster than the || version, so I'd call this microbenchmark a wash.