-
-
Save fsb4000/7618e7ca2646cc1ef785d8ccf73e0858 to your computer and use it in GitHub Desktop.
cout vs fmt::print
This file contains 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
#define FMT_HEADER_ONLY | |
#include <cassert> | |
#include <iostream> | |
#include "fmt/core.h" | |
#include <Windows.h> | |
void useCout() { | |
for (unsigned i = 0; i < 4000; i++) { | |
std::cout << "cupcakes are made of flour, eggs, and sugar"; | |
} | |
std::cout.flush(); | |
} | |
void useFMT() { | |
for (unsigned i = 0; i < 4000; i++) { | |
fmt::print("cupcakes are made of flour, eggs, and sugar"); | |
} | |
} | |
static inline int64_t GetTicks() | |
{ | |
LARGE_INTEGER ticks; | |
bool res = QueryPerformanceCounter(&ticks); | |
assert(res); | |
return ticks.QuadPart; | |
} | |
int main() | |
{ | |
constexpr std::size_t size = 1024 * 8; | |
char buffer[size]; | |
std::cout.rdbuf()->pubsetbuf(buffer, size); | |
std::ios::sync_with_stdio(false); | |
const auto tick0 = GetTicks(); | |
useCout(); | |
const auto tick1 = GetTicks(); | |
useFMT(); | |
const auto tick2 = GetTicks(); | |
std::cout << "\n\ntime cout = " << tick1 - tick0 | |
<< "\ntime fmt = " << tick2 - tick1 | |
<< std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment