# ios::sync_with_stdio(false);

1 篇文章 0 订阅

#include <ctime>
int main()
{
int start = clock();
//DO SOMETHING
printf("%.3lf\n",double(clock()-start)/CLOCKS_PER_SEC);
}﻿﻿﻿﻿﻿﻿
最简单的方法就算写一个循环scanf了，代码如下：
const int MAXN = 10000000;

int numbers[MAXN];

{
freopen("data.txt","r",stdin);
for (int i=0;i<MAXN;i++)
scanf("%d",&numbers[i]);
}

?可是效率如何呢？在我的电脑Linux平台上测试结果为2.01秒。接下来是cin，代码如下
const int MAXN = 10000000;

int numbers[MAXN];

{
freopen("data.txt","r",stdin);
for (int i=0;i<MAXN;i++)
std::cin >> numbers[i];
}

const int MAXN = 10000000;

int numbers[MAXN];

{
freopen("data.txt","r",stdin);
std::ios::sync_with_stdio(false);
for (int i=0;i<MAXN;i++)
std::cin >> numbers[i];
}

const int MAXS = 60*1024*1024;
char buf[MAXS];

void analyse(char *buf,int len = MAXS)
{
int i;
numbers[i=0]=0;
for (char *p=buf;*p && p-buf<len;p++)
if (*p == ' ')
numbers[++i]=0;
else
numbers[i] = numbers[i] * 10 + *p - '0';
}

const int MAXN = 10000000;
const int MAXS = 60*1024*1024;

int numbers[MAXN];
char buf[MAXS];

{
freopen("data.txt","rb",stdin);
buf[len] = '\0';
analyse(buf,len);
}

const int MAXN = 10000000;
const int MAXS = 60*1024*1024;

int numbers[MAXN];
char buf[MAXS];

{
int fd = open("data.txt",O_RDONLY);
buf[len] = '\0';
analyse(buf,len);
}

0.
06-16 1万+

01-05 3391
11-06 1万+
07-24 7279
01-23 2419
06-27 9904
12-07 753
06-09 4487
07-15 163