|
-
Dec 5th, 2002, 11:36 AM
#1
Thread Starter
<?="Moderator"?>
which is more effective?
which method is more effective, or faster, well better.
Code:
cout<<"line 1"<<endl<<"line 2";
or
Code:
cout<<"line 1\nline 2";
-
Dec 5th, 2002, 11:52 AM
#2
Monday Morning Lunatic
The second is faster.
Not only is it less function calls, it also has the newline embedded.
The main kicker, though, is the non-use of endl. That actually outputs a newline, then flushes the stream.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Dec 5th, 2002, 04:15 PM
#3
Therefore the second makes more efficient use of the buffer.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 5th, 2002, 11:27 PM
#4
Guru
...But they slightly differ in their effect.
Try putting a getch(); after each one (assuming you use VC++) and testing them separately.
-
Dec 6th, 2002, 06:05 AM
#5
They do, but if one of the effects is unwanted then the other most probably is too. And you shouldn't rely on that.
Anyway:
cout<<"line 1"<<endl<<"line 2"<<endl;
and
cout<<"line 1\nline 2"<<endl;
have exactly the same effect with the second being much better.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 6th, 2002, 02:18 PM
#6
Guru
Or just call cout.flush(); if you don't want the '\n' to be printed.
Code:
cout << endl;
// is the same as
cout << '\n';
cout.flush();
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|