microsoft / microsoft/STL

std::format with std::chrono poor performance

Open
#3,575 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

chrono format performance
Dominant language
C++
Stars
11.2k
Forks
1.7k
Avg merge
4d 15h
Merged PRs (30d)
22

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source file or test is named. Start by reproducing the two formatting approaches with the shown std::chrono::system_clock::duration input and compare their performance; done means identifying the source of the slowdown and establishing whether a library change or documented limitation is appropriate.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.