Others noted that readability (as a rule) is more important and that you really don't need it if an example is provided. In addition, there is an old saw: "Premature optimization is the root of all evil . "
However, the best way to find out performance is to check. So, I put your two examples in a function (changing range[0,1023] to range(0, 1023) to make everything work) called the first example “no_not” and the second example “with_not” and created a simple object for testing with testit module :
def main(): global outfile outfile = open('test.dat', 'wt') num_tests = 10000 without_timer = timeit.Timer(stmt=without_not) with_timer = timeit.Timer(stmt=with_not) print 'without result: ', without_timer.timeit(number=num_tests) print 'with result: ', with_timer.timeit(number=num_tests) outfile.close()
Then I did some tests. Like me and the other responders, as expected, the version with not went a bit - about 0.6% - faster in each test; not enough to worry about IMO. (Well, there may be times when it matters, but if that were the case, I would recommend C or some other compiled language.)
Greenmatt
source share