Personal tools

Timing computation in cycles

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
(how to time in cycles, using rdtsc)
(More a howto than a tutorial)
Line 57: Line 57:
     3058 cycles
     3058 cycles
[[Category:How to]]

Latest revision as of 18:43, 13 February 2007

This page illustrates the use of the 'rdtsc' register on x86 machines to measure the number of cycles a chunk of Haskell code requires. We use lookup of a key in a map as an example.

The Rdtsc library is available from the libraries page.

import System.CPUTime.Rdtsc
import qualified Data.Map as M
import Text.Printf
import System.Environment
list =
    ["Baughn" ,"falconair" ,"Lemmih" ,"Philippa" ,"ToRA" ,"bd_" ,"felipe" ,"levitation[A"
    ,"Plareplane" ,"TSC" ,"bdash" ,"flux__" ,"lisppaste2" ,"Poeir" ,"tuukkah" ,"beelsebob"
    ,"fnordus" ,"lispy" ,"prb" ,"twanvl" ,"benc__" ,"fridim_" ,"liyang" ,"profmakx"
    ,"TwigEther" ,"benja_" ,"gaal" ,"LoganCapaldo" ,"Prozen" ,"uebayasi" ,"Betovsky"
    ,"gdsx" ,"lokadin" ,"psnl" ,"Ugarte" ,"bitshifter" ,"GeoBesh" ,"Lor" ,"pstickne" ,"ulfdoz"
    ,"bobwhoops" ,"george--" ,"loud-" ,"psykotic" ,"vegai" ,"bohanlon" ,"giksos" ,"lucca"
    ,"ptolomy" ,"vegaiW" ,"bos" ,"glguy" ,"Lunar^" ,"Pupeno" ,"Vq^" ,"bos31337" ,"gmh33"
    ,"Lunchy" ,"PupenoR" ,"waern" ,"Botje" ,"grumpy_old_one" ,"magagr" ,"pyronicide"
    ,"Wallbraker" ,"boulez" ,"GueNz" ,"mahogny" ,"quazaway" ,"wchogg" ,"cajole" ,"guillaumh"
    ,"makinen" ,"quetzal" ,"wilx`" ,"Cale" ,"gvdm_other" ,"MarcWeber" ,"qwr" ,"woggle"
    ,"calvins_" ,"Hirvinen" ,"maskd" ,"rafl" ,"wolverian" ,"cameron" ,"ibid" ,"mathrick"
    ,"ramkrsna" ,"xerox" ,"carp_" ,"Igloo" ,"mathrick_" ,"ramza3" ,"Xgc" ,"clanehin" ,"ikaros"
    ,"mattam" ,"rashakil_" ,"xian" ,"ClaudiusMaximus" ,"integral" ,"matthew-_" ,"ray"
    ,"xinming" ,"clog" ,"Jaak" ,"mauke" ,"rc-1" ,"xpika" ,"cmeme" ,"jbalint" ,"mbishop"
    ,"rds" ,"yaarg" ,"Codex_" ,"jcreigh" ,"metaperl" ,"reppie" ,"yosemite" ,"cods" ,"jdev"
    ,"Mitar" ,"resiak" ,"Z4rd0Z" ,"cognominal" ,"jgrimes" ,"mlh" ,"retybok" ,"zamez" ,"cpfr"
    ,"JKnecht" ,"moconnor" ,"rey_" ,"zeuxis" ,"ctkrohn" ,"jlouis" ,"monochrom" ,"saccade"
    ,"ziggurat" ,"daniel_larsson" ,"jmg_" ,"moonlite" ,"Saizan" ,"|shad0w|" ,"dany2k" ,"jmob"
    ,"mornfall" ,"SamB" ,"Daveman" ,"joene_" ,"mr_ank" ,"SamB_XP" ,"dblog" ,"johs_" ,"ms_"
    ,"scw" ,"dcoutts" ,"jrockway" ,"Muad_Dib" ,"shapr" ]
main = do
    [v] <- getArgs
    -- force evaluation (don't want to time evaluation of lazy structures)
    length list `seq` return ()
    let m = M.fromList (zip list [1..])
    M.size m `seq` return ()
    -- do the lookup
    t <- rdtsc
    k <- M.lookup v m
    u <- rdtsc
    print k
    printf "%d cycles\n" (fromIntegral (u - t) :: Int)

Running this:

   $ ./A shapr
   3058 cycles