Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8980693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:09:04+00:00 2026-06-15T20:09:04+00:00

My programming fundamentals teacher had said in one of her classes that using the

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:09:05+00:00Added an answer on June 15, 2026 at 8:09 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just had a 'Fundamentals of Programming' lecture at uni and was told that the
Recently a teacher said PHP isn't a real programming language, but only gave, in
I took my first 'fundamentals of programming' lab session at uni today. One thing
I am currently taking a Programming Fundamentals college class that deals with the C
I'm going through some lecture notes, Fundamentals of Mathematica Programming (see the .nb file
Programming language: Java Ok, so I want to have a BufferedImage that keeps rotating
Programming language books usually explain that value types are created on the stack, and
I give lessons on the fundamentals of the Java programming language, to students who
For a functional programming assignment, I am writing a scheme macro that translates scheme
While doing concurrent programming I need to tell the compiler/optimizer that it may not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.