# 2016q3 Homework1 (phonebook) contributed by `HahaSula` [github](https://github.com/HahaSula/phonebook) ## 開發環境 ***Linux**核心: ```shell $ cat /etc/issue Ubuntu 16.04.1 LTS \n \l` ``` ***Linux**系統版本: ```shell $ uname -a Linux sula-IdeaPad-U430p 4.4.0-38-generic #57-Ubuntu SMP Tue Sep 6 15:42:33 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux ``` ***電腦規格** ```shell $ lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 4 On-line CPU(s) list: 0-3 Thread(s) per core: 2 Core(s) per socket: 2 Socket(s): 1 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 69 Model name: Intel(R) Core(TM) i5-4200U CPU @ 1.60GHz Stepping: 1 CPU MHz: 805.000 CPU max MHz: 2600.0000 CPU min MHz: 800.0000 BogoMIPS: 4589.16 Virtualization: VT-x L1d cache: 32K L1i cache: 32K L2 cache: 256K L3 cache: 3072K NUMA node0 CPU(s): 0-3 ``` ## 準備工作 灌ubunt 多重開機 安裝git fork clone 作業 ## Perf ## 原始版本 總之先按表操課一下 ```shell $ make run echo 3 | sudo tee /proc/sys/vm/drop_caches [sudo] password for sula: ``` ``` size of entry : 136 bytes execution time of append() : 0.068601 sec execution time of findName() : 0.005916 sec ``` make run 之後可以看到一次次的append()跟findName()Time make plot 之後可以看到圖表已經自動畫好了 ![](https://i.imgur.com/wqS4nqH.png) # 減少表單內容 由於findName裏面只有用到lastName和*pNext 所以其餘資料先不加進去 append & findName 先直接照抄orig版 ```c /* original version */ typedef struct __PHONE_BOOK_ENTRY { char lastName[MAX_LAST_NAME_SIZE]; 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; ``` ```c * line to set OPT properly */ // #define OPT 1 typedef struct __PHONE_BOOK_ENTRY { char lastName[MAX_LAST_NAME_SIZE]; BOOK_ENTRY *pNext; } entry; ``` 這邊看到 cache-misses 次數大幅減少 ``` Performance counter stats for './phonebook_orig' (100 runs): 126,0577 cache-misses # 90.307 % of all cache refs ( +- 0.18% ) 139,5880 cache-references ( +- 0.16% ) 2,6100,9656 instructions # 1.45 insns per cycle ( +- 0.02% ) 1,7956,9863 cycles ( +- 0.18% ) 0.072096865 seconds time elapsed ( +- 0.30% ) Performance counter stats for './phonebook_opt' (100 runs): 8,8189 cache-misses # 39.000 % of all cache refs ( +- 0.57% ) 22,6128 cache-references ( +- 0.63% ) 2,4066,2724 instructions # 1.96 insns per cycle ( +- 0.02% ) 1,2291,8917 cycles ( +- 0.30% ) 0.049770149 seconds time elapsed ( +- 0.43% ) ``` 修改opt.h上面做一個define opt 1 的動作 ```c ###main.c### #if defined(OPT) output = fopen("opt.txt", "a"); #else output = fopen("orig.txt", "a"); ``` calculate裏面也做修改 ```clike= if (!fp) { //fp = fopen("orig.txt", "r"); if (!fp) { printf("ERROR opening input file opt.txt\n"); exit(0); } } ``` ![](https://i.imgur.com/AZ3OcIO.png) ## 修改append&findname ### append ### findname