To check for odd and even integer, is the lowest bit checking more efficient than using the modulo?
>>> def isodd(num):
return num & 1 and True or False
>>> isodd(10)
False
>>> isodd(9)
True
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yep. The
timeitmodule in the standard library is how you check on those things. E.g:As you see, on my (first-day==old==slow;-) Macbook Air, the
&solution is repeatably between 7 and 18 nanoseconds faster than the%solution.timeitnot only tells you what’s faster, but by how much (just run the tests a few times), which usually shows how supremely UNimportant it is (do you really care about 10 nanoseconds’ difference, when the overhead of calling the function is around 400?!-)…Convincing programmers that micro-optimizations are essentially irrelevant has proven to be an impossible task — even though it’s been 35 years (over which computers have gotten orders of magnitude faster!) since Knuth wrote
which as he explained is a quote from an even older statement from Hoare. I guess everybody’s totally convinced that THEIR case falls in the remaining 3%!
So instead of endlessly repeating "it doesn’t matter", we (Tim Peters in particular deserves the honors there) put in the standard Python library module
timeit, that makes it trivially easy to measure such micro-benchmarks and thereby lets at least some programmers convince themselves that, hmmm, this case DOES fall in the 97% group!-)