summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMerijnVerstraaten <>2018-02-20 10:51:00 (GMT)
committerhdiff <hdiff@hdiff.luite.com>2018-02-20 10:51:00 (GMT)
commitf909a00392e98b5c5ef34048b42d80f4da0c386a (patch)
tree79e0ce5fb297490d293661ccc133631517aa6f6e
parentab262956277e3546d967e3d437069303ee803291 (diff)
version 2.0HEAD2.0master
-rw-r--r--CHANGELOG.md4
-rw-r--r--LICENSE30
-rw-r--r--System/TimeIt.hs38
-rw-r--r--timeit.cabal52
4 files changed, 102 insertions, 22 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
index 0000000..e8138be
--- /dev/null
+++ b/CHANGELOG.md
@@ -0,0 +1,4 @@
+2.0
+---
+* Generalise `timeIt` and `timeItT` to work for any `MonadIO`
+* Added `timeItShow` and `timeItNamed`
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..d7eabc0
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,30 @@
+Copyright (c) 2009, Lennart Augustsson
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following
+ disclaimer in the documentation and/or other materials provided
+ with the distribution.
+
+ * Neither the name of Merijn Verstraaten nor the names of other
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/System/TimeIt.hs b/System/TimeIt.hs
index da1e499..bb3c715 100644
--- a/System/TimeIt.hs
+++ b/System/TimeIt.hs
@@ -1,20 +1,38 @@
-module System.TimeIt(timeIt, timeItT) where
+module System.TimeIt(timeIt, timeItShow, timeItNamed, timeItT) where
import System.CPUTime
import Text.Printf
+import Control.Monad.IO.Class (MonadIO(liftIO))
--- |Wrap an 'IO' computation so that it prints out the execution time.
-timeIt :: IO a -> IO a
-timeIt ioa = do
+-- | Wrap a 'MonadIO' computation so that it prints out the execution time.
+timeIt :: MonadIO m => m a -> m a
+timeIt = timeItNamed "CPU time"
+
+-- | Like 'timeIt', but uses the 'show' rendering of @a@ as label for the
+-- timing.
+--
+-- @since 2.0
+timeItShow :: (MonadIO m, Show a) => m a -> m a
+timeItShow ioa = do
+ (t, a) <- timeItT ioa
+ liftIO $ printf (show a ++ ": %6.2fs\n") t
+ return a
+
+-- | Like 'timeIt', but uses the 'String' as label for the timing.
+--
+-- @since 2.0
+timeItNamed :: MonadIO m => String -> m a -> m a
+timeItNamed name ioa = do
(t, a) <- timeItT ioa
- printf "CPU time: %6.2fs\n" t
+ liftIO $ printf (name ++ ": %6.2fs\n") t
return a
--- |Wrap an 'IO' computation so that it returns execution time is seconds as well as the real value.
-timeItT :: IO a -> IO (Double, a)
+-- | Wrap a 'MonadIO' computation so that it returns execution time in seconds,
+-- as well as the result value.
+timeItT :: MonadIO m => m a -> m (Double, a)
timeItT ioa = do
- t1 <- getCPUTime
+ t1 <- liftIO getCPUTime
a <- ioa
- t2 <- getCPUTime
+ t2 <- liftIO getCPUTime
let t :: Double
- t = fromIntegral (t2-t1) * 1e-12
+ t = fromIntegral (t2-t1) * 1e-12
return (t, a)
diff --git a/timeit.cabal b/timeit.cabal
index ffdc09a..e4e55ea 100644
--- a/timeit.cabal
+++ b/timeit.cabal
@@ -1,12 +1,40 @@
-Name: timeit
-Version: 1.0.0.0
-License: BSD3
-Author: Lennart Augustsson
-Maintainer: Lennart Augustsson
-Category: System
-Synopsis: Time a computation
-Description: A simple wrapper of an IO computation to show the used CPU time.
-Build-type: Simple
-Build-Depends: base
-Exposed-modules: System.TimeIt
-build-depends: base >= 3 && < 5
+Name: timeit
+Version: 2.0
+
+Homepage: https://github.com/merijn/timeit
+Bug-Reports: https://github.com/merijn/timeit/issues
+
+Author: Lennart Augustsson
+Maintainer: Merijn Verstraaten <merijn@inconsistent.nl>, Lennart Augustsson
+Copyright: Copyright © 2009, Lennart Augustsson
+
+License: BSD3
+License-File: LICENSE
+
+Category: System
+Cabal-Version: >= 1.10
+Build-Type: Simple
+Tested-With: GHC == 7.6.3, GHC == 7.8.4, GHC == 7.10.3, GHC == 8.0.2,
+ GHC == 8.2.2, GHC == 8.4.1, GHC == 8.5.*
+
+Extra-Source-Files: CHANGELOG.md
+
+Synopsis: Time monadic computations with an IO base.
+
+Description:
+ A simple wrapper to show the used CPU time of monadic computation with an
+ IO base.
+
+Library
+ Default-Language: Haskell2010
+ GHC-Options: -Wall
+
+ Exposed-Modules: System.TimeIt
+ Build-Depends: base >= 3 && < 5
+
+ if impl(ghc < 8.0)
+ Build-Depends: transformers >= 0.2 && < 0.6
+
+Source-Repository head
+ Type: git
+ Location: ssh://github.com:merijn/broadcast-chan.git