+ 2
What is the difference between \n and std::endl ?
is there any different performance advantage between \n and std::endl? if i want to output a large file like a briefing page through network , which one better?
6 Respostas
+ 5
@Setiawan Next
Here is a test I just did, the results were:
Test A time in ms: 790
Test B time in ms: 124
So in conclusion, \n really is much faster.
#include <fstream>
#include <ctime>
#include <iostream>
void funcA(unsigned len)
{
std::ofstream file("testA.txt");
time_t start = clock();
for (unsigned i = 0; i < len; i++)
{
file << "aaa" << std::endl;
}
std::cout << "Test A time in ms: " << clock() - start << "\n";
}
void funcB(unsigned len)
{
std::ofstream file("testB.txt");
time_t start = clock();
for (unsigned i = 0; i < len; i++)
{
file << "aaa" << "\n";
}
std::cout << "Test B time in ms: " << clock() - start << "\n";
}
int main()
{
unsigned len = 100000;
funcA(len);
funcB(len);
}
+ 10
@Ulisses. Nice link. Provides additional info ontop of the answer provided aklex
@Setiawan: Is this another question from your teacher? If so I feel he is slowly becoming mine also! Keep up the questions!
+ 8
@aklex: wow. not even apples and oranges.. more like apples and bricks.. didnt think it would make that much difference.
+ 5
Endl flushes the stream and starts a new line
\n just starts a new line
Use \n unlesss you need to flush as it's much faster
+ 2
@aklex
Thank You very much, i know \n is faster but i can not prove it. Your explanation is really helpful.
@jay
no, i figure it out by myself. my teacher give me one quizz each time he meet me. and i don't meet him today, not yet.