-
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?
-
Show your code. It's hard to optimise something you can't see.
-
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...
-
There is no reason those comparisons should take a long time. You could do hundreds of those without a noticable delay.
-
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.
-
Can you give an example? I don't know exactly what you mean with COMMAND_ENUM
-
>>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.
-
>> 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).
-
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.
-
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.