fgnjuf's blog

By fgnjuf, 10 years ago, In English

I have a question that : cin/cout with ios_base::sync_with_stdio(0) and scanf/printf. Which is faster? Can anybody help me? Thank you a lot!

  • Vote: I like it
  • -16
  • Vote: I do not like it

»
10 years ago, # |
  Vote: I like it 0 Vote: I do not like it

look at this

»
10 years ago, # |
  Vote: I like it 0 Vote: I do not like it

or at this

»
10 years ago, # |
  Vote: I like it +5 Vote: I do not like it

Why don't you try to read 5*10^6 or 10^7 integers and see the difference?

»
10 years ago, # |
  Vote: I like it 0 Vote: I do not like it

cin/cout uses operator <<. That is function. If you do cin >> a >> b >> c; you call 3 functions. scanf, printf should be faster still. cin/cout are comfortable and simple, but not flexible as scanf/printf. If you want to find a way to get rid of scanf/printf then it is bad idea. I always recommend to use scanf/printf. There is no thing that scanf/printf can't but cin/cout can.

  • »
    »
    10 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    There is no thing that scanf/printf can't but cin/cout can.

    To read string for example?

    • »
      »
      »
      10 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      std::string seems like is using vector like container. This trick does it. But I am not talking about that. I am talking about reading 14 and 32 from "14:32" by ignoring ':', reading string(including spaces and newlines) until ';' or '$'. I do not think that cin can to that easily.

      #include <iostream>
      #include <queue>
      #include <string>
      using namespace std;
      
      string s;
      
      int main(){
      	s.resize(1000);
      	scanf("%s", &s[0]);
      	printf("%d\n", s.size());
      	s.resize(strlen(s.c_str()));
      	cout << s << endl;
      	printf("%s\n%d\n", s.c_str(), s.size());
      	return 0;
      }