ss41153
ss41153

Reputation: 71

Performance or style difference between "if" and "if not"?

Is there a performance difference or style preference between these two ways of writing if statements? It is basically the same thing, the 1 condition will be met only once while the other condition will be met every other time. Should the condition that is met only once be first or second? Does it make a difference performance wise? I prefer the 1st way if the 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])

vs

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:])

Upvotes: 7

Views: 884

Answers (7)

piotrek
piotrek

Reputation: 14520

if there will be a performance difference? yes, processor have to do a predictive jump in case of if-else statements. will you notice the difference? no way. your compiler/optimizator will put your code upside-down before execution. your computation probably won't take a few thousands years to make it matters. you probably even don't use real time system.

as others said: focus on readability, don't preoptimize unimportant parts

Upvotes: 0

GreenMatt
GreenMatt

Reputation: 18570

Others have noted that readability is (usually) more important and that you don't really need the if the way the example is presented. Also, there's the old saw: "Premature optimization is the root of all evil".

That said, the best way to find out the performance is to test. So, I put your two examples into functions (changing range[0,1023] to range(0, 1023) to make things work) named the first example 'without_not' and the second example 'with_not', and created a simple main to test using the 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 ran several tests. As I, and other answerers, expected, the version with out the not ran a little bit - about 0.6% - faster in each test; not enough to worry about, IMO. (Well there may be some cases where it matters, but if that were the case I'd recommend C or some other compiled language.)

Upvotes: 2

Pierre GM
Pierre GM

Reputation: 20339

Well, let's 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

Nope, not a lot of differences here. Even with a larger x, the differences are minimal.

I must say I'm a bit surprised, I'd have expected the direct version (f) to be slightly more efficient than the not version (g).

Moral: do as you like.

Upvotes: 7

defuz
defuz

Reputation: 27581

In your example, if isn't needed 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 efficient way in your case.

Upvotes: 9

alanmanderson
alanmanderson

Reputation: 8200

I can't verify this, but using common sense, I would think the first one would perform slightly better. The second one is evaluating the < and then it evaluates the not. That is one additional operation. And it does it every time. Plus intuitively the first makes more sense.

Upvotes: 0

charlee
charlee

Reputation: 1369

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

So use the 1st way, because it is easier to understand.

Upvotes: 5

Ry-
Ry-

Reputation: 224857

There shouldn't be any performance difference (if at all, I think the second would be less performant), but just use the one that's clearer to you. I like the first one better too. :)

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

Upvotes: 2

Related Questions