Thanks for the reply. I’m not worried it will become a bottleneck in development. I simply want to learn the “best” way to write code since I’m learning from scratch. While I don’t think this difference would make a significant impact on the overall performance, I suppose it would make a difference if I write various bits of code less effective than it could be. Maybe I’m worrying about completely unnecessary stuff? Like optimizing the carrying load of some ants while I have elephants contributing aswell?
Since I’m just starting out to code I struggle with how to code the test you recommend. I tried by googling, but I don’t know if it’s a good way of measuring. Basically checking clock() before and after each loop block.
#include <iostream>
#include <ctime>
using namespace std;
int main() {
constexpr int WORD_LENGHT = 5;
clock_t start1, end1, start2, end2;
start1 = clock();
for (int i = 0; i < 50000; i++)
{
cout << "Welcome to Bulls and Cows!\n";
cout << "Can you get the " << WORD_LENGHT;
cout << " letter I'm thinking of?\n";
}
end1 = clock();
start2 = clock();
for (int i = 0; i < 50000; i++)
{
cout << "Welcome to Bulls and Cows!\n"
<< "Can you get the " << WORD_LENGHT
<< " letter I'm thinking of?\n";
}
end2 = clock();
cout << "\n\n\nThe interval was: ";
cout << (double)(end1 - start1) / (double)CLOCKS_PER_SEC;
cout << " seconds for multiple cout statements and ";
cout << (double)(end2 - start2) / (double)CLOCKS_PER_SEC;
cout << " seconds for a single cout statement." << endl;
return 0;
}
I’m using Windows by the way, and I suppose the suggestion of sending the output to /dev/null is a GNU/Linux privilege? I suppose I have to settle with the content being printed 100,000 times.
According to the results of the 50,000 iterations of the two different code variations, using a single cout statement could be better performance wise (143.299 seconds vs. 137.343). However, I’m unsure of the legitimacy of the test, and it is according to this test only slightly faster. I suppose the results could be the reverse in a second test. I should probably focus on making the code look pretty and understandable.