Thread: the speed of ? operator vs. the if statement

  1. #1
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743

    the speed of ? operator vs. the if statement

    Is there any difference between the speed of the ? operator and the if statement? ex:

    Code:
    int myint;
    cin >> myint;
    (myint == 5) ? cout << 'a' : cout << 'b';
    vs.

    Code:
    int myint;
    cin >> myint;
    if(myint == 5) cout << 'a';
    else cout << 'b';
    so is there any speed difference? is the ? operator any faster?
    My Website

    "Circular logic is good because it is."

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >is the ? operator any faster?
    Yes, the ternary ?: operator can be faster. But since a decent compiler will optimize away the gains of ?: over if..else you should only use the ternary operator when there is a signifigant gain in readability.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    375
    Of course, the true power of ? : is the fact that it is an operator. It can be evaluated by the "true" expression. For instance:

    PHP Code:
    printf("You have %d item%s.\n"nn=="" "s"); 
    is equivalent to:

    PHP Code:
    printf("You have %d item"n);
    if(
    n>1)
     {
     
    printf("s");
     }
    printf(".\n"); 
    You've saved 3 lines. There are really a lot of instances where treating it as an operator can really save your beacon.

    -Justin
    Allegro precompiled Installer for Dev-C++, MSVC, and Borland: http://galileo.spaceports.com/~springs/

  4. #4
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Originally posted by Justin W


    PHP Code:
    printf("You have %d item%s.\n"nn=="" "s"); 
    is equivalent to:

    PHP Code:
    printf("You have %d item"n);
    if(
    n>1)
     {
     
    printf("s");
     }
    printf(".\n"); 
    You've saved 3 lines. There are really a lot of instances where treating it as an operator can really save your beacon.

    -Justin
    You could do:
    Code:
    if(n>1)  printf("s\n");

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    375
    [possible misunderstanding]Not if you want a new line (and period) after either one, but not two for any given one.[/pm]

    Unless you mean I didn't need the braces, which I didn't but just did 'cause I didn't want to step on most people's style. Save two or three lines depending upon if you use:
    if(...)
    statement

    or
    if(...) statement.

    Last edited by Justin W; 03-05-2002 at 02:41 PM.
    Allegro precompiled Installer for Dev-C++, MSVC, and Borland: http://galileo.spaceports.com/~springs/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I am very new . . . :(
    By Eternalglory47 in forum C++ Programming
    Replies: 6
    Last Post: 09-05-2008, 11:29 AM
  2. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  3. Flight Simulator Wind Speed!!
    By Dilmerv in forum C++ Programming
    Replies: 6
    Last Post: 03-20-2006, 12:40 AM
  4. Replies: 6
    Last Post: 01-08-2006, 02:49 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM