Skip to content

Instantly share code, notes, and snippets.

@fsb4000

fsb4000/test.cpp Secret

Created July 19, 2021 02:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fsb4000/7618e7ca2646cc1ef785d8ccf73e0858 to your computer and use it in GitHub Desktop.
Save fsb4000/7618e7ca2646cc1ef785d8ccf73e0858 to your computer and use it in GitHub Desktop.
cout vs fmt::print
#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