contributed by<yanang
>
yananag
csielee
$ ./phonebook_orig
size of entry : 136 bytes
execution time of append() : 0.105205 sec
execution time of findName() : 0.004954 sec
$sudo make cache-test
4,868,866 cache-misses # 93.482 % of all cache refs ( +- 0.20% )
5,208,353 cache-references ( +- 0.38% )
262,398,775 instructions # 1.35 insn per cycle ( +- 0.02% )
194,347,267 cycles ( +- 0.77% )
0.064319469 seconds time elapsed
typedef struct __PHONE_BOOK_ENTRY_REMAIN {
char firstName[16];
char email[16];
char phone[10];
char cell[10];
char addr1[16];
char addr2[16];
char city[16];
char state[2];
char zip[5];
struct __PHONE_BOOK_ENTRY *pNext;
} entry_remain;
typedef struct __PHONE_BOOK_ENTRY {
char lastName[MAX_LAST_NAME_SIZE];
struct __PHONE_BOOK_ENTRY_REMAIN *remain;
struct __PHONE_BOOK_ENTRY *pNext;
} entry;
資料將以 linklist 方式
執行時間:
size of entry : 32 bytes
execution time of append() : 0.137438 sec
execution time of findName() : 0.002579 sec
1,679,608 cache-misses # 68.692 % of all cache refs ( +- 0.48% )
2,445,143 cache-references ( +- 0.81% )
244,480,047 instructions # 1.84 insn per cycle ( +- 0.02% )
132,706,657 cycles ( +- 1.11% )
0.044007362 seconds time elapsed
#ifdef OPT_HASH
達到目的。可以參考閱讀 BKDR hash 詳盡解說 並嘗試引入其他 hash function 實驗比較課程助教
unsigned int BKDRHash(char *str)
{
unsigned int seed = 131;
unsigned int hash = 0;
while (*str)
{
hash = hash * seed + (*str++);
}
return (hash & 0x7FFFFFFF) % SIZE_OF_TABLE;
}
entry *pHead = (entry *) malloc (sizeof(entry));
pHead->pNext = e[hash];
e[hash] = pHead;
strcpy(pHead->lastName,lastName);
/* entry *pLast = (entry *) malloc (sizeof(entry));
pLast -> pNext = NULL ;
strcpy(pLast->lastName,lastName);
e[hash] -> pNext = pLast;
* not finished
* should add loop to find the last data
*/
size of entry : 24 bytes
execution time of append() : 0.039091 sec
execution time of findName() : 0.000009 sec
437,137 cache-misses # 58.419 % of all cache refs ( +- 1.13% )
748,282 cache-references ( +- 2.45% )
232,794,043 instructions # 1.82 insn per cycle ( +- 0.02% )
127,598,545 cycles ( +- 0.89% )
0.042041802 seconds time elapsed
$ git commit --amend
$ git push --force-with-lease origin master