I notice that the output on the Window's Command Prompt is much slower than outputting on Bash. I'm talking about the Command Prompt taking about 10 seconds to print 40000 lines, while Bash takes less than a tenth of a second. Case in point, the following code fills a stack with integers from 1 to 40000 and then prints it to stdout and to a text file:
#include <iostream>
#include <chrono>
#include <fstream>
template <typename T>
struct Node {
Node(T data, Node *next = nullptr) {
this->data = data;
this->next = next;
}
T get_data() { return data; }
Node * get_next() { return next; }
void set_data(T data) { this->data = data; }
void set_next(Node *next) { this->next = next; }
private:
T data;
Node *next;
};
template <typename T>
class Stack {
public:
Stack() { head = nullptr; }
void push(T val) { head = new Node<T>(val, head); }
void print(std::ostream &os = std::cout) {
Node<T> *it = head;
while (it) {
os << it->get_data() << "\n";
it = it->get_next();
}
}
private:
Node<T> *head;
};
int main() {
Stack<int> s;
std::chrono::high_resolution_clock::time_point starttime = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 40000; i++) {
s.push(i + 1);
}
s.print();
std::chrono::high_resolution_clock::time_point endtime = std::chrono::high_resolution_clock::now();
std::cout << "Time taken to print to stdout: " << std::chrono::duration_cast<std::chrono::duration<double>>(endtime - starttime).count() << " seconds.\n";
std::cout << "Writing to file...";
starttime = std::chrono::high_resolution_clock::now();
std::ofstream ofs("output.txt");
s.print(ofs);
endtime = std::chrono::high_resolution_clock::now();
std::cout << " Done\nTime taken to print to file output.txt: " << std::chrono::duration_cast<std::chrono::duration<double>>(endtime - starttime).count() << " seconds.\n";
return 0;
}
On Windows,the print to stdout takes anywhere from 6 to 10 seconds, whereas writing to a file takes about 2 milliseconds. On Linux, printing to terminal takes one tenth of a second, whereas printing to a file is about on par as on Windows. Is there a way to improve CMD's output speed?