I'm sorry, I realize now that the question was very unclear. Bad example, but consider this:
for (int i = 0; i < 100; ++i) cout << "output";
or
1 2 3 4
|
cout << "output";
cout << "output";
cout << "output";
//etc..
| |
First code will check a condition 100-times and don't forget about allocating a memory for the variable and incrementing the variable 100-times. Second code will give you a headache, but the program won't have to spent time doing all the extra stuff.
And this is a really bad example, but I hope you know a little bit better what I mean.
EDIT: Maybe a better example... Let's say you have a window form with multiple buttons. All button clicks do almost the same action. An example: Button A does a mix of X1 and X2, button B does a mix of X1 and X3, button C does a mix of X2 and X3.
You can send all button clicks to the same function but with a different argument value and write a lot of conditional statements in the function. The code will be short because you can group various button clicks into same blocks of conditional statements and it will be quick to write. X1, X2, X3, X4 etc - all in the same function.
or
You can write a separate function for each button click, even though they do almost the same thing. BUT, the program won't have to check which button was clicked. Function for button A will contain action X1-X2, function for button B X1-X3 and so on...
The obvious answer would be to put all actions (X1, X2, X3, X4, etc) in their own function. But I'm talking about a "mix" of actions. So the only thing left to do (if you want to separate the actions for each button) is to write a separate function for each button, thus repeating almost the same code over and over again.
I'm hoping for an "compilers optimize your code enough so that it doesn't really matter much" answer, because sometimes it's easier to write a lot of conditionals and sometimes it's easier to separate everything.