C++ Logo

STD-DISCUSSION

Advanced search

Subject: is `\n` replacement of `std::endl`
From: praveer kumar (yadav.praveer.kumar_at_[hidden])
Date: 2019-07-25 20:57:24


HI Experts,
Below is my sample code. I can see there is different time duration
captured to process the same data by using "\n", '\n' and std::endl.
below is my sample code.
I want to know is it OK to use '\n' all the time ?

#include <fstream>
#include <iostream>

#include <chrono>

// check performance among std::endl, "\n", '\n'

template<typename T>
auto _writeDataIntoFile( std::ofstream& of, const T& _data) {
auto startTime = std::chrono::steady_clock::now();
for (size_t i = 0; i < 1000; i++)
{
//of << _data << "\n";
//of << _data << '\n';
of << _data << std::endl;
}
auto endTime = std::chrono::steady_clock::now();
auto timeElapsed = endTime - startTime;
return timeElapsed.count();
}

int main()
{
std::ofstream outputFile("temp.txt", std::ios_base::trunc );
auto duration = _writeDataIntoFile(outputFile, "String to be printed");
std::cout << " Time taken = " << duration;

return 0;
}



STD-DISCUSSION list run by herb.sutter at gmail.com

Older Archives on Google Groups