1 // ConsoleApplication5.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 6 #include <iostream> 7 #include <hash_map> 8 #include <map> 9 #include <ctime> 10 11 using namespace std; 12 13 void testMap() 14 { 15 map<int, int> m; 16 clock_t t1 = clock(); 17 18 for (int i = 0; i < 1000; i++) 19 { 20 for (int j = 0; j < 1000; j++) 21 { 22 int tmp = (i << 16) + j; 23 m[tmp] = 1; 24 } 25 } 26 clock_t t2 = clock(); 27 28 cout << "MAP: Add item time is " << ((double)(t2 - t1) / CLOCKS_PER_SEC) << "S" << endl; 29 30 clock_t t3 = clock(); 31 int sum = 0; 32 for (int i = 0; i < 1000; i++) 33 { 34 for (int j = 0; j < 1000; j++) 35 { 36 int tmp = (i << 16) + j; 37 sum += m[tmp]; 38 } 39 } 40 41 clock_t t4 = clock(); 42 43 cout << "MAP: look for item time is " << ((double)(t4 - t3) / CLOCKS_PER_SEC) << "S" << sum << endl; 44 45 int count = 0; 46 clock_t t5 = clock(); 47 for (int i = 0; i < 1000; i++) 48 { 49 for (int j = 0; j < 1000; j++) 50 { 51 int tmp = (i << 16) + j; 52 if (m[tmp] > 0) 53 { 54 count++; 55 } 56 } 57 } 58 59 clock_t t6 = clock(); 60 cout << "MAP: Calc Item time is " << ((double)(t6 - t5) / CLOCKS_PER_SEC) << "S,Count = " << count << endl; 61 } 62 63 void testHashMap() 64 { 65 hash_map<int, int> m; 66 clock_t t1 = clock(); 67 68 for (int i = 0; i < 1000; i++) 69 { 70 for (int j = 0; j < 1000; j++) 71 { 72 int tmp = (i << 16) + j; 73 m[tmp] = 1; 74 } 75 } 76 clock_t t2 = clock(); 77 78 cout << "HASH_MAP:time is " << ((double)(t2 - t1) / CLOCKS_PER_SEC) << "S" << endl; 79 80 clock_t t3 = clock(); 81 int sum = 0; 82 for (int i = 0; i < 1000; i++) 83 { 84 for (int j = 0; j < 1000; j++) 85 { 86 int tmp = (i << 16) + j; 87 sum += m[tmp]; 88 } 89 } 90 91 clock_t t4 = clock(); 92 93 cout << "HASH_MAP:time is " << ((double)(t4 - t3) / CLOCKS_PER_SEC) << "S, sum = " << sum << endl; 94 95 int count = 0; 96 clock_t t5 = clock(); 97 for (int i = 0; i < 1000; i++) 98 { 99 for (int j = 0; j < 1000; j++) 100 { 101 int tmp = (i << 16) + j; 102 if (m[tmp] > 0) 103 { 104 count++; 105 } 106 } 107 } 108 109 clock_t t6 = clock(); 110 cout << "HASH_MAP: Calc Item time is " << ((double)(t6 - t5) / CLOCKS_PER_SEC) << "S,Count = " << count << endl; 111 } 112 113 void tesArray() 114 { 115 clock_t t1 = clock(); 116 117 int *m = new int[1024 * 1024]; 118 memset(m, 0, sizeof(*m)); 119 120 for (int i = 0; i < 1000; i++) 121 { 122 for (int j = 0; j < 1000; j++) 123 { 124 int tmp = (i << 10) + j; 125 m[tmp] = 1; 126 } 127 } 128 clock_t t2 = clock(); 129 130 cout << "ARRAY:time is " << ((double)(t2 - t1) / CLOCKS_PER_SEC) << "S" << endl; 131 132 clock_t t3 = clock(); 133 int sum = 0; 134 for (int i = 0; i < 1000; i++) 135 { 136 for (int j = 0; j < 1000; j++) 137 { 138 int tmp = (i << 10) + j; 139 sum += m[tmp]; 140 } 141 } 142 143 clock_t t4 = clock(); 144 145 cout << "ARRAY:time is " << ((double)(t4 - t3) / CLOCKS_PER_SEC) << "S" << sum << endl; 146 147 int count = 0; 148 clock_t t5 = clock(); 149 for (int i = 0; i < 1000; i++) 150 { 151 for (int j = 0; j < 1000; j++) 152 { 153 int tmp = (i << 10) + j; 154 if (m[tmp] > 0) 155 { 156 count++; 157 } 158 } 159 } 160 161 clock_t t6 = clock(); 162 cout << "ARRAY: Calc Item time is " << ((double)(t6 - t5) / CLOCKS_PER_SEC) << "S,Count = " << count << endl; 163 164 delete[] m; 165 } 166 167 int _tmain(int argc, _TCHAR* argv[]) 168 { 169 testMap(); 170 testHashMap(); 171 tesArray(); 172 return 0; 173 }
VS2013 + WIN7 64运行结果:
MAP: Add item time is 0.265S
MAP: look for item time is 0.094S1000000
MAP: Calc Item time is 0.094S,Count = 1000000
HASH_MAP:time is 0.203S
HASH_MAP:time is 0.047S, sum = 1000000
HASH_MAP: Calc Item time is 0.047S,Count = 1000000
ARRAY:time is 0S
ARRAY:time is 0S1000000
ARRAY: Calc Item time is 0S,Count = 1000000
时间: 2024-10-16 09:13:12