这是C ++中输入速度最快的方法 [英] Which is the fastest method of input in C++
问题描述
我正在编写各种编程奥林匹克运动,因此提高时间效率我寻找最快的方法获得输入,而不需要添加额外的库,即在标准的可用目录下的gcc编译器,因为在比赛我们显然不能向编译器添加库。直到现在在C + +,我使用cin和cout,但发现scanf和printf比它更快,所以任何人都可以告诉我一个更快的方式,它可能是一个自定义函数,因为我不介意空间复杂性,但我更喜欢时间更多。
谢谢你在高级。
I am coding for various programming olympiads and thus to improve time efficiency i am looking for the fastest method for getting input without need of adding extra library , ie , within the standard available directory as per gcc compilers as during the competitions we obviously cant add libraries to compiler . Till now in C++ , i used cin and cout , but found that scanf and printf are much faster than it , so can anybody tell me a more faster way , it maybe a custom function , for i don't mind space complexity much , but i prefer time more . Thankyou in advanced .
推荐答案
流总是慢于C-API函数是一个很常见的误解,因为默认情况下,它们与C层同步。
That streams are always slower than the C-API functions is a pretty common misconception because by default, they synchronize with the C-layer. So yeah, that's a feature, not a bug.
在不牺牲类型安全性(和可读性,取决于您的品味)的情况下,您可以通过使用以下方式获得流的性能: / p>
Without sacrificing type safety (and readability, depending on your taste), you possibly gain performance with streams by using:
std::ios_base::sync_with_stdio (false);
一点指示符:
A little indicator:
#include <cstdio>
#include <iostream>
template <typename Test>
void test (Test t)
{
const clock_t begin = clock();
t();
const clock_t end = clock();
std::cout << (end-begin)/double(CLOCKS_PER_SEC) << " sec\n";
}
void std_io() {
std::string line;
unsigned dependency_var = 0;
while (!feof (stdin)) {
int c;
line.clear();
while (EOF != (c = fgetc(stdin)) && c!='\n')
line.push_back (c);
dependency_var += line.size();
}
std::cout << dependency_var << '\n';
}
void synced() {
std::ios_base::sync_with_stdio (true);
std::string line;
unsigned dependency_var = 0;
while (getline (std::cin, line)) {
dependency_var += line.size();
}
std::cout << dependency_var << '\n';
}
void unsynced() {
std::ios_base::sync_with_stdio (false);
std::string line;
unsigned dependency_var = 0;
while (getline (std::cin, line)) {
dependency_var += line.size();
}
std::cout << dependency_var << '\n';
}
void usage() { std::cout << "one of (synced|unsynced|stdio), pls\n"; }
int main (int argc, char *argv[]) {
if (argc < 2) { usage(); return 1; }
if (std::string(argv[1]) == "synced") test (synced);
else if (std::string(argv[1]) == "unsynced") test (unsynced);
else if (std::string(argv[1]) == "stdio") test (std_io);
else { usage(); return 1; }
return 0;
}
使用g ++ -O3和一个大文本文件:
With g++ -O3, and a big text file:
cat testfile | ./a.out stdio
...
0.34 sec
cat testfile | ./a.out synced
...
1.31 sec
cat testfile | ./a.out unsynced
...
0.08 sec
适用于您的情况。修改此玩具基准,添加更多测试,并比较例如 std :: cin>> a>> b>> c 与
scanf(%d%d%d,& a,& b,& c);
。我保证,通过优化(即不处于调试模式),性能差异将是微妙的。
How this applies to your case depends. Modify this toy-benchmark, add more tests, and compare e.g. something like std::cin >> a >> b >> c
with scanf ("%d %d %d", &a, &b, &c);
. I guarantee, with optimizations (i.e. without being in debug mode), performance differences will be subtle.
如果这不会使您的需求饱和,您可以尝试其他方法,例如。首先读取整个文件(可能带来更多性能)或内存映射(这是一个非便携式解决方案,但是大型桌面有它们)。
If that does not saturate your needs, you might try other approaches, e.g. reading the whole file first (may or may not bring more performance) or memory maps (which is a non-portable solution, but the big desktops have them).
#include <cstdio>
#include <iostream>
template <typename Test>
void test (Test t)
{
const clock_t begin = clock();
t();
const clock_t end = clock();
std::cout << (end-begin)/double(CLOCKS_PER_SEC) << " sec\n";
}
void scanf_() {
char x,y,c;
unsigned dependency_var = 0;
while (!feof (stdin)) {
scanf ("%c%c%c", &x, &y, &c);
dependency_var += x + y + c;
}
std::cout << dependency_var << '\n';
}
void unsynced() {
std::ios_base::sync_with_stdio (false);
char x,y,c;
unsigned dependency_var = 0;
while (std::cin) {
std::cin >> x >> y >> c;
dependency_var += x + y + c;
}
std::cout << dependency_var << '\n';
}
void usage() { std::cout << "one of (scanf|unsynced), pls\n"; }
int main (int argc, char *argv[]) {
if (argc < 2) { usage(); return 1; }
if (std::string(argv[1]) == "scanf") test (scanf_);
else if (std::string(argv[1]) == "unsynced") test (unsynced);
else { usage(); return 1; }
return 0;
}
结果:
scanf: 0.63 sec
unsynced stream: 0.41
这篇关于这是C ++中输入速度最快的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!