Created
January 9, 2013 13:27
-
-
Save xjia1/4493103 to your computer and use it in GitHub Desktop.
Thread creation benchmark in Haskell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Concurrent | |
import Control.Concurrent.Chan | |
import System.Environment | |
import System.Exit | |
import System.Time | |
main = getArgs >>= start | |
start [s1,s2] = do | |
stopCh <- newChan | |
startTime <- getClockTime | |
createThreads 0 [] (read s1 :: Int) (read s2 :: Int) startTime stopCh | |
createThreads rounds chans total step lastTime stopCh | |
| rounds == total = exitWith ExitSuccess | |
| length chans == step = waitThreads rounds (reverse chans) total step lastTime stopCh | |
| otherwise = do ch <- newChan | |
forkIO (thread ch stopCh) | |
createThreads rounds (ch:chans) total step lastTime stopCh | |
waitThreads rounds [] total step lastTime stopCh = do | |
currentTime <- getClockTime | |
putStrLn $ show $ diffToMillisec $ diffClockTimes currentTime lastTime | |
createThreads (rounds + 1) [] total step currentTime stopCh | |
waitThreads rounds (ch:chans) total step lastTime stopCh = | |
readChan ch >>= \_ -> waitThreads rounds chans total step lastTime stopCh | |
thread ch stopCh = writeChan ch 1 >> readChan stopCh >>= \_ -> putStrLn "WTF?!" | |
diffToMillisec diff = toInteger (seconds * 1000) + (p `div` 1000000000) | |
where h = tdHour diff | |
m = tdMin diff | |
s = tdSec diff | |
p = tdPicosec diff | |
seconds = (h * 60 + m) * 60 + s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment