<commit_before>
module Development.Shake.Timing(resetTimings, addTiming, printTimings) where

import Control.Arrow
import Data.IORef
import Data.Time
import System.IO.Unsafe
import Numeric


{-# NOINLINE timings #-}
timings :: IORef [(UTCTime, String)] -- number of times called, newest first
timings = unsafePerformIO $ newIORef []


resetTimings :: IO ()
resetTimings = do
    now <- getCurrentTime
    writeIORef timings [(now, "Start")]


-- | Print all withTiming information and clear the information.
printTimings :: IO ()
printTimings = do
    now <- getCurrentTime
    old <- atomicModifyIORef timings $ \ts -> ([(now, "Start")], ts)
    putStr $ unlines $ showTimings now $ reverse old


addTiming :: String -> IO ()
addTiming msg = do
    now <- getCurrentTime
    atomicModifyIORef timings $ \ts -> ((now,msg):ts, ())


showTimings :: UTCTime -> [(UTCTime, String)] -> [String]
showTimings stop times = map (\(a,b) -> a ++ " " ++ showDP 3 b) xs
    where xs = [ (name,fromRational $ toRational $ stop `diffUTCTime` start)
               | ((start, name), stop) <- zip times $ map fst (drop 1 times) ++ [stop]]


showDP :: Int -> Double -> String
showDP n x = a ++ "." ++ b ++ replicate (n - length b) '0'
    where (a,b) = second (drop 1) $ break (== '.') $ showFFloat (Just 2) x ""<commit_msg>Print timings in a more informative way<commit_after>
module Development.Shake.Timing(resetTimings, addTiming, printTimings) where

import Control.Arrow
import Data.IORef
import Data.Time
import System.IO.Unsafe
import Numeric


{-# NOINLINE timings #-}
timings :: IORef [(UTCTime, String)] -- number of times called, newest first
timings = unsafePerformIO $ newIORef []


resetTimings :: IO ()
resetTimings = do
    now <- getCurrentTime
    writeIORef timings [(now, "Start")]


-- | Print all withTiming information and clear the information.
printTimings :: IO ()
printTimings = do
    now <- getCurrentTime
    old <- atomicModifyIORef timings $ \ts -> ([(now, "Start")], ts)
    putStr $ unlines $ showTimings now $ reverse old


addTiming :: String -> IO ()
addTiming msg = do
    now <- getCurrentTime
    atomicModifyIORef timings $ \ts -> ((now,msg):ts, ())


showTimings :: UTCTime -> [(UTCTime, String)] -> [String]
showTimings _ [] = []
showTimings stop times = showGap [(a ++ "  ", showDP 3 b ++ "s  " ++ showPerc b ++ "  " ++ progress b) | (a,b) <- xs]
    where
        showPerc x = let s = show $ floor $ x * 100 / sm in replicate (3 - length s) ' ' ++ s ++ "%"
        progress x = let i = floor $ x * 25 / mx in replicate i '=' ++ replicate (25-i) ' '
        mx = maximum $ map snd xs
        sm = sum $ map snd xs
        xs = [ (name, fromRational $ toRational $ stop `diffUTCTime` start)
             | ((start, name), stop) <- zip times $ map fst (drop 1 times) ++ [stop]]


showGap :: [(String,String)] -> [String]
showGap xs = [a ++ replicate (n - length a - length b) ' ' ++ b | (a,b) <- xs]
    where n = maximum [length a + length b | (a,b) <- xs]


showDP :: Int -> Double -> String
showDP n x = a ++ "." ++ b ++ replicate (n - length b) '0'
    where (a,b) = second (drop 1) $ break (== '.') $ showFFloat (Just 2) x ""
{'fraction_non_alphanumeric': 0.1530903328050713, 'fraction_numerical': 0.005705229793977813, 'mean_word_length': 3.0775193798449614, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7457195', 'n_tokens_mistral': 1109, 'n_tokens_neox': 1036, 'n_words': 349}