contributed by <kylingithub
>
snoopy831002
>
#include <stdlib.h>
#include <stdio.h>
int b =2048;
void testfunc(int** ptrptr){
printf("In function Before : ptrptr = %d , *ptrptr = %d , **ptrptr = %d \n\n",ptrptr,*ptrptr,**ptrptr);
*ptrptr= &b;
printf("Do : *ptrptr= &b;\n");
}
int main(){
int a =5270;
int* ptrA = &a;
int* ptrptrA = &ptrA;
printf("Before\n");
printf("a = %d, &a = %d, ptrA=%d, &ptrA = %d,*ptrA = %d \n",a,&a,ptrA,&ptrA,*ptrA);
printf("&*ptrA = %d, *&ptrA = %d, ptrptrA = %d \n",&*ptrA,*&ptrA,ptrptrA);
printf("b = %d, &b = %d \n\n",b,&b);
testfunc(ptrptrA);
printf("After\n");
printf("a = %d, &a = %d, ptrA=%d, &ptrA = %d,*ptrA = %d \n",a,&a,ptrA,&ptrA,*ptrA);
printf("&*ptrA = %d, *&ptrA = %d, ptrptrA = %d \n",&*ptrA,*&ptrA,ptrptrA);
printf("ptrptrA = %d \n",ptrptrA);
printf("b = %d, &b = %d \n",b,&b);
return 0;
}
要增加效能,首先先參考programming small的方式,簡化了資料結構,以及使用Hash Function
void append(char lastName[])
{
/* allocate memory for the new entry and put lastName */
int index = hash(lastName);
entry* e = (entry *) malloc(sizeof(entry));
strcpy(e->lastName, lastName);
//insert to hash table
e->pNext = hashtable[index];
hashtable[index] = e;
}
entry *findName(char lastname[])
{
int index = hash(lastname);
entry* e = hashtable[index];
while (e != NULL) {
if (strcasecmp(lastname, e->lastName) == 0)
return e;
e= e->pNext;
}
return NULL;
}
效能分析
原生版本
優化版本
cache-miss cache-reference 都減少許多。
執行時間的比較
因為使用HashTable查找,查詢時間趨近於0,比起原本依序查找要快上許多。
可以看到不僅是findName(),append()的時間也減少許多,推測是因為我減少了append輸入的參數量。
問題討論
1.為何輸出HashTable的size,會顯示8bytes?