slow if-else

This is a discussion on slow if-else within the C++ Programming forums, part of the General Programming Boards category; I made a source code it asks for a command, I made a lot of different command enz. It stores ...

  1. #1
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312

    slow if-else

    I made a source code it asks for a command, I made a lot of different command enz.

    It stores the command input in the string "command". I check the value with a lot of if-else statements. Because I have a lot of commands, It works slower. Does anyone have an idea to improve that?
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Show your code. It's hard to optimise something you can't see.
    Sent from my iPadŽ

  3. #3
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Code:
        if (command == "AVGscan")
        {
          cout << "Starting Anti-virus program" << endl;
          char path[] = "C:\\Program Files\\Grisoft\\AVG Free\\avgw.exe";
          char par[] = "";
          char dir[] = "";
          execute(path, par, dir);
        }
        else if (command == "abort")
        {
          cout << "Stopping applications..." << endl;
          break;
        }
        else if (command == "info")
        {
          system("cls");
          info();}
        etc...
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    There is no reason those comparisons should take a long time. You could do hundreds of those without a noticable delay.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Even though there is a distinct possibility that those aren't actually slowing your program down, one way to speed it up would be to put the commands in a map. The key would be the command string and the value could be an enum that you then use in a switch statement, or a function pointer, or a class object that knows what to do. The fastest map would be unordered_map (i.e. hash_map).

    The simplest solution that would still be faster than if-else is std::map<std::string, COMMAND_ENUM> and a switch statement where you define an enum type COMMAND_ENUM with an entry for each command.

  6. #6
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Can you give an example? I don't know exactly what you mean with COMMAND_ENUM
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    >>that would still be faster than if-else is <<

    how is that? maybe nicer on the eyes, but map class still has to make string comparisons in order to find the value. So it has saved nothing, and actually slows it down a tad.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> Can you give an example?
    http://cboard.cprogramming.com/showt...203#post537203

    >> So it has saved nothing, and actually slows it down a tad.
    Actually, it is faster algorithmically. An if-else if series is a linear search O(n), where each string must be compared to the input until it is found. Using the map will make it O(log n). Using an unordered_map would make it O(1).

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    but ... the map still has to make the comparisons, unless it can use some kind of binary search algorithm and in that case yes it will be considerably faster. An unordered map would be just as time consuming as a series of if-then-else statements except the map will use a loop to sequentially search the strings.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    You don't loop throughthe map to find the element, you use the appropriate lookup methods.

    Sure, there will be string comparisons. The difference is in how many are done. It is is the same as if you are comparing integers. The if-else if is like a linked list. You compare each element one at a time. On average you will make n/2 comparisons, which means O(n), worst case is n comparisons. The std::map is generally implemented as a binary tree, which means at most you make log2 n comparisons, and the standard guarantees O(log n). Finally, the unordered_map is based on a hash table. Assuming a good hash function and knowledge of the number of elements from the start (which should be the case here), lookup is constant because all you have to do is hash the string and at worst compare against a couple of strings that collided with the same hash value.

    This is what maps are for... faster lookup. If you only have a couple of commands, then it won't matter because the overhead will outweigh the algorithmic improvements, but if the OP really is having a slowdown because of all the string compares, then this is the solution.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Slow startup
    By Desolation in forum Tech Board
    Replies: 38
    Last Post: 12-27-2008, 06:54 AM
  2. Can someone clarify what a 'slow event' is.
    By Overworked_PhD in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-13-2008, 04:11 PM
  3. Program is slow. Help
    By jjj93421 in forum C++ Programming
    Replies: 14
    Last Post: 08-08-2004, 07:39 AM
  4. slow game
    By lambs4 in forum Game Programming
    Replies: 2
    Last Post: 08-21-2003, 02:08 PM
  5. Solutions for slow performance?
    By Hunter2 in forum Game Programming
    Replies: 52
    Last Post: 08-24-2002, 10:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21