Replaced throw to throwIO where type is IO

Bas van Dijk v.dijk.bas at gmail.com
Sat Sep 25 11:30:20 EDT 2010


On Sat, Sep 25, 2010 at 5:12 PM, Bas van Dijk <v.dijk.bas at gmail.com> wrote:
> I'll see if I can make some Criterion benchmarks to see if it's actually faster.

And it's faster indeed:

------------------------------------------------------------------------------------------------------
import Control.Exception
import Prelude hiding (catch)

import Criterion.Main

throws :: Int -> IO ()
throws 0 = return ()
throws n = throw DivideByZero `catch` \DivideByZero -> throws (n-1)

throwIOs :: Int -> IO ()
throwIOs 0 = return ()
throwIOs n = throwIO DivideByZero `catch` \DivideByZero -> throws (n-1)

n :: Int
n = 1000000

main = defaultMain [ bench "throw"   (throws n)
                   , bench "throwIO" (throwIOs n)
                   ]
------------------------------------------------------------------------------------------------------

$ ghc --make Throwing.hs -O2 -o throwing
[1 of 1] Compiling Main             ( Throwing.hs, Throwing.o )
Linking throwing ...

$ ./throwing
warming up
estimating clock resolution...
mean is 24.38796 us (40001 iterations)
found 1669 outliers among 39999 samples (4.2%)
  1 (2.5e-3%) low severe
  1446 (3.6%) high severe
estimating cost of a clock call...
mean is 1.814127 us (46 iterations)
found 7 outliers among 46 samples (15.2%)
  5 (10.9%) high mild
  2 (4.3%) high severe

benchmarking throw
collecting 100 samples, 1 iterations each, in estimated 21.14298 s
bootstrapping with 100000 resamples
mean: 207.5380 ms, lb 207.1919 ms, ub 207.9817 ms, ci 0.950
std dev: 2.002210 ms, lb 1.645117 ms, ub 2.496120 ms, ci 0.950
found 6 outliers among 100 samples (6.0%)
  5 (5.0%) high mild
  1 (1.0%) high severe
variance introduced by outliers: 0.990%
variance is unaffected by outliers

benchmarking throwIO
collecting 100 samples, 1 iterations each, in estimated 20.68932 s
bootstrapping with 100000 resamples
mean: 211.1240 ms, lb 210.8239 ms, ub 211.5199 ms, ci 0.950
std dev: 1.752349 ms, lb 1.389534 ms, ub 2.311696 ms, ci 0.950
found 9 outliers among 100 samples (9.0%)
  5 (5.0%) high mild
  4 (4.0%) high severe
variance introduced by outliers: 0.990%
variance is unaffected by outliers

Regards,

Bas


More information about the Libraries mailing list