sayeed_1999's blog

By sayeed_1999, history, 3 days ago, In English

In some problems in different websites, you will see written — 'Dataset is HUGE! Use FAST I/O'

Here, you need to know C++'s cin cout are much slower than C's printf scanf when it comes to larger dataset input.

I was doing the problem -

''Array Queries'' in Lightoj Link- http://lightoj.com/volume_showproblem.php?problem=1082 The problem has large dataset (you may try) ;)

When I took inputs in queries(and elsewhere) like-

int x, y;

cin >> x >> y;

I was getting TLE.**(Time taken: 4s of Time limit 3s)**

[Note: many guys downvoted. Because it is said that cin can be made faster than scanf with fastio method. But i am mentioning, in 'lightoj compiler' it still gives tle. Cin isnt getting faster in lightoj ... Check before downvoting :) ]

When I learnt about the fact and then used-

int x, y;

scanf("%d",&x);

scanf("%d",&y); (Time taken- 0.285s!!!)

(Huge Optimization of Time from TLE to 0.285s)

So, this is not a slight time difference that we may ignore :) 4second VS 0.285s !!!

My blog doesn't end here! One more insight! :D

I just changed the code from

int x, y;

scanf("%d",&x);

scanf("%d",&y); (Time: 0.285s)

to

int x, y;

scanf("%d%d",&x,&y); (Time: 0.316s)

So, taking two inputs in one scanf has relatively more time consumption than by single line each.. Still, this doesn't bother us much like cin cout does ;)

Thank you all. (Don't downvote!) ^_^

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

»
3 days ago, # |
  Vote: I like it +7 Vote: I do not like it

Did you try

  ios_base::sync_with_stdio(false); cin.tie(0);

?

int x, y; scanf("%d",&x); scanf("%d",&y);

It's not a fast I/O, it's an ordinary I/O.

int x, y; scanf("%d%d",&x,&y); (Time: 0.316s)

With large I/O the execution time can vary badly so you need to submit several time to claim the dependency.

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

    I have checked several times before putting the blog :) you may check too. The problem link is given :))

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

    I would like to compensate this comment a bit more.

    You haven't stated whether you have tried

    std::ios_base::sync_with_stdio(false); std::cin.tie(NULL);
    

    as the other guy said.
    This would increase the speed for cin/cout, since there is no need to sync with scanf/printf if you just use cin/cout, and there is no need for, say, console interaction when you just read and write to separate files. Some benchmarks show that this method can be faster than scanf/printf. You can see more here.

    Also, scanf/printf is still standard C/C++, so it's normal, not "fast". Usually when Fast I/O is mentioned, they usually talk about custom I/O functions, like buffered reading/writing or such. You can find tons of articles about this topic online.

»
3 days ago, # |
Rev. 2   Vote: I like it +1 Vote: I do not like it

I have checked run several time before putting the blog :) In lightoj compiler , you will get tle with ios_base::sync_with_stdio(0); cin.tie(0);

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

    Ok. Did you use '\n' instead of endl? I've got RTE with cin/cout (I do not know why) and AC with scanf/printf (C and C++ both). Timing (C++ compiler):

    0.187, 0.202, 0.188, 0.273
    

    with scanf( "%d %d", &L, &R); and

    0.204, 0.205, 0.248, 0.249
    

    with scanf( "%d", &L); scanf( "%d", &R); So

    taking two inputs in one scanf has relatively more time consumption than by single line each

    is not true.

    AC code
    • »
      »
      »
      3 days ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Now you found my issue! On lightoj compiler its Runtime Error if you write-

      ios_base::sync_with_stdio(0); cin.tie(0);

      If you write only-

      ios::sync_with_stdio(0); //omit cin.tie(0); it works but TLE with cin cout

      Only scanf printf works..

      So may be it is their compiler problem issue or something we dont know about fastio