先做一个有趣的实验,测试一下 CArray和 vector添加数据的效率: 结果 (VS2005, release,默认优化 O2): 可以看到,当需要添加大量数据时, CArray明显比 vector慢。 测试代码: const int TEST_CASE_SIZE = 5; long len[TEST_CASE_SIZE]; long time_used[2][TEST_CASE_SIZE]; CalcuUsedTime ct; { //Init data length len[0] = 100; for (int i=1; i<TEST_CASE_SIZE; ++i) len[i] = len[i-1] * 10; } { //Test CArray for (int i=0; i<TEST_CASE_SIZE; ++i) { CArray<int, int> arr; ct.SetStartTimeSpot(); for (int j=0; j<len[i]; ++j) arr.Add (j); time_used[0][i] = ct.LookForTimeUsed(); } } { //Test vector for (int i=0; i<TEST_CASE_SIZE; ++i) { vector<int> arr; ct.SetStartTimeSpot(); for (int j=0; j<len[i]; ++j) arr.push_back (j); time_used[1][i] = ct.LookForTimeUsed(); } } { //Output result string strResult[3]; strResult[0] = "Data Length : "; strResult[1] = "CArray(ms) : "; strResult[2] = "vector(ms) : "; const int VALUE_LEN = 10; char value[VALUE_LEN]; for (int i=0; i<TEST_CASE_SIZE; ++i) { sprintf_s (value, VALUE_LEN, "%7d ", len[i]); strResult[0] += value; sprintf_s (value, VALUE_LEN, "%7d ", time_used[0][i]); strResult[1] += value; sprintf_s (value, VALUE_LEN, "%7d ", time_used[1][i]); strResult[2] += value; } cout << strResult[0].c_str() << endl; cout << strResult[1].c_str() << endl; cout << strResult[2].c_str() << endl; } #ifdef _WIN3