ssd6 ex4

只有題目的簡略翻譯  我還沒做 有的句子不會翻譯

Exercise 4

Profiling Lab: Understanding Program Performance

這個練習呢,將給一個程序讓你去優化,雖然會有很多可以優化的地方,不過你應該注意那些能更顯著地減少運行時間的優化。

這個程序呢,可以perform string substitutions on a list of files。這個程序的輸入是specified on the command line的。舉個例子:

substitute.exe replacements.txt file1.txt file2.txt ... fileN.txt

例子中這個叫做replacements.txt的文件,包含a list of substitutions to perform。每個Each string substitution is specified in 3 lines:第一行是搜索的關鍵詞(第一個字符串),下一行是替代詞(第二個字符串),然後空一行。(感覺就是office裏的替代)如下所示:

the

that

 

his

Her

 

command line上剩下的文件是要被修改的文件。這個程序讀取每個文件,每一次performs the substitutions one line,然後寫文件。爲了實現替代,這個程序在文件裏尋找第一個字符串的精確匹配。匹配的字符將被第二個字符串所替代。然後將繼續在這個文件裏搜索第一個字符串的匹配。This match is performed on the new state of the file,所以它可能包含之前替代過的字符。實際上,如果替代詞包含搜索詞,那麼這個程序將進入。如果不再找得到匹配,這個程序將進行到替代文件的下一行。

一個好的編程風格將避免一次讀進整個文件,因爲文件可能會很大。這個練習裏呢,你則可以假定總有足夠的內存去讀取整個文件。

要運行 substitute.Exe的話,把它移到任意一個目錄下,在命令行裏輸入:

substitute.exe replace.txt call.cpp compiler.cpp driver.cpp getopt.cpp jnk.cpp mach.cpp  math.cpp semantics.cpp test.cpp

Profiling substitute.exe

Once everything is running (check the test files to see that the substitutions were applied), you are ready to start optimizing. The first step will be to use a profiler to find out where the program is spending its time and what it is doing with that time. Consult Appendix A Profiler Customized for SSD6 for more information about the profiler.

當一切都在運行的時候就可以試着去優化了。第一步呢,先用一個profiler(分析器)去找出這個程序在什麼地方很花時間,這段時間程序在做什麼。關於分析器呢,看看 Appendix A Profiler Customized for SSD6 吧。

You should make a new version of substitute.exe and demonstrate, using profiling output, that it runs faster. You should be able to obtain at least a factor of 2 speedup (old run time divided by new run time). You do not have to use Microsoft Foundation Class objects, but given that these are well written and probably correct, you should only replace code that is doing unnecessary work as reflected in profiler measurements.

你應該發佈一個新的substitute.exe,並且用分析器的輸出結果證實你發佈的東東的確跑的要快一些。你應該能夠獲得至少一個 factor of 2 speedup (old run time divided by new run time)。你不一定要用 Microsoft Foundation Class objects,不過既然它們都是well written而且基本上都是正確的,你應該只需要修改那些做無用功的代碼,as reflected in profiler measurements.

提交兩個文件:

你修改過的 substitute.cpp

一個文件,包含下面這些東西: 

1  一個清楚簡明的優化前你所觀察到的現象的描述。這個應該是由一個分析器的輸出來證實一下子。

2  你注意到的瓶頸。

3  爲了address這些瓶頸,你做了什麼,然後你觀察到了怎樣的提升(還是要有empirical evidence)。

4  如果你決定繼續下一塊最能優化的代碼,那麼指出要優化的是什麼。並且說明你爲什麼沒曾試着去優化這一塊。

爲了保證你的正確性,比較一下源代碼改變前後的輸出文件,因爲你的優化並不能改變程序的功能,因此對應的輸出文件應該是一致的。你可以用comp這個命令來檢查下這兩個文件是否一致。

另外,Unix工具比如 FindSED還有AwkPerl這些語言使這種替代很簡單。

 

=======================

 

Profiler Customized for SSD6

The Visual C++.Net software bundle does not include a code profiler. However, the bundle includes a set of API (along with some examples) that tool developers can use to build a profiler. iCarnegie has customized the profiler code provided by Microsoft to suit the purposes of this course.

VC++並不包含一個代碼分析器,但是包含一組API(還有例子),用這些就能夠做個分析器出來。iCarnegie就做了一個。

解壓那個下載的zip包,打開命令行,運行EnableProfiler.Bat。這個批處理文件將在你的註冊表里加點東西。然後在同一個命令行裏運行你的程序。將有一個output.Log記錄分析的數據。用excel打開output.log as a csv (comma separated values)

Note: In Microsoft Excel 2007, choose the "Data" menu, and click on "From Text". Choose Semicolon as the delimiter.

There are nine columns in the csv file:

Thread ID: The thread under which the function executed

Function: Name of the function

Times Called: The number of times the function was called

Exclusive Time: Amount of time (in seconds) spent in the function excluding time spent in its callees, Suspended Time and Profiler Time

Callee Exclusive Time: Amount of time (in seconds) spent in the function and its callees (children) excluding Suspended Time and Profiler Time

Inclusive Time: Amount of time (in seconds) spent in the function including Callee Time, Suspended Time and Profiler Time

Callee Time: Amount of time (in seconds) spent in the callees (including Suspended Time and Profiler Time spent under the callees)

Suspended Time: Amount of suspended time (in seconds)

Profiler Time: Amount of time spent by profiler (in seconds)

Clean the profiler data file: Sometimes you will see some functions such as static void System.AppDomain::OnExitProcess( ) that runs under a different Thread ID. You might see that function included as part of your output.log file (typically as the last few rows). If you see those function listed, delete all rows corresponding to that thread. Also, delete all rows that are empty.

要計算the fraction of time spent in each function (as a percentage of the total time spent across all functions) in column J, perform the following sequence of actions:

在J1敲一個恰當的名字(比如Function%)

把鼠標停在 J2

輸入這個公式: =D2/SUM(D:D)

把這個公式複製到J的這一列

用百分比表示右鍵點擊J列的比率,然後選擇 "Format Cells...", "Number" tab, 然後specify "Percentage" as the Category.

要計算the fraction of time spent by each function and its callees (as a percentage of the total time spent by the program) in column K, 照着下面的做:

K1敲個恰當的名字(比如Function+Child%).

降序排列(Callee Exclusive Time). 這會讓main function跑到第一個.

E2標記爲TotalTime (since cell E2 gives the time spent in main and its callees) by doing the following: 

鼠標放在E2

Insert > Name > Define. (In Excel 2007, choose "Formulas" menu, click "Define Name")

Type TotalTime as the name

click OK

選中K2

10 輸入 =E2/TotalTime

11 把上面那公式複製到K列的每一格

12 在K列上右鍵,用百分數表示比率。然後選 "Format Cells...",  "Number" tab, 然後specify "Percentage" as the Category.

Now you can examine where your program spends significant amounts of time by sorting the data by Function% or Function+Child%

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章