Read files from iostream 3x-4x times slowly than libstdc++
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 11.1k
- Forks
- 1.7k
- Avg merge
- 4d 15h
- Merged PRs (30d)
- 22
Description
I just test read integers from text files by iostreams , cin/cout.
MSVC STL works very slowly than GCC llibstdc++ mingw version.
What I doing wrong?
Build type Release in both cases.
GCC - tested with CLion 2025.1 community edition
MSVC -- Visual Studio 2022 x64 Release .
Thanks.
My code:
#include <stdio.h>
#include <chrono>
#include <vector>
#include <iostream>
static int generate_file(int n, int q, const char* filename)
{
FILE* file = fopen(filename, "w");
if (file == nullptr)
{
fprintf(stderr, "Error opening file\n");
return -1;
}
fprintf(file,"%d\n", n);
for (int i = 1; i <= n; i ++)
{
fprintf(file, "%d ", i + (788787878));
}
fprintf(file, "%d\n", q);
for (int i = 1; i <= q; i++)
{
fprintf(file, "%d %d\n", i, i + (666777888));
}
fclose(file);
return 0;
}
static int read_scanf_printf(const char* filename_in, const char* filename_out)
{
freopen(filename_in, "r", stdin);
freopen(filename_out, "w", stdout);
int n;
scanf("%d", &n);
//std::cin >> n;
std::vector<int> a(n + 4);
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
// std::cin >> a[i];
}
int q;
scanf("%d", &q);
//std::cin >> q;
for (int i = 0; i < q; i++)
{
int p,x;
scanf("%d%d", &p, &x);
printf("%d\n", a[p] + x);
//std::cin >> p >> x;
//std::cout << a[p] << '\n';
}
fclose(stdin);
fclose(stdout);
return 0;
}
static int read_cout_cin(const char* filename_in, const char* filename_out)
{
freopen(filename_in, "r", stdin);
freopen(filename_out, "w", stdout);
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int n;
std::cin >> n;
std::vector<int> a(n + 4);
for (int i = 1; i <= n; i++)
{
std::cin >> a[i];
}
int q;
std::cin >> q;
for (int i = 0; i < q; i++)
{
int p,x;
std::cin >> p >> x;
std::cout << a[p] + x << '\n';
}
std::cout.flush();
fclose(stdin);
fclose(stdout);
return 0;
}
int main(int argc, char* argv[])
{
generate_file(100000, 100000, "input.txt");
{
auto start_time = std::chrono::system_clock::now();
read_scanf_printf("input.txt", "output.txt");
auto finish_time = std::chrono::system_clock::now();
auto elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(finish_time - start_time);
long elapsed_ld = static_cast<long>(elapsed_time.count());
fprintf(stderr, "printf/scanf. elapsed time: %ld ms\n", elapsed_ld);
}
{
auto start_time = std::chrono::system_clock::now();
read_cout_cin("input.txt", "output.txt");
auto finish_time = std::chrono::system_clock::now();
auto elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(finish_time - start_time);
long elapsed_ld = static_cast<long>(elapsed_time.count());
fprintf(stderr, "cout/cin. elapsed time: %ld ms\n", elapsed_ld);
}
#ifdef __GNUC__
std::cerr << "GCC: " << __GNUC__ << " " << __GNUC_MINOR__ << " " << __GNUC_PATCHLEVEL__ << "\n";
#endif
#ifdef _MSC_VER
std::cerr << "MSVC: " << _MSC_VER << "\n";
#endif
return 0;
}
/**
*
*
* printf/scanf. elapsed time: 154 ms
* cout/cin. elapsed time: 40 ms
* GCC: 13 1 0
*/
/*
printf/scanf. elapsed time: 56 ms
cout/cin. elapsed time: 130 ms
MSVC: 1944
*/
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 building the supplied program with Visual Studio 2022 in x64 Release mode and reproduce its timings for scanf/printf versus cin/cout. Compare the same program and inputs with the reported GCC results, checking the stream setup and measurement conditions shown in the issue. Done means explaining the performance difference or identifying a reproducible MSVC STL issue.
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
- 28/100