Skip to content

std::format with std::chrono poor performance #3575

@nuuSolutions

Description

@nuuSolutions

Is this a known issue or am I doing something sub-optimal?

I'm simply calling

std::format_to( std::back_inserter( buffer ), "[{:%H:%M:%S}] ", time );

where buffer is a std::string and time is of type std::chrono::system_clock::duration
and this is about 4-5 times slower than 'rolling my own' e.g.

static constexpr auto h = 3600'000'000'000LL;
static constexpr auto min = 60'000'000'000LL;
static constexpr double sec = 1'000'000'000;
auto elapsed_ns = time.count();
const auto hours = elapsed_ns / h; elapsed_ns -= hours * h;
const auto minutes = elapsed_ns / min; elapsed_ns -= minutes * min;
const double seconds = elapsed_ns / sec;

std::format_to( std::back_inserter( buffer ), "[{}:{:02}:{:010.7f}] ", hours, minutes, seconds );

Not sure where this huge difference can come from. Allocations?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions