Move tests from tests/ghc-regress/* to just tests/*
[ghc.git] / testsuite / tests / perf / should_run / T3245.hs
1 {-# LANGUAGE ExistentialQuantification #-}
2
3 -- The second version (count2) took ages with GHC 6.12
4 -- because the typeOf function was not properly memoised
5
6 import Data.Typeable
7 import System.CPUTime
8
9 size :: Int
10 size = 40000 -- This was big enough to take 5 seconds in
11 -- the bad case on my machine.
12
13 data Any = forall a. (Typeable a) => Any a
14
15 int_type, int_list_type :: TypeRep
16 int_type = typeOf (undefined :: Int)
17 int_list_type = typeOf (undefined :: [Int])
18
19 count1 :: [Any] -> Int
20 count1 [] = 0
21 count1 (Any x:xs) = count1 xs + (if typeOf x == int_type then 1 else 0)
22
23 doTime x = do
24 start <- getCPUTime
25 putStr "Result: "
26 print x
27 stop <- getCPUTime
28 putStr "Time(sec): "
29 print (round $ fromIntegral (stop - start) / 1e12)
30 -- The 'round' rounds to an integral number of seconds
31 -- Should be zero if things are working right!
32
33 main = do
34 let list = [MkT | i <- [1..size :: Int]]
35 putStrLn "count1"
36 let x = map Any list
37 doTime $ count1 x
38 doTime $ count1 x
39 doTime $ count1 x
40 putStrLn ""
41 putStrLn "count2"
42 let x = map (Any . (:[])) list
43 doTime $ count1 x
44 doTime $ count1 x
45 doTime $ count1 x
46
47 data T = MkT
48 tcname :: TyCon
49 tcname = mkTyCon "T"
50 instance Typeable T where { typeOf _ = mkTyConApp tcname [] }