Skip to content

Program that runs slower with optimizations on

This program runs significantly slower with optimization than without.

I need two modules to manifest this bug.

Is this a duplicate of 917/1945? Using -fno-full-laziness does not mitigate the problem.

This seems to be the opposite of 1945. Without optimizations, there is a long delay and then all 10 results print at once. With optimizations, there is a shorter delay between each print statement.

module Main

 (main)

>  where

import NaiveFib
import Control.Monad

main :: IO ()
main = replicateM_ 10 (printFib 37)

module NaiveFib

 (printFib,naiveFib)

>  where

printFib :: Integer -\> IO ()
printFib = print . naiveFib

naiveFib :: Integer -\> Integer
naiveFib 0 = 0
naiveFib 1 = 1
naiveFib n = naiveFib (n-1) + naiveFib (n-2)

lane\@wired:\~/test/optimizer-bug$ make main-O0
ghc-6.8.1 -O0 --make Main.hs -o main-O0
\[1 of 2\] Compiling NaiveFib ( NaiveFib.hs, NaiveFib.o )
\[2 of 2\] Compiling Main ( Main.hs, Main.o )
Linking main-O0 ...
lane\@wired:\~/test/optimizer-bug$ time ./main-O0
24157817
24157817
24157817
24157817
24157817
24157817
24157817
24157817
24157817
24157817

real 0m34.491s
user 0m25.982s
sys 0m0.564s

lane\@wired:\~/test/optimizer-bug$ make main-O2
ghc-6.8.1 -O2 --make Main.hs -o main-O2
\[1 of 2\] Compiling NaiveFib ( NaiveFib.hs, NaiveFib.o )
\[2 of 2\] Compiling Main ( Main.hs, Main.o )
Linking main-O2 ...
lane\@wired:\~/test/optimizer-bug$ time ./main-O2
24157817
24157817
24157817
24157817
24157817
24157817
24157817
24157817
24157817
24157817

real 1m50.331s
user 1m23.641s
sys 0m1.008s
Trac metadata
Trac field Value
Version 6.8.1
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
Edited by Ben Gamari
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information