The great difference between scanf and CIN!!! (Acmen must see!) )

Source: Internet
Author: User
Tags stdin
differences in scanf and CINAs you know, there are two types of inputs, outputs,-SCANF and CIN in C + +, but there are differences between them, you know. Let's take a look at the assessment below.
Test questions:Enter n number, output n number.
test Environment:
ubuntu12.04 i3cpu 4G memory 7200 RPM HDD
Test Code:(in order to test the correctness of the results, keep the other parts (except the input and output)
scanf's code:
#include <cstdio>
#include <iostream>
using namespace std;

#define MAX 1000001

int main (int argc, char const *argv[])
{
	freopen ("scan.in", "R", stdin);

	int n;
	scanf ("%d", &n);
	for (int i=0; i<n; i++) {
		Scnaf ("%d", A[i]);
	}
	for (int i=0; i<n; i++) {
		printf ("%d", A[i]);
	}
	return 0;
}

the code for CIN:
#include <cstdio>
#include <iostream>
using namespace std;

#define MAX 1000001

int main (int argc, char const *argv[])
{
	freopen ("scan.in", "R", stdin);

	int n;
	scanf ("%d", &n);
	for (int i=0; i<n; i++) {
		Scnaf ("%d", A[i]);
	}
	for (int i=0; i<n; i++) {
		printf ("%d", A[i]);
	}
	return 0;
}

Test Results:The test results use the time test times of Linux and the results are as follows:
10000 sets of data: real represents the total time of the program execution, user input data on behalf of the time, SYS represents the system time from the above test results, seemingly did not find the scanf of the fierce and even seems to be slow (data volatility), then we look down:
100000 sets of data:
At this time scanf has begun to emerge, but the difference is not very big, let's look down!
1000000 sets of data:

At this point, we can see that CIN used twice times more time than scanf. The following will not continue to test, to this interested or want to verify their own children's shoes can continue .... However, it is said that scanf's input speed is more than 3 times times that of CIN.
Test summary: From the above test results, you can find the huge difference between scanf and cin! So, what is the main application of this thing? Needless to say, it is the ACM platform! Those are the ones that have no problem with the time limit of children's shoes, you might be because of the use of CIN caused by it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.