[Haskell-beginners] How to display a time difference?
daniel.is.fischer at web.de
Wed Mar 18 06:41:56 EDT 2009
Am Mittwoch, 18. März 2009 11:28 schrieb Colin Paul Adams:
> >>>>> "Yitzchak" == Yitzchak Gale <gale at sefer.org> writes:
> Yitzchak> Colin Paul Adams wrote:
> >> The code of the following routine is intended to indicate how
> >> long it takes for the computer to make a move. However the time
> >> is printed (as very close to zero) long before the move is
> >> made.
> Yitzchak> You are writing a thunk to the IORef. It only gets
> Yitzchak> computed later on when you read the value.
> Yitzchak> Try using:
> >> readIORef game_state_ior >>= evaluate
> >> . update_interactive_from_move move >>= writeIORef
> >> game_state_ior
> Yitzchak> evaluate is in Control.Exception.
> Still no joy with:
> play_move :: IORef Game_state -> IO ()
> play_move game_state_ior = do
> (_, state, _) <- readIORef game_state_ior
> putStr "Playing AI: "
> start_time <- getCurrentTime
> let move = recommended_move state
> readIORef game_state_ior >>=
> evaluate . update_interactive_from_move move >>=
> writeIORef game_state_ior
> end_time <- getCurrentTime
> putStrLn $ show $ (diffUTCTime end_time start_time)
You would have to completely evaluate move. Depending on what type move is,
that might require much more than what seq or evaluate do.
let move = ...
(rnf move) `seq` mofifyIORef (...)
More information about the Beginners