Question: I don't know if this is true, but when I was reading FAQ on one of the problem providing sites, I found something, that poke my attention:

> 
> Check your input/output methods. In C++, using cin and cout is too slow. Use these, and you will guarantee not being able to solve any problem with a decent amount of input or output. Use printf and scanf instead.
> 
> 
> 

Can someone please clarify this? Is really using *scanf()* in C++ programs faster than using *cin >> something* ? If yes, that is it a good practice to use it in C++ programs? I thought that it was C specific, though I am just learning C++...

Answer: 
Here's a quick test of a simple case: a program to read a list of numbers from standard input and XOR all of the numbers.

**iostream version:**

```
#include <iostream>

int main(int argc, char **argv) {

  int parity = 0;
  int x;

  while (std::cin >> x)
    parity ^= x;
  std::cout << parity << std::endl;

  return 0;
}

```

**scanf version:**

```
#include <stdio.h>

int main(int argc, char **argv) {

  int parity = 0;
  int x;

  while (1 == scanf("%d", &x))
    parity ^= x;
  printf("%d\n", parity);

  return 0;
}

```

**Results** 

Using a third program, I generated a text file containing 33,280,276 random numbers. The execution times are:

```
iostream version:  24.3 seconds
scanf version:      6.4 seconds

```

Changing the compiler's optimization settings didn't seem to change the results much at all.

Thus: there really is a speed difference.

---

*EDIT:* User clyfish [points out below](https://stackoverflow.com/a/15939289/462335) that the speed difference is largely due to the iostream I/O functions maintaining synchronization with the C I/O functions. We can turn this off with a call to `std::ios::sync_with_stdio(false);`:

```
#include <iostream>

int main(int argc, char **argv) {

  int parity = 0;
  int x;

  std::ios::sync_with_stdio(false);

  while (std::cin >> x)
    parity ^= x;
  std::cout << parity << std::endl;

  return 0;
}

```

**New results:**

```
iostream version:                       21.9 seconds
scanf version:                           6.8 seconds
iostream with sync_with_stdio(false):    5.5 seconds

```

**C++ iostream wins!** It turns out that this internal syncing / flushing is what normally slows down iostream i/o. If we're not mixing stdio and iostream, we can turn it off, and then iostream is fastest.

The code: <https://gist.github.com/3845568>
Of course it's ridiculous to use cstdio over iostream. At least when you develop software (if you are already using c++ over c, then go all the way and use it's benefits instead of only suffering from it's disadvantages).

But in the online judge you are not developing software, you are creating a program that should be able to do things Microsoft software takes 60 seconds to achieve in 3 seconds!!!

So, in this case, the golden rule goes like (of course if you dont get into even more trouble by using java)

* Use c++ and use all of it's power (and heaviness/slowness) to solve the problem
* If you get time limited, then change the cins and couts for printfs and scanfs
(if you get screwed up by using the class string, print like this: printf(%s,mystr.c\_str());
* If you still get time limited, then try to make some obvious optimizations (like avoiding too many embedded for/while/dowhiles or recursive functions). Also make sure to pass by reference objects that are too big...
* If you still get time limited, then try changing std::vectors and sets for c-arrays.
* If you still get time limited, then go on to the next problem...