microsoft / microsoft/STL

<iostream>: Bad performance of wcout operator<< compared to wprintf

Open
#605 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

If I compile the following code:

for(auto idx = 0; idx < 10; idx++){
        const wchar_t* a = L"this is a test\n";      
        std::wcout << a;
}

it needs round about 18 milliseconds during runtime.

But if I use C functions like:

for(auto idx = 0; idx < 10; idx++){
        const wchar_t* a = L"this is a test\n";      
        wprintf(L"%s", a);
}

It needs only 5 milliseconds!
Why is std::cout / wcout so slow? I would assume that the C function should be slower because it has to analyze the format string, but...

I tried to optimize it by calling first: std::ios::sync_with_stdio(false);
But this seems to be completely ignored (I found no reference to _Sync...)?

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

Start by reproducing the two provided wcout and wprintf loops, including the std::ios::sync_with_stdio(false) call. Trace the iostream/wcout implementation and the sync_with_stdio path to identify whether the reported difference is reproducible and what behavior, if any, should be changed; done means a confirmed diagnosis with a focused regression test or a documented reason no code change is needed.

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
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.