<iostream>: Bad performance of wcout operator<< compared to wprintf
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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