contributed by <heathcliffYang
>
Because I spent much time installing Lubuntu on mac, I read reference first.
$ lscpu | grep cache
Check the size of cache.
$ echo 1 | sudo tee /proc/sys/vm/drop_caches
Clean the cache.
eog runtime.png
Watch the picture.
Set a linked list to store the searching flags which is the first word in the segment of the same first letter.
cache-misses: 2,009,169 # 96.121% of all cache refs (± 0.71%)
cache-references: 2,090,254 (± 0.72%)
instructions: 260,893,651 # 1.42 insns per cycle (± 0.02%)
cycles: 183,226,677 (± 0.31%)
time elapsed: 0.072529778 seconds (± 2.26%)
cache-misses: 7,562 # 41.626% of all cache refs (± 1.47%)
cache-references: 18,166 (± 0.63%)
instructions: 627,073 # 0.53 insns per cycle (± 0.17%)
cycles: 1,191,255 (± 0.73%)
time elapsed: 0.160321086 seconds (± 11.76%)
Segmentation fault
.h
typedef struct __PHONE_BOOK_ENTRY {
char lastName[MAX_LAST_NAME_SIZE];
struct __PHONE_BOOK_ENTRY *pNext;
} entry;
typedef struct __PHONE_BOOK_MORE {
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];
} more;
static entry *searchIndex; // for index searching
static entry *indexHead; // for index searching
static entry *temp;
.c
entry *findName(char lastName[], entry *pHead)
{
while (pHead != NULL) {
if (lastName[0]==pHead->lastName[0]) {
if (strcasecmp(lastName, pHead->lastName) == 0)
return pHead;
pHead = pHead->pNext;
}
else {
pHead = indexHead->pNext;
indexHead = indexHead->pNext;
}
}
return NULL;
}
entry *append(char lastName[], entry *e)
{
e->pNext = (entry *) malloc(sizeof(entry));
e = e->pNext;
if (temp->lastName[0]=='0') {
/* build the entry for index searching */
indexHead = (entry *) malloc(sizeof(entry));
searchIndex = indexHead;
}
if (temp->lastName[0]!=e->lastName[0]) {
searchIndex->pNext = e;
searchIndex=searchIndex->pNext;
}
strcpy(e->lastName, lastName);
temp=e;
e->pNext = NULL;
return e;
}
Remove
opt.txt
andorig.txt
. Because the program won't remove the previous 100 data. – From hugikun999
Forgot to malloc() for temp
cache-misses: 470,826 # 77.273% of all cache refs (± 0.14%)
cache-references: 605,423 (± 0.37%)
instructions: 270,811,645 # 0.53 insns per cycle (± 0.02%)
cycles: 150,092,320 (± 0.30%)
time elapsed: 0.054915752 seconds (± 1.66%)
flag 設置缺失: 因為在e的lastName尚未設置好時,就檢查開頭字母,printf檢查結果如下
set the flag,1; temp:a, e:set the flag,2; temp:a, e:
解決方法: 將strcpy()
提前,讓e的lastName有東西
Performance counter stats for './phonebook_opt' (100 runs):
227,675 cache-misses # 78.157 % of all cache refs ( +- 0.14% )
291,305 cache-references ( +- 0.39% )
186,204,940 instructions # 1.76 insns per cycle ( +- 0.03% )
106,058,289 cycles ( +- 0.14% )
0.037255178 seconds time elapsed ( +- 1.01% )
cache-miss 仍偏高
strlen()
計算lastName長度去判斷Performance counter stats for './phonebook_opt' (100 runs):
328,439 cache-misses # 82.416 % of all cache refs ( +- 0.04% )
398,513 cache-references ( +- 0.35% )
189,556,866 instructions # 1.70 insns per cycle ( +- 0.03% )
111,267,313 cycles ( +- 0.22% )
0.043569709 seconds time elapsed ( +- 2.10% )
cache-miss 亦增加
兩者時間都增加了
It can compress text by 40-50% in the average case.
Target function:
int smaz_compress(char *in, int inlen, char *out, int outlen);
重寫code
int pthread_create(pthread_t *restrict thread, const pthread_attr_t *restrict attr, void *(*start_routine)(void*), void *restrict arg);
pthread_exit (status)
pthread_attr_t
的pthread 的屬性變數pthread_attr_init()
初始化屬性變數pthread_attr_setdetachstate()
設定其屬性為detached statusThese topics are not covered here, however a good overview of "how things work" under Linux can be found in the sched_setscheduler man page.
一種SRAM,存取速度較能與CPU匹配,因此會將常用的資料保存在cache中,讓CPU不需要等待資料進來。
根據同一條指令的歷史紀錄進行預測,讀取接下來最可能被執行的指令,並非順序讀取;對重複性的分支指令序列()能得到比較好的預測結果,若是swithc-case()則無法。
Q:造成branch-miss的原因?
待讀完1
待讀完2
待讀完3
strlen(stringName);
returns the length of string.
typedef struct _a_{ unsigned long size; struct _a_ *next; } b;
typedef extends struct a as a data type, b;
int strcasecmp(const char *s1, const char *s2);
strcasecmp() compares s1 and s2 with ignoring the letter case.
Return 0: length equal
Return >0: s1>s2
Return <0: s1<s2
int *ptr; ptr = malloc(sizeof(int));
malloc() allocates dynamic memory with sizeof() calculating the space it needs. If it succeeds, malloc() will return the pointer to the space.
ptr=fopen("filename","r");
fgets(s,n,ptr)
fopen() r represents the open mode.
fgets() reads data line by line from the file. The first parameter is the array which stores the input data. The second one is the character number at most. The third one is the pointer to the file.
clock_gettime()
<< : shift left