20200917-3 白名單
此作業要求詳見[https://edu.cnblogs.com/campus/nenu/2020Fall/homework/11207]
作業0(5分)
修改create.cpp檔案,改成由命令列引數確定生成的資料的資料量。修改readme.md的對應部分。(要求貼出修改之後的程式碼和read.md。)
//create.cpp #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; int main(int argc, char* argv[]) { int num = atoi(argv[1]); srand((unsigned)time(NULL)); for (int i = 0; i < num; i++) { cout << rand(); if(i!=num-1) cout << "\n"; } cout << endl; return 0; }
ReadMe.md |
1,專案名稱:白名單 模組構成: (1)create建立測試用例 (2)brute 通過白名單過濾資料 2,安裝環境 Visual Studio 2015以上(包含C++,C#語言模組) 3,簡要使用說明 (1)下載並安裝vs; (2)配置環境變數; (3)編譯create.cpp檔案; (4)執行“create 10>whitelist"生成白名單檔案whitelist (5)執行“create 1000>q”生成測試檔案q; (6)編譯brute.cs檔案; (7)執行“brute -w whitelist < q > output” |
作業1(10分)
對上面兩段老楊寫的程式碼任選其一進行profile,觀察現象(要求有截圖記錄)。
我選擇對brute程式C#版本進行profile。
首先使用VS2019自帶的效能探查器進行CPU效能分析。
接著等待程式執行完成,執行結果如下:
很容易的發現System.Console.WriteLine(int)佔用了很大一部分CPU導致執行緩慢。
作業2(10分)
以biggerwhitelist和biggerq作為輸入,對作業1中選擇的程式碼再次進行profile,找到程式碼執行最“慢”的地方,截圖為證並文字說明。
對作業1中修改資料後再次profile。
發現System.Console.WriteLine(int)佔用了程式80%的CPU資源。
作業3(10分)
根據作業2找到的最慢的地方,優化作業1中你選擇的程式碼,在保證輸出結果正確的前提下,減少老楊程式執行的時間。(優化後的程式碼需要你提交到git上,作為教師的判斷依據。優化後的程式的名字應該是better.cpp或者better.cs。)
專案地址:https://thinkget.coding.net/public/wf/whitelist/git(https://e.coding.net/thinkget/wf/whitelist.git)
控制檯輸出再寫入檔案是一件很“費力”的事情,所以直接不使用控制檯輸出,把結果寫到陣列中用檔案輸出可以大幅度減少時間。
using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace profiletest { class Better { static void Main(string[] args) { DateTime beforDT = System.DateTime.Now; if (args.Count() < 1) return; string path = args[1]; string[] sm = File.ReadAllLines(path); int[] p = new int[sm.Length]; for (int i = 0; i < sm.Length; i++) { p[i] = Convert.ToInt32(sm[i]); } //Console.WriteLine(sm.Length); string[] sj = new string[10000000];//修改:新增存放的陣列 int sjflag = 0; int a = Console.Read(); string num = ""; for (int i = 0; i < 10000000; i++) { int tempnum = Convert.ToInt32(Console.ReadLine()); if (find(tempnum, p) == -1) sj[sjflag++] = num;//修改:使用陣列儲存資料 } sj = sj.Where(s => !string.IsNullOrEmpty(s)).ToArray(); File.WriteAllLines("output", sj, Encoding.UTF8);//修改:將資料存放到output檔案 DateTime afterDT = System.DateTime.Now; TimeSpan ts = afterDT.Subtract(beforDT); Console.WriteLine("DateTime: {0}ms.", ts.TotalMilliseconds); } static int find(int key, int[] array) { for (int j = 0; j < 10; j++) { if (key == array[j]) return key; } return -1; } } }
作業4(5分)
對作業3優化後的程式碼進行profile,結果與作業2的結果做對比。畫表格並文字說明。
對作業3進行profile,所有語句佔用比很均勻,由於要求的限制,輸入方面暫時沒有好的優化方案。對於型別轉換,由於水平不高也無法進行優化。
時間方面從原來的十幾分鍾到現在的幾秒鐘,提升98%以上
做業5(5分)
你覺得老楊的文件(readme),註釋和程式碼風格又哪些問題,該如何改進?
readme方面需要寫的更加詳細一點,如程式名稱,功能的介紹等,還要加上執行環境,方便別人進行安裝除錯。操作步驟也可以再寫詳細一點,最後還要說明一些會遇到的問題等等。