array
#include <array>
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
using namespace std;
#define RAND_MAX 0x7fff
const long ASIZE = 100000L;
long get_a_target_long()
{
long target = 0;
cout << "target (0~" << RAND_MAX << "):";
cin >> target;
return target;
}
int compareLongs(const void*a, const void* b)
{
return (*(long*)a - *(long*)b);
}
void test_array() //主要运行函数
{
array<long, ASIZE> c;
clock_t timeStart = clock();
for (long i=0; i < ASIZE; ++i) {
c[i] = rand();
}
cout << "milli-seconds :" << (clock()-timeStart) << endl;
cout << "array.size()=" << c.size() << endl;
cout << "array.front()=" << c.front() << endl;
cout << "array.back()=" << c.back() << endl;
cout << "array.data()=" << c.data() << endl; //data表示第一个元素地址
long target = get_a_target_long();
timeStart = clock();
::qsort(c.data(), ASIZE, sizeof(long), compareLongs);
long* pItem = (long*)::bsearch(&target, (c.data()), ASIZE, sizeof(long), compareLongs);
cout << "qsort()+bsearch(), milli-seconds :" << (clock()-timeStart) << endl;
if (pItem != NULL)
cout << "found, " << *pItem << endl;
else
cout << "not found!" << endl;
}
结论:
- 插入100000L个,用时10
- qsort+bsearch查找用时153
vector
vector的内存是成倍增长(一个元素一个空间,两个元素两个空间,三个元素有四个空间,四个元素四个空间)存不下就找个新的内存,全部数据移动到新内存
#include <vector>
#include <stdexcept>
#include <string>
#include <cstdlib> //abort()
#include <cstdio> //snprintf()
#include <iostream>
#include <ctime>
#include <algorithm> //sort()
#include <stdio.h>
//vs2012使用snprintff
#if _MSC_VER
#define snprintf _snprintf
#endif
string get_a_target_string()
{
long target = 0;
char buf[10];
cout << "target (0~" << RAND_MAX << "): ";
cin >> target;
snprintf(buf, 10, "%d",target);
return string(buf);
}
int compareStrings(const void *a, const void *b)
{
if (*(string*)a > *(string*)b)
return 1;
else if (*(string*)a < *(string*)b)
return -1;
else
return 0;
}
void test_vector(const long &value)
{
vector<string> c;
char buf[10];
clock_t timeStart = clock();
for (long i = 0; i < value; ++i)
{
try
{
snprintf(buf, 10, "%d", rand());
c.push_back(string(buf));
}
catch(exception& p)
{
cout << "i=" << i << " " << p.what() << endl; //防止超过最大内存导致程序失败
abort(); // 结束程序(exit表示结束,有清理动作,abort只是结束)
}
}
cout << "milli-seconds : " << (clock()-timeStart) << endl;
cout << "vector.max_size()= " << c.max_size() << endl; //1073747823
cout << "vector.size()= " << c.size() << endl;
cout << "vector.front()= " << c.front() << endl;
cout << "vector.back()= " << c.back() << endl;
cout << "vector.data()= " << c.data() << endl;
cout << "vector.capacity()= " << c.capacity() << endl << endl;
string target = get_a_target_string();
{
timeStart = clock();
auto pItem = find(c.begin(), c.end(), target);
cout << "std::find(), milli-seconds : " << (clock() - timeStart) << endl;
if (pItem != c.end())
{
cout << "found, " << *pItem << endl << endl;
}
else
{
cout << "not found!" << endl << endl;
}
{
timeStart = clock();
sort(c.begin(), c.end());
cout << "sort, milli-seconds :" << (clock()-timeStart) << endl;
timeStart = clock();
string *pItem = (string*)::bsearch(&target, (c.data()),
c.size(), sizeof(string), compareStrings);
cout << "bsearch(), milli-seconds : " << (clock()-timeStart) <<endl;
if (pItem != NULL)
cout << "found, " << *pItem << endl << endl;
else
cout << "not found!" <<endl << endl;
}
c.clear();
}
}
结论:
- 插入100000L个,用时1740
- 占用138255个空间
- find查找用时:52
- sort用时:6670,bsearch查找用时0
list
void test_list(const long &value)
{
list<string> c;
char buf[10];
clock_t timeStart = clock();
for (long i=0; i < value; ++i)
{
try
{
snprintf(buf, 10, "%d", rand());
c.push_back(string(buf));
}
catch (exception& p)
{
cout << "i=" << i << " " << p.what() << endl;
abort();
}
}
cout << "milli-seconds :" << (clock() - timeStart) << endl;
cout << "list.size()= " << c.size() << endl;
cout << "list.max_size()=" << c.max_size() << endl;
cout << "list.front()=" << c.front() << endl;
cout << "list.back()=" << c.back() << endl;
string target = get_a_target_string();
timeStart = clock();
auto pItem = find(c.begin(), c.end(), target);
cout << "std::find(), milli-seconds:" << (clock()-timeStart) << endl;
if (pItem != c.end())
cout << "found, " << *pItem << endl;
else
cout << "not found!" << endl;
timeStart = clock();
c.sort(); //list自带sort,有自带使用自带的
cout << "c.sort(), milli-seconds:" << (clock()-timeStart) << endl;
//list不使用bsearch
c.clear();
}
结论:
- 插入100000L个,用时1066
- find查找用时81,
- 自带的sort用时22922
forward_list(单向链表)
#include <forward_list>
#include <stdexcept>
#include <string>
#include <cstdlib> //abort()
#include <cstdio> //snprintf()
#include <iostream>
#include <ctime>
void test_forward_list(const long &value)
{
forward_list<string> c;
char buf[10];
clock_t timeStart = clock();
for (long i=0; i < value; ++i)
{
try
{
snprintf(buf, 10, "%d", rand());
c.push_front(string(buf));
}
catch (exception& p)
{
cout << "i=" << i << " " << p.what() << endl;
abort();
}
}
cout << "milli-seconds:" << (clock()-timeStart) << endl;
cout << "forward_list.max_size()=" << c.max_size() << endl;
cout << "forward_list.front()=" << c.front() << endl;
string target = get_a_target_string();
timeStart = clock();
auto pItem = find(c.begin(), c.end(), target);
cout << "std::find(), milli-seconds:" << (clock()-timeStart) << endl;
if (pItem != c.end())
cout << "found, " << *pItem << endl;
else
cout << "not found!" << endl;
timeStart = clock();
c.sort();
cout << "c.sort, milli-seconds :" << (clock()-timeStart) << endl;
c.clear();
}
结论:
- 插入100000L个,用时1130
- find查找用时2
- 自带的sort用时28547
slist
(单向链表,gnu独特的链表类似forward_list)