On Dec 15, 7:30 pm, Maciej Piechotka <[email protected]> wrote: > On Tue, 2009-12-15 at 09:52 -0800, Johann Höchtl wrote: > > Hello, > > > I'm still to Haskell, and after I read through > >http://users.aber.ac.uk/afc/stricthaskell.html#seq > > > I thought, that these tow fragments after term rewriting are really > > the same: > > > myLength :: [a] -> Integer > > myLength xs = len xs 0 > > where len [] l = l > > len (x:xs) l = l `seq` len xs (l+1) > > > main = print $ myLength [1..10000000] > > > -- vs. > > > myLength :: [a] -> Integer > > myLength xs = len xs 0 > > where len [] l = l > > len (x:xs) l = len xs $! (l+1) > > > main = print $ myLength [1..10000000] > > > main = print $ myLength [1..10000000] > > > But the first expression evaluates more then twice as fast as the > > second one. Tested on GHC 6.10.4 and Windows XP, dual core (for what > > it's worth) > > > It's onhttp://moonpatio.com/fastcgi/hpaste.fcgi/view?id=5321#a5321 > > btw. > > > I can't see the difference, especially as $! is expressed in terms of > > seq > > The second one is IMGO: > myLength :: [a] -> Integer > myLength xs = len xs 0 > where len [] l = l > len (x:xs) l = let l' = l+1 > in l' `seq` len xs l' > > So in thew first + is not forced to be evaluated. >
Please describe for me as a beginner, why there _is_ a difference: 1. does len (x:xs) l = l `seq` len xs (l+1) vs. len xs $! (l+1) expand into sthg. different? 2. Do I understand right, that the first expression "should" actually be slower but (for what reason ever in an unoptimized case isn't? 3. The function is anotated with Integer. Why is suddenly Int of importance? (4. When optimizing is switched on, the second expession executes faster; as such I assume, that there is a difference between these two statements) Thank you! > My results (ghc 6.12.1, Core 2 Duo 2.8 GHz, Linux 2.6.32, Gentoo): > > Not Optimized & not compiled: > First: 12.47 secs, 1530911440 bytes > Second: 17.40 secs, 1929614816 bytes > Optimized & compiled: > First: 1.24 secs, 966280832 bytes > Second: 1.11 secs, 966277152 bytes > > Repeating gave similar results - first being better w/out > optimalization as 1.2:1.7 and second being better with optimalizations (-O). > > Why the first one is better unoptimalized? > > Regards > > _______________________________________________ > Haskell-Cafe mailing list > [email protected]http://www.haskell.org/mailman/listinfo/haskell-cafe _______________________________________________ Haskell-Cafe mailing list [email protected] http://www.haskell.org/mailman/listinfo/haskell-cafe
