Marshma11ow
Marshma11ow

Reputation: 101

Is it more effecient to use std::cout once rather than multiple times to display the same amount of data?

For example,

Would it be more memory efficient to display these variables like this:

std::cout << "First char is " << char1 << " and second char is " << char2;

rather than this:

std::cout << "First char is " << char1;
std::cout << " and second char is " << char2;

Of course, I'm not literally worried about two lines of code.. But I'm trying to learn to write code more efficiently

Thank you

Upvotes: 5

Views: 528

Answers (2)

Pubby
Pubby

Reputation: 53047

Having it be a single statement could theoretically be faster as the compiler can rearrange the order of argument evaluation more freely. However, this is talking about 0.00000000000001% difference and is pointless. Don't care about this - the bottleneck is in the console itself.

Anyway, column alignment is really helpful for readability and so try this:

std::cout <<       "First char is " << char1;
std::cout << " and second char is " << char2;

Or this:

std::cout <<       "First char is " << char1
          << " and second char is " << char2;

(I prefer the first because I find it easier to format in my text editor).

Upvotes: 3

imulsion
imulsion

Reputation: 9040

It makes no difference with programs that small. The toss-up comes with readability. Do u want it quick and easy or more readable?

Upvotes: 0

Related Questions