Docsity

GHC Profiling: Analyzing Performance Issues in Haskell Quicksort Algorithm, Exams of Computer Science

Partial preview of the text

Download GHC Profiling: Analyzing Performance Issues in Haskell Quicksort Algorithm and more Exams Computer Science in PDF only on Docsity!

CS510APProfiling in GHC

GHC Profiling• The GHC compiler has extensive supportfor profiling.• Links to documentation– http://www.haskell.org/ghc/docs/latest/html/users_guide/profiling.html• GHC can perform two kinds of profiling– time profiling– space profiling

Lets try it.• Our goal is to write a quik-sort routine• We expect it to have n-log(n) behavior• We write it in Haskell (see next page)^2 • It appears to behave like an nalgorithm• We want to know why?

First some tests test1 = check "null list"(quik []) ([]::[Int])test2 = check "one list"(quik [3]) [3]test3 = check "1 to 10"(quik [1,9,2,8,3,7,4,6,5,10])[1,2,3,4,5,6,7,8,9,10]tests = [test1,test2,test3]test = runTestTT (TestList tests)

quik^ [] = []quik^ [x] = [x]quik^ (x:xs) = merge (merge small [x]) largewhere small = quik

(smaller x xs)large = quik (larger x xs)

main^ = do { let l = last(quik xs); putStrLn("The last element of the sort is: “++show l)}

Run the tests• Type :? for help• Main> test• Cases: 3 Tried: 3 Errors: 0 Failures: 0• Main>

  • • ghc -prof -auto-all -id:/work/sheard/Lib/Hunit/ --make sort1.hs • $ ./main.exe +RTS -p -RTS• The last element of the sort is:

Tue May 23 09:03 2006 Time and Allocation Profiling Report

(Final) main.exe +RTS -p -RTStotal time^ =^ 0.80 secs^ (40 ticks @ 20 ms)total alloc =^ 61,072,232 bytes^ (excludes profiling overheads)COST CENTRE MODULE^

%time %alloc smaller^

Main^ 50.0^ 0. larger^

Main^ 47.5^ 98. main^

Main^ 2.5^ 0. COST CENTRE^ MODULE^ no.^

entries^ %time %alloc^ %time %alloc MAIN^ MAIN^1

0 0.0^ 0.0^ 100.0^ 100.

CAF^ Main^162

7 0.0^ 0.0^ 100.0^ 100.

merge^ Main^172

2 0.0^ 0.0^ 0.0^ 0.

xs^ Main^170

1 0.0^ 0.3^ 0.0^ 0.

n^ Main^171

1 0.0^ 0.0^ 0.0^ 0.

main^ Main^168

1 2.5^ 0.0^ 100.0^ 99.

quik^ Main^169

8001 0.0^ 0.5^ 97.5^ 99.

larger^ Main^175

47.5^ 98.5^ 47.5^ 98.

smaller^ Main^174

50.0^ 0.2^ 50.0^ 0.

merge^ Main^173

19990 0.0^ 0.5^ 0.0^ 0.

CAF^ System.IO^117

1 0.0^ 0.0^ 0.0^ 0.

CAF^ GHC.Handle^98

4 0.0^ 0.0^ 0.0^ 0.

GHC profiler overview• Compile with “–prof” to instrument the code(1) ghc –prof Main.hs –o Main• Run with cues to the runtime system to generatea heap profile (.hp)(2) ./Main +RTS^ {options} • Convert the heap profile to Postscript (.ps) andview it(3) hp2ps Main.hp(4) gv Main.ps

GHC profiler options• One breakdownoption (see right)• And one option torestrict the profile to aspecific part of theprogram (see GHCUser’s Guide online)

./Main +RTS^ {options} BreakdownOptionBy function-hcBy module-hmBy type-hy By thunk-hbbehavior

Program 1 mean :: [Float] -> Floatmean xs = sum xs / length xsmain = print (mean [0.0 .. 1000000]) • xs is lazily computed, must be stored untilboth sum and length^ finish.• Program runs out of memory and crashes!

Program 1 (by type)

Program 2 mean :: [Float] -> Floatmean xs = loop 0 0 xs whereloop sum len [] = sum / lenloop sum len (x:xs) =loop (sum+x) (len+1) xsmain = print (mean [0.0 .. 1000000]) • Now we only traverse the list once.• But STILL runs out of memory and crashes!

Program 2 (by type)