chapter16test9

原创 2015年07月07日 16:58:55


测试效果挺好的,时间明显缩短了

#include<iostream>

#include<vector>
#include<list>
#include<ctime>
#include<cstdlib>
#include<algorithm>
using namespace std;
int main()
{
srand(time(0));
int const size = 100000;
vector<int>vi0(size);
for (int i = 0; i < size; i++)
vi0[i] = rand();
vector<int>vi(vi0);    //define vi
list<int>li(vi0.begin(), vi0.begin() + size);   //define li


clock_t start0 = clock();
sort(vi.begin(), vi.end());
clock_t end0 = clock();
cout << "For vector<int> time0 :" << (double)(end0 - start0) / CLOCKS_PER_SEC;
cout << endl;
clock_t start1 = clock();
li.sort();
clock_t end1 = clock();
cout << "For list<int> time1 :" << (double)(end1 - start1) / CLOCKS_PER_SEC;
cout << endl;
clock_t start2 = clock();
copy(vi0.begin(), vi0.begin() + size,li.begin());    //redefine li
copy(li.begin(), li.end(), vi.begin());
sort(vi.begin(), vi.end());
copy(vi.begin(), vi.begin() + size,li.begin());
clock_t end2 = clock();
cout << "For list<int> time2 :" << (double)(end2 - start2) / CLOCKS_PER_SEC;
cout << endl;
return 0;
}
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:chapter16test9
举报原因:
原因补充:

(最多只允许输入30个字)