Question

My programming fundamentals teacher had said in one of her classes that using the "break" or "continue" keywords is less efficient then using a boolean to exit a loop.

I wrote and ran a program written in Python to see if this was the case:

import time;

TIMES = 100000000
COMPARE_FROM = "foo"
COMPARE_TO = "roo"

def breakTest() :
    while(True) :
        break;
        if(COMPARE_FROM == COMPARE_TO) :
            boo = "boo"

def booleanTest() :
    running = True;
    while(running) :
        running = False;
        if(running):
            if(COMPARE_FROM == COMPARE_TO) :
                boo = "boo"

def main() :
    breakTimeBefore = 0;
    breakTimeAfter = 0;
    booleanTimeBefore = 0;
    booleanTimeAfter = 0;

    print("running break test ...");

    breakTimeBefore = time.time();
    for i in range(0, TIMES, 1):
        breakTest();
    breakTimeAfter = time.time();

    print("break test complete");
    print("Time: %f seconds \n" % (breakTimeAfter - breakTimeBefore));

    print("running boolean test ...");

    booleanTimeBefore = time.time();
    for i in range(0, TIMES, 1):
        booleanTest();
    booleanTimeAfter = time.time();

    print("boolean test complete");
    print("Time: %f seconds \n" % (booleanTimeAfter - booleanTimeBefore))

    print("---- FINDINGS ----");
    print("breakTest time:   %f" % (breakTimeAfter - breakTimeBefore));
    print("booleanTest time: %f" % (booleanTimeAfter - booleanTimeBefore));
    print("diffrence:        %f" % ((breakTimeAfter - breakTimeBefore) - (booleanTimeAfter - booleanTimeBefore)));
    input("Press enter to close...");

main();

After running it three times, and averaging the results, i found that the breakTest was 6.25 seconds faster.

So is the break keyword more efficient or is my code wrong?

Was it helpful?

Solution

Your tests are not exactly equivalent. I think your teacher may have had something more like the following in mind:

def breakTest():
    i = 0
    while True:
        i += 1
        if i == 10:
            break

def booleanTest():
    i = 0
    while i < 10:
        i += 1

As you can see below, putting the condition into the while statement instead of having an if/break does improve performance (and shorten code):

In [14]: %timeit breakTest()
1000000 loops, best of 3: 999 ns per loop

In [15]: %timeit booleanTest()
1000000 loops, best of 3: 201 ns per loop

Note that this is just an example of the different ways to exit the loop and what I think your teacher meant. Of course if you were actually writing this code, you should use for i in range(11): ...

OTHER TIPS

Intro computer programming courses usually teach you things like this as truisms instead of best practices, because it tends to be hard to convince people with little experience that best practices are worth the effort. You should avoid using break and continue when you can, but they satisfy legitimate usecases that can't be managed, or in the least can't be managed cleanly, using other mechanisms. For the things you do when first learning programming, leaning on things like break and continue tend to be lazy, messy solutions, so you should try to avoid them. By the time you legitimately need them, you should have enough knowledge and experience behind you to use your own best judgement.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top