-3

I need to input a large amount of numbers in range of 1 to 10000 in a programming question.The questions advices to use a fast I/O method.

I looked at Fast input/output in competitive programming but it was too complex. So can anybody please tell me a simpler way to get fast io.

Also please tell me if using gets and then doing atoi() is faster than using scanf(%d) for taking numbers as input.

Community
  • 1
  • 1
Devender Goyal
  • 1,450
  • 3
  • 18
  • 26
  • 2
    `atoi` and `scanf` are used for separate things, and not really comparable. – Richard J. Ross III May 26 '12 at 01:29
  • I'd be curious to know what the problem poser thinks of as Fast IO and Slow IO methods... – sarnold May 26 '12 at 01:31
  • 1
    > Also please tell me if using gets and then doing atoi() is faster than using scanf(). I wouldn't trust anyone to tell me anything about what is faster, measure it for your use case then make your own decision. – Doug T. May 26 '12 at 01:32

4 Answers4

3

To answer the second part of your question, for me, it appears that atoi is about twice as fast. Consider the following:

#define ITERS 1000000

clock_t testAtoi()
{
    char buffer[64];
    clock_t start = clock();

    for (int i = 0; i < ITERS; i++) {
        sprintf(buffer, "%i", i);
        int l = atoi(buffer);
    }

    return clock() - start;
}

clock_t testScanf()
{
    char buffer[64];
    clock_t start = clock();

    for (int i = 0; i < ITERS; i++) {
        sprintf(buffer, "%i", i);
        int l = 0;
        sscanf(buffer, "%i", &l);
    }

    return clock() - start;
}

int main()
{
    printf("clocks for atoi: %lu\n", testAtoi());
    printf("clocks for sscanf: %lu\n", testScanf());

    return 0;
}

For me, using gcc and -O0 (so my variables aren't optimized away), the program outputs:

clocks for atoi: 222011
clocks for sscanf: 392409

But, if you are using a FILE *, then maybe fscanf would be faster. I don't have the time to compare the two right now, but for raw strings, I would just use atoi for the most part.

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
  • 1
    `clock` is probably not the appropriate function to measure this, since it measures the cpu utilization and not the time spent. And, why do you use `-Os` and not `-O3` ? – Jens Gustedt May 26 '12 at 06:38
  • 1
    @JensGustedt While I realize that `clock` only shows CPU ticks, generally speaking, the function with the greatest amount of CPU ticks is slower. It also gives us a way to measure the Raw speed of a function albeit a little bit out of whack (e.g. sleep will mess it up). I used `-Os` simply because it's what Xcode said was the fastest, but I'll retry with `-O3` and show the results. – Richard J. Ross III May 26 '12 at 13:11
1

There is no specific library for fast input/output. You can turn off the syncing, this will take the input together and will print the output together.

Add the following lines of code in main() to turn off syncing:

int main(){
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        // Your code ................
}
-1

There is no any special library in C/C++ for FASTIO.

The Fastest Method to print output in C/C++ is fwrite or fwrite_unlocked in stdio.h library.

Also there isn't any special/Direct function to print output at once .

Indirectly,it can be achieved using append function ( appending all output strings to one single string and printing the final string)in cplusplus and even strcat function in string.h lib,but performance wise ,"Appending or concat + printing",is slower ,even far slower than printf.

So the fastest way in c/c++ is :directly read the stream in the raw form, and extract the information required.Also put the output in raw form in a huge buffer and display it finally using fwrite.

And please try to go through the solution in this link for fast IO .(http://www.codechef.com/viewsolution/244848)

Devender Goyal
  • 1,450
  • 3
  • 18
  • 26
-1

Refer to this blog: http://bugdivine.blogspot.com/p/fast-input-reader-in-cc.html

The fastest way to take input in C/C++ is to read each character from input buffer and push them into your resulting variable until you reach a delimiter.

However, scanf is also pretty fast, and the case where we have to use getchar_unlocked rarely occurs even in the world of competitive programming.

Shubham Jain
  • 392
  • 1
  • 3
  • 15