引言:
在读取大量数据(数组)时,使用vector会尽可能保证不会炸空间(MLE),可是相比于scanf的读取方式会慢上很多。但到底效率相差有多大,咱们将经过对比测试获得结果。c++
测试数据:利用srand()函数生成1e7的随机数组(x[i] ∈ (0, 115000]),最终结果将是读取这1e7(一千万)的数组所消耗的时间。数组
测试环境:在Linux虚拟机下测试,利用编译命令:time ./t获得运行时间。markdown
备注:在debug模式下运行,不开任何优化。函数
生成数据代码:测试
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10000005, lenth = 115000;
int n, x, y;
int main()
{
freopen("test.in", "w", stdout);
cout << maxn << endl;
srand((unsigned int) time(0));
for(int i = 0; i != maxn; ++i)
{
x = rand()%lenth+1;
cout << x << endl;
}
fclose(stdout);
return 0;
}
对比读入:
1.正常使用push_back()读入优化
for(int i = 0; i != n; ++i)
{ scanf("%d", &curr); q1.push_back(curr); }
2.每次空间不够时将vector数组增大空间ui
void test_resize(int a)
{
if(num == size_2-1)
{
q2.resize(size_2 += 10000);
}
q2[++num] = a;
return ;
}
for(int i = 0; i != n; ++i)//main函数中
{
scanf("%d", &curr);
test_resize(curr);
}
3.scanf读入spa
for(int i = 0; i != n; ++i)//main函数中
{
scanf("%d", &x[i]);
}
4.读入优化debug
int read()
{
input = 0;
a = getchar();
while(a < '0' || a > '9')
a = getchar();
while(a >= '0' && a <= '9')
{
input = input*10+a-'0';
a = getchar();
}
return input;
}
for(int i = 0; i != n; ++i)
{
x[i] = read();
}
5.读入优化+resize(),再扔入vector数组code
void test_resize(int a)
{
if(num == size_2-1)
{
q2.resize(size_2 += 10000);
}
q2[++num] = a;
return ;
}
int read()
{
input = 0;
a = getchar();
while(a < '0' || a > '9')
a = getchar();
while(a >= '0' && a <= '9')
{
input = input*10+a-'0';
a = getchar();
}
return input;
}
for(int i = 0; i != n; ++i)
{
curr = read();
test_resize(curr);
}
测试结果:
1.push_back()读入
real 0m2.046s
user 0m1.620s
sys 0m0.428s
2.resize()后再读入
real 0m1.743s
user 0m1.636s
sys 0m0.104s
3.scanf读入
real 0m1.885s
user 0m1.776s
sys 0m0.108s
4.读入优化
real 0m0.996s
user 0m0.948s
sys 0m0.044s
5.读入优化+resize,再扔入vector数组
real 0m1.121s
user 0m1.036s
sys 0m0.084s
读入优化一骑绝尘,读入优化+resize位居第二,scanf和resize大体至关,push_back()最慢。
结论:
当数据范围很大的时候,建议使用vector的resize(lenth)+读入优化的方式进行读取,这样既最大限度下降了内存的浪费,又保证了不会在读入上花费过久。
完整测试程序:
#include <bits/stdc++.h>
using namespace std;
#define maxn 10000005
vector<int> q1, q2, q3;
int n, curr, num = -1, size_1, size_2;
int x[maxn], input;
char a;
void test_resize(int a)
{
if(num == size_2-1)
{
q2.resize(size_2 += 10000);
}
q2[++num] = a;
return ;
}
int read()
{
input = 0;
a = getchar();
while(a < '0' || a > '9')
a = getchar();
while(a >= '0' && a <= '9')
{
input = input*10+a-'0';
a = getchar();
}
return input;
}
int main()
{
freopen("test.in", "r", stdin);
scanf("%d", &n);
for(int i = 0; i != n; ++i)
{
//x[i] = read();
//curr = read();
//test_resize(curr);
//scanf("%d", &x[i]);
//scanf("%d", &curr);
//test_resize(curr);
//q3.push_back(curr);
}
return 0;
}
测试自此结束。
箜瑟_qi 2017.04.07 13:55