I am currently writing a simulation in C++ (on OSX). The biggest time effort runs into storing the data, where the output is a table of double pairs t and f(t).
Until now I used the (simplified) code:
ofstream ofs;
ofs.open("test");
for(int i = 0; i < 4e7; i++){
ofs << i;
}
ofs.close();
Since everthing is stored in 1's and 0's my guess was, that using the binary format would be more time efficient. But the follwing change (ios::binary) did not improve the computing time:
ofstream ofs;
ofs.open("test", ios::binary);
for(int i = 0; i < 4e7; i++){
ofs << i;
}
ofs.close();
Was my guess wrong or do I need to add something?
No, not really.
std::cout
is linked to the console, and the speed of this is mostly OS dependent.That being said, there are a few simple tricks to speed up console output:
<<
call. This has improved the speed at which my output landed in the Windows console a lot.sync_with_stdio
.