以前编程的时候,对于索引的计算时间基本忽略,但是当数据量很大的时候,这些索引计算的时间累加起来也是很大的时间开销,下面用实例分析
- #include <iostream>
- #include <time.h>
- #include <map>
- using namespace std;
- struct KillEvent
- {
- KillEvent()
- :killerId(0)
- ,bekillerId(0)
- {
- }
- KillEvent(int killerid, int bekillerid)
- :killerId(killerid)
- ,bekillerId(bekillerid)
- {
- }
- int killerId;
- int bekillerId;
- };
- struct PlayerScore
- {
- PlayerScore()
- :userId(0)
- ,score1(0)
- ,score2(0)
- ,score3(0)
- {
- }
- PlayerScore(int userid)
- :userId(userid)
- ,score1(0)
- ,score2(0)
- ,score3(0)
- {
- }
- int userId;
- int score1;
- int score2;
- int score3;
- };
- map<int, KillEvent> g_killEvent;
- map<int, PlayerScore> g_playerScoreList;
- int main()
- {
- clock_t start, end;
- for(int i = 0; i < 10000; ++i)
- {
- g_playerScoreList[i] = PlayerScore(i);
- }
- for(int i = 0; i < 1000000; ++i)
- {
- g_killEvent[i] = KillEvent(i,(i + 100) %1000000);
- }
- start = clock();
- for(int i = 0; i < g_killEvent.size(); ++i)
- {
- g_playerScoreList[g_killEvent[i].killerId].score1 += 10;
- g_playerScoreList[g_killEvent[i].killerId].score2 += 5;
- g_playerScoreList[g_killEvent[i].killerId].score3 += 3;
- /*int index = g_killEvent[i].killerId;
- g_playerScoreList[index].score1 += 10;
- g_playerScoreList[index].score2 += 5;
- g_playerScoreList[index].score3 += 3;*/
- }
- end = clock();
- cout << "total use " << (end - start) /CLOCKS_PER_SEC << " s" << endl;
- system("pause");
- return 0;
- }
直接使用索引的结果是
:total use 45 s
将索引计算的结果保存下来的计算结果是
:total use 34 s
所以在一些大数据量处理的时候,我们千万不要忽略索引本身的计算开销
阅读(1190) | 评论(0) | 转发(0) |