为什么scanf比cin快
I frequently hear that cin
is significantly slower than scanf
in C++. Is this true? And how to improve the efficiency of cin
? It is really nice to use most of time.
我经常听到cin
比C ++中的scanf
慢得多。 这是真的? 以及如何提高cin
的效率? 大部分时间都非常好用。
One discussion about that cin is very slow is here: http://apps.topcoder.com/forums/?module=Thread&threadID=508058&start=0&mc=7
关于该cin的讨论非常慢,请点击此处: http : //apps.topcoder.com/forums/? module=Thread&threadID=508058&start=0& mc=7
In short: cin
is not always slower (can be faster actually, see below) than scanf
; add this piece of code and avoid using scanf
printf
totally:
简而言之: cin
并不总是比scanf
慢(实际上可以更快,见下文); 添加这段代码,并避免完全使用scanf
printf
:
std::ios::sync_with_stdio(false);
The special steps taken by libstdc++, at least for version 3.0, involve doing very little buffering for the standard streams, leaving most of the buffering to the underlying C library. (This kind of thing is tricky to get right.) The upside is that correctness is ensured. The downside is that writing through cout can quite easily lead to awful performance when the C++ I/O library is layered on top of the C I/O library (as it is for 3.0 by default).
libstdc ++至少在3.0版中采取的特殊步骤涉及对标准流进行很少的缓冲,而将大部分缓冲留给了基础C库。 (这种事情很难弄对。)好处是可以确保正确性。 缺点是,当C ++ I / O库位于CI / O库的顶部时,通过cout编写很容易导致糟糕的性能(默认情况下为3.0)。
However, the C and C++ standard streams only need to be kept in sync when both libraries’ facilities are in use. If your program only uses C++ I/O, then there’s no need to sync with the C streams. The right thing to do in this case is to call
但是,仅在使用两个库的功能时,才需要使C和C ++标准流保持同步。 如果您的程序仅使用C ++ I / O,则无需与C流进行同步。 在这种情况下,正确的做法是致电
#include any of the I/O headers such as ios, iostream, etc
#include any of the I/O headers such as ios, iostream, etc
std::ios::sync_with_stdio(false);
You must do this before performing any I/O via the C++ stream objects.
您必须先执行此操作,然后才能通过C ++流对象执行任何I / O。
More at: http://gcc.gnu.org/onlinedocs/libstdc /manual/io_and_c.html
有关更多信息,请访问: http : //gcc.gnu.org/onlinedocs/libstdc /manual/io_and_c.html
Now, lets have some fun:
现在,让我们玩得开心:
[zma@office io]$ cat ../../python/1ton.py
i = 0
while i < 10000000:print i
i = i + 1
[zma@office io]$ python ../../python/1ton.py > /tmp/in.txt
[zma@office io]$ cat scanf.c
#include <stdlib.h>
#include <stdio.h>
int main()
{
int i;
while ( scanf("%d", &i) != EOF);
return 0;
}
[zma@office io]$ gcc scanf.c
[zma@office io]$ time ./a.out < /tmp/in.txt
real 0m 1.645s
user 0m 1.621s
sys 0m 0.015s
[zma@office io]$ cat cin.cc
#include <iostream>
int main()
{
int i;
// std::ios_base::sync_with_stdio(false);
while (std::cin >> i);
return 0;
}
[zma@office io]$ g++ cin.cc
[zma@office io]$ time ./a.out < /tmp/in.txt
real 0m 3.864s
user 0m 3.838s
sys 0m 0.007s
[zma@office io]$ cat cin-no-sync-with-stdio.cc
#include <iostream>
int main()
{
int i;
std::ios_base::sync_with_stdio(false);
while (std::cin >> i);
return 0;
}
[zma@office io]$ g++ cin-no-sync-with-stdio.cc
[zma@office io]$ time ./a.out < /tmp/in.txt
real 0m 0.984s
user 0m 0.970s
sys 0m 0.008s
The results above should be clear enough.
上面的结果应该足够清楚。
A try with scala:
尝试使用scala :
$ cat ReadInts.scala
object ReadInts extends App {
valstart = System.nanoTime
var d = 0
try {
while (true) {
d = Console.readInt
}
} catch {
case _: Throwable => 0
}
println("Elapsed " + (System.nanoTime - start) / 1000000000.0 + "s");
}
$ sbt compile
...
$ time scala -cp target/scala-2.10/classes/ ReadInts < ./test.txt
Elapsed 1.050056043s
real 0m1.335s
user 0m1.460s
sys 0m0.079s
For comparison:
为了比较:
$ cat scanf.c
#include <stdlib.h>
#include <stdio.h>
int main()
{
int i;
while ( scanf("%d", &i) != EOF);
return 0;
}
$ gcc scanf.c
$ time ./a.out <../../scala/test.txt
real 0m1.083s
user 0m1.057s
sys 0m0.020s
The results of the time on Scala is impressive—it achieves similar time as the scanf
version in C. Of course, the starting of the JVM/scala takes additional 0.2+ seconds.
Scala上的时间结果令人印象深刻,它实现的时间与C语言中的scanf
版本相似。当然,JVM / scala的启动还需要0.2+秒的时间。
翻译自: https://www.systutorials.com/is-cin-much-slower-than-scanf-in-c/
为什么scanf比cin快