天天看點

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(分析器)去找出這個程式在什麼地方很花時間,這段時間程式在做什麼。關于分析器呢,看看 

 吧。

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.

送出兩個檔案:

1 你修改過的 substitute.cpp

2 一個檔案,包含下面這些東西: 

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

2  你注意到的瓶頸。

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

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

為了保證你的正确性,比較一下源代碼改變前後的輸出檔案,因為你的優化并不能改變程式的功能,是以對應的輸出檔案應該是一緻的。你可以用comp這個指令來檢查下這兩個檔案是否一緻。

另外,Unix工具比如 Find和SED還有Awk、Perl這些語言使這種替代很簡單。

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

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:

1 Thread ID: The thread under which the function executed

2 Function: Name of the function

3 Times Called: The number of times the function was called

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

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

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

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

8 Suspended Time: Amount of suspended time (in seconds)

9 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:

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

2 把滑鼠停在 J2

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

4 把這個公式複制到J的這一列

5 用百分比表示右鍵點選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, 照着下面的做:

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

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

8 把E2标記為TotalTime (since cell E2 gives the time spent in main and its callees) by doing the following: 

8 滑鼠放在E2

8 選Insert > Name > Define. (In Excel 2007, choose "Formulas" menu, 

click "Define Name"

)

8 Type TotalTime as the name

8 click OK

9 選中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%

<!---->

繼續閱讀