The difference between performance or style between "if" and "if not"?

Is there a performance difference or style preference between the two ways of writing if statements? This is basically the same, condition 1 will be fulfilled only once, and another condition will be fulfilled at another time. If a condition that is satisfied only once is the first or second? Does the difference in performance matter? I prefer the 1st method if the performance is the same.

data = range[0,1023] length = len(data) max_chunk = 10 for offset in xrange(0,length,max_chunk): chunk = min(max_chunk,length-offset) if chunk < max_chunk: write_data(data[offset:]) else: write_data(data[offset:offset+max_chunk]) 

against

 data = range[0,1023] length = len(data) max_chunk = 10 for offset in xrange(0,length,max_chunk): chunk = min(max_chunk,length-offset) if not chunk < max_chunk: write_data(data[offset:offset+max_chunk]) else: write_data(data[offset:]) 
+7
source share
7 answers

In your example, if is not required at all:

 data = range[0,1023] length = len(data) max_chunk = 10 for offset in xrange(0,length,max_chunk): write_data(data[offset:offset+max_chunk]) # It works correctly 

I think this is the most effective way in your case.

+9
source

Well, try:

 x = np.random.rand(100) def f(x): z = 0 for i in x: if i < 0.5: z += 1 else: z += 0 return z def g(x): z = 0 for i in x: if not (i < 0.5): z += 0 else: z += 1 return z 

We get:

 %timeit f(x) 10000 loops, best of 3: 141 us per loop %timeit g(x) 10000 loops, best of 3: 140 us per loop 

No, there are not many differences. Even with large x, the differences are minimal.

I have to say that I'm a little surprised, I would expect the direct version ( f ) to be slightly more efficient than the not ( g ) version.

Moral: do as you want.

+7
source

I think READABILITY is more important than the performance advantage, if any, although I don't think there are any differences.

So, use the first method because it is easier to understand.

+5
source

There should be no difference in performance (if at all, I think that the second will be less productive), but just use the one that will become clearer to you. I also like the first one. :)

If you find that it matters later, then go and change it, but you know what they say: premature optimization is the root of all evil.

+2
source

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.)

+2
source

I cannot verify this, but using common sense, I think the first one will be a little better. The second method evaluates <and then evaluates it. This is another operation. And he does it every time. Plus intuitively the first makes more sense.

0
source

if there is a difference in performance? yes, the processor should make a predictive leap in case of if-else statements. will you notice the difference? never. your compiler / optimizer will put your code in reverse order before execution. your calculations will probably not take several thousand years to make a difference. you probably don’t even use a real-time system.

as others said: focus on readability, not optimize non-essential parts

0
source

All Articles