Created
February 14, 2015 11:41
-
-
Save pranavcode/5b9631b4cac3b2d81954 to your computer and use it in GitHub Desktop.
C++ performance test - '\n' vs std::endl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Timing the performance | |
// Example: '\n' vs std::endl | |
// The winner is given at the end | |
#include <cmath> | |
#include <iostream> | |
#include <fstream> | |
#include <time.h> | |
constexpr long MAX = 19999999; | |
int main() { | |
std::ofstream f("test_time.dat"); | |
std::ofstream o("test_run.out"); | |
clock_t t1, t2, diff; | |
t1=clock(); | |
for(int i = 0; i < MAX; ++i) o << std::endl; | |
t2=clock(); | |
diff = ((float) t2 - (float) t1); | |
f << "std::endl : " << diff << std::endl; | |
t1 = clock(); | |
for(int i = 0; i < MAX; ++i) o << '\n'; | |
t2 = clock(); | |
diff = ((float) t2 - (float) t1); | |
f << "\'\\n\' : " << diff << '\n'; | |
f.close(); o.close(); | |
} | |
/* | |
Results: | |
* Run 1 : MAX = 199999 | |
std::endl : 266843 | |
'\n' : 8810 | |
* Run 2 : MAX = 1999999 | |
std::endl : 2504474 | |
'\n' : 67492 | |
* Run 3 : MAX = 19999999 | |
std::endl : 25223044 | |
'\n' : 679572 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment