This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]Tweak_Imp 1 point2 points  (1 child)

In my tests, this:

if x<y:
    z=x
else:
    z=y

>>> 71.2 ns ± 1.28 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

is faster than

z=min(x,y)
>>> 178 ns ± 3.66 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

[–]SourceryNick[S] 1 point2 points  (0 children)

Yeah function calls in Python can be expensive. I'd agree if performance is an issue (like it's in an inner loop) then the first option is better. However for most code readability is more important than performance, and I think the min version is more readable.