typedef pair<long, int> PAIR_X; map<long, int> mLongInt; map<PAIR_X, int> mPairx; #define LOOP_MAX 1000000 int main(void) { time_t tSimpleInsert, tSimpleQuery; { time_t tBgn = clock(); for (int i = 0; i < LOOP_MAX; i++) { mLongInt[i] = i + 100; } int xCount = 0; time_t tInsert = clock(); tSimpleInsert = tInsert - tBgn; for (int i = 0; i < LOOP_MAX; i++) { auto it = mLongInt.find(i); if (it != mLongInt.end()) { xCount++; } } time_t tQuery = clock(); tSimpleQuery = tQuery - tInsert; } time_t tPairInsert, tPairQuery; { time_t tBgn = clock(); for (int i = 0; i < LOOP_MAX; i++) { PAIR_X px(i, i + 1); mPairx[px] = i + 100; } int xCount = 0; time_t tInsert = clock(); tPairInsert = tInsert - tBgn; for (int i = 0; i < LOOP_MAX; i++) { PAIR_X px(i, i + 1); auto it = mPairx.find(px); if (it != mPairx.end()) { xCount++; } } time_t tQuery = clock(); tPairQuery = tQuery - tInsert; } return 0; }
今天突然想到个问题。
typedef pair<long, int> PAIR_X; map<long, int> mLongInt; map<PAIR_X, int> mPairx;
这种写法情况下。mLongInt和mPairx的性能差距大不大。。然后就用vs写了个测试.测试结果还好。100W次insert差个1S多、find差个1s左右。。
记录下