關於vector push_back()與其餘方式讀取數據的效率對比

引言:
在讀取大量數據(數組)時,使用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

相關文章
相關標籤/搜索