+ 1

Large switch case statement optimisation question.

Hello! Beginner C++ programmer here. While creating a program which turns alphanumerical input into big ASCII art characters, I made a desition which I'm not sure of if it is good practise to do. Basically, I made a huge switch case statement that loops through every character in the user input (max 24) times. The switch case statement is about 60 cases long. My question is, if it is terribly inefficient to have that big of a switch case. Does the compiler (gcc) have some crazy way to optimise it, or will the compiled program be way too large to make this a reasonable solution? If it is bad practise, can you think of an alternative and tell me? Also, if the compiler does some cool stuff, I would like to read about it if you can link it to me :P. Thanks in advance for helping me out!

17th Feb 2019, 1:04 PM
Roina
Roina - avatar
5 Respostas
+ 3
Just another approach to do so. Not quiet sure what do you mean by optimization here but it can be achieved by some other bizarre method as well! #include <iostream> #include <string> #include <map> using namespace std; #define ART_DIM 5 typedef map<char, string[ART_DIM]> patterns; int main() { patterns lookup = { // { input character [0-9][a-z][A-Z], big art! } {'0', { " ### ", " # ## ", " # # # ", " ## # ", " ### " } }, {'1', { " ## ", " # ", " # ", " # ", " ### " } }, // ... {'a', { " #### ", " # # ", " # # ", " # # ", " ##### " } }, // ... {'i', { " # ", " ", " # ", " # ", " # " } }, // ... {'n', { " # ", " ### ", " # # ", " # # ", " # # " } }, {'o', { " ### ", " # # ", " # # ", " # # ", " ### " } }, {'R', { " #### ", " # # ", " #### ", " # # ", " # # " } }, // ... }; string word = ""; cout << "Word is: "; cin >> word; int row = 0; while ( row < ART_DIM ) { for (size_t i = 0; i < word.length(); ++i) { patterns::iterator current_char = lookup.find(word[i]); if ( current_char != lookup.end() ) cout << current_char->second[row]; } ++row; cout << endl; } } Live demo: http://cpp.sh/7styz
17th Feb 2019, 4:36 PM
Babak
Babak - avatar
+ 3
Oh nice solution there! Thanks. What I mean by optimization is just to know if a large switch statement is somehow bad practise and not really a good solution to this problem and might result to some unwanted behaviour like slowness. Dunno, should I even worry about well optimized code in an application as small as this? I'm just interested. Thats all. Thanks for your answer and that creative solution. :)
17th Feb 2019, 6:28 PM
Roina
Roina - avatar
+ 3
Despite being archaic and having a lengthy conditional block, there's no drawback in using switch...case statement that I know of. As far as the code readability is concerned, the semantic of picking one item from a tagged list of cases is quiet simple to follow by reader. Also in terms of speed, there wouldn't be any performance penalty even in longer lists since comparing operations are considered cheap compare to other basic operations. Finally, every language constructs will cause issue in some particular cases if get used without much thought and care! 8)
17th Feb 2019, 7:11 PM
Babak
Babak - avatar
17th Feb 2019, 6:27 PM
unChabon
unChabon - avatar
+ 1
Alright. So I guess the huge switch case statement in this case is a good solution. Gets the job done and isn't too inefficient. You have tought me something about assembly too. Thanks for going through all the trouble with me. Have a good day! :)
17th Feb 2019, 11:28 PM
Roina
Roina - avatar