I saw this users post yesterday. And I thought that was a cool way to output a vector. So I typed up an example and asked myself how does this compare to a for each
loop?
template <typename T>
void printVectorO(std::vector<T> &v)
{
std::cout << "Ostream_iterator contents: " << std::endl;
auto start = std::chrono::high_resolution_clock::now();
std::ostream_iterator<T> ost(std::cout, " ");
std::copy(begin(v), end(v), ost);
std::cout << std::endl;
auto end = std::chrono::high_resolution_clock::now();
auto time = end - start;
auto nano = std::chrono::duration_cast<std::chrono::nanoseconds>(time);
std::cout << "Ostream_iterator computation took: " << nano.count() << " nano seconds"<< std::endl;
std::cout << std::endl;
}
template <typename T>
void printVectorC(std::vector<T> &v)
{
std::cout << "For Each Loop contents: " << std::endl;
auto start = std::chrono::high_resolution_clock::now();
for (auto && e : v) std::cout << e << " ";
std::cout << std::endl;
auto end = std::chrono::high_resolution_clock::now();
auto time = end - start;
auto nano = std::chrono::duration_cast<std::chrono::nanoseconds>(time);
std::cout << "For Each Loop took: " << nano.count() << " nano seconds" << std::endl;
std::cout << std::endl;
}
I used 3 vectors to test this:
std::vector<double> doubles = { 3.15, 2.17, 2.555, 2.014 };
std::vector<std::string> strings = { "Hi", "how", "are", "you" };
std::vector<int> ints = { 3, 2 , 2 , 2 };
And I get various results. The for each
loop always beats the ostream_iterator
when I output the doubles (ex 41856 vs 11207 and 55198 vs 10308 nanoseconds). Sometimes the string ostream_iterator
beats out the for each
loop, and the for each
loop and ostream_iterator
almost stay neck and neck with integers.
Why is this? What is going on behind the scenes of ostream_iterator
? When would I use ostream_iterator
over a for each
loop when it comes to efficiency and speed?