Skip to content

Instantly share code, notes, and snippets.

@fsb4000

fsb4000/test.cpp Secret

Created Jul 19, 2021
Embed
What would you like to do?
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