summaryrefslogtreecommitdiff
path: root/testsuite/tests/perf/should_run/T3245.hs
blob: 806df34879188f60e74d4170d58e3495bcaedddb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
{-# LANGUAGE ExistentialQuantification, DeriveDataTypeable #-}

-- The second version (count2) took ages with GHC 6.12
-- because the typeOf function was not properly memoised

import Data.Typeable
import System.CPUTime

size :: Int
size = 40000    -- This was big enough to take 5 seconds in
                -- the bad case on my machine.

data Any = forall a. (Typeable a) => Any a

int_type, int_list_type :: TypeRep
int_type = typeOf (undefined :: Int)
int_list_type = typeOf (undefined :: [Int])

count1 :: [Any] -> Int
count1 [] = 0
count1 (Any x:xs) = count1 xs + (if typeOf x == int_type then 1 else 0)

doTime x = do
  start <- getCPUTime
  putStr "Result: "
  print x
  stop <- getCPUTime
  putStr "Time(sec): "
  print (round $ fromIntegral (stop - start) / 1e12)
    -- The 'round' rounds to an integral number of seconds
    -- Should be zero if things are working right!

main = do
  let list = [MkT | i <- [1..size  :: Int]]
  putStrLn "count1"
  let x = map Any list
  doTime $ count1 x
  doTime $ count1 x
  doTime $ count1 x
  putStrLn ""
  putStrLn "count2"
  let x = map (Any . (:[])) list
  doTime $ count1 x
  doTime $ count1 x
  doTime $ count1 x

data T = MkT deriving Typeable