何俊逸
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # 2016q3 Homework01 (phonebook) contributed by <`hugikun999`> Reviewed by <`heathcliffYang`> ## Reviewed by `heathcliffYang` 在拿到一份程式碼之後,可以先嘗試把所有的程式碼理解、仔細看Makefile裡到底有那些指令,就可以大略知道這份程式碼的目的,也才不會走很多冤枉路(也是我的切身之痛QQQQQ) 如果時間足夠的話,應該可以再花更多力氣嘗試不同的優化方法,或者嘗試不同的data set ## 開發環境 * OS: Ubuntu16.04 LTS * CPU: i7-4712MQ * RAM: 8G * Disk: 200G ![](https://i.imgur.com/CK2i7GP.png) 安裝完成進入系統後發現File和Trash無法打開,重新開機後這個問題就消失了。另外我安裝的是英文版本,所以參考了網路上的作法安裝了ibus的拼音輸入。 [如何在 Ubuntu 16.04 上使用預設的 ibus 中文輸入法 (及如何使用倉頡萬用字元) ](it.livekn.com/2016/05/ubuntu-1604-ibus.html) ## Github 我以前曾經使用過github上面的source code,但是沒有自己的github帳戶,算是第一次使用。花了一些時間在github的ssh上面,主要是參考官方網站的教學的教學,比較不一樣的是我沒有用到xclip。 * 複製ssh key ``` $ cd .ssh $ vim id_rsm.pub ``` ## Perf ``` $ perf list //perf可以觸發的event $ perf stat //顯示出統計結果 $ perf stat -e cache-misses //記錄cache $misses的情況 $ perf stat --repeat //重覆執行 $ perf top //即時監測正在運行的工作 $ perf record //統計函式級別的運作 $ perf record -g //更詳細記錄每個函式 $ perf record -F //增加取樣頻率 $ perf report //觀看perf record的結果 ``` ## Phonebook ### 遇到的問題 * make plot出來的圖表不會更新(已解決)->記得要刪除orig.txt和opt.txt才會產生正確的圖表 我只有在下載phonebook後的第一次make plot才會有正確圖表產生,之後做的修改可以成功執行並cache-misses有正確的改變,但是圖表仍會出現第一次的結果。 ### 優化方法 記錄一些平常可能會用到的優化想法和這次的目標 * 數據的分離或合併-如果有些數據是經常一起使用,可以將這些數據定意為一個結構體,減少之間的距離。如果一個結構體內有些資料較常用到就可以另建一個結構體給它們。 * 在陣列中,同一行的資料是相臨的,同一列卻不是。 * 減少cache misses * 減少instruction * binary search tree ### function ``` strcasecmp(const char* s1,const char* s2) ``` 說明:比較s1和s2字串的長度,會忽略大小寫。 回傳值: 0 字串相符; >0 s1長度大於s2; <0 s1 長度小於s2 > ``` malloc(size_t size) ``` 說明:配置一個size大小的空間並回傳地址,此函式所配置的記憶體在程式結束前不會歸還,必須使用free()才能將記憶體歸還。 回傳值:動態計憶體的位置。 ``` fgets(char* str, int n, FILE* stream) ``` 說明:將streamn所指向的文件複製到str字串中,n指的是最大被讀取的字符數。 回傳值: successful 回傳str; 若為文件的最後回傳null pointer; error 回傳null pointer ### 未優化 底下是尚未優化執行100次的結果,可以發現cache-misses高達84.843%,instruction也多達2意6千萬次,由此可知從減少cache misses是一個明顯的目標。 ![](https://i.imgur.com/p4hTSmS.png) ### 優化後 1.重新寫一個結構體:原本的entry中存有許多的資料,真正用到的卻只有lastname和pNext,所以我寫了一個other去放其它的資料,可以看到cache misses已經降低了許多,instruction卻沒有明顯的減少。另外由於other沒有malloc()給它,所以append()的時間沒有增加。 ![](https://i.imgur.com/vljXooI.png) ![](https://i.imgur.com/dIkCgUJ.png) ```C typedef struct __PHONE_BOOK_OTHER { 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]; } other; typedef struct __PHONE_BOOK_ENTRY { char lastName[MAX_LAST_NAME_SIZE]; other *other; struct __PHONE_BOOK_ENTRY *pNext; } entry; ``` 如果在append()中將other配置記憶體會造成幾乎所有數據都變更差。 ```C entry *append(char lastName[], entry *e) { e->other = (other *) malloc(sizeof(other)); e->pNext = (entry *) malloc(sizeof(entry)); e = e->pNext; strcpy(e->lastName, lastName); e->pNext = NULL; return e; } ``` ![](https://i.imgur.com/YCBOdFD.png) ![](https://i.imgur.com/emb3HJR.png) 2.Hash function:原本的搜尋方法必須從頭到尾每一筆資料都比對,透過hash function先將資料依照開頭字母做分類,我在entry中多加了一個pOther,在append()時先比對第一個字母如果不同則將指標指向pOther,如此一來便可大幅減少花在findname()的時間。但是會造成在append()的時間小幅上升,另外分類的寫法必須建立在database已經按字母排序的前題下。 ```C typedef struct __PHONE_BOOK_ENTRY { char lastName[MAX_LAST_NAME_SIZE]; other *other; struct __PHONE_BOOK_ENTRY *pNext; struct __PHONE_BOOK_ENTRY *pOther; } entry; ``` ```C static entry *record; static int i=0; /* FILL YOUR OWN IMPLEMENTATION HERE! */ entry *findName(char lastname[], entry *pHead) { /* TODO: implement */ pHead = pHead ->pNext; while (pHead != NULL) { if(lastname[0] == pHead->lastName[0]) { if (strcasecmp(lastname, pHead->lastName) == 0) return pHead; pHead = pHead->pNext; } else pHead = pHead->pOther; } return NULL; } entry *append(char lastName[], entry *e) { // e->other = (other *) malloc(sizeof(other)); if( i == 0 ) { e->pNext = (entry *) malloc(sizeof(entry)); e = e->pNext; strcpy(e->lastName, lastName); e->pNext = NULL; i++; record = e; return e; } else if(e->lastName[0] == lastName[0]) { e->pNext = (entry *) malloc(sizeof(entry)); e = e->pNext; strcpy(e->lastName, lastName); e->pNext = NULL; return e; } else { e = record; e->pOther = (entry *) malloc(sizeof(entry)); e = e->pOther; strcpy(e->lastName, lastName); e->pNext = NULL; record = e; return e; } } ``` ![](https://i.imgur.com/fv2KqWu.png) ![](https://i.imgur.com/3k3RP7K.png)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully