title: Slowdown by removing useless code (Project Euler 23)
body: <p>I'm trying to optimize my old code from Project Euler #23 and noticed some strange slowdown while removing useless comparisons in a function for list merging.</p>
  
  <p>My code:</p>
  
  <pre><code>import Data.List
  import Debug.Trace
  
  limit = 28123
  
  -- sum of all integers from 1 to n
  summe :: Int -&gt; Int
  summe n = div (n*(n+1)) 2
  
  -- all divisors of x excluding itself
  divisors :: Int -&gt; [Int]
  divisors x = l1 ++ [x `div` z | z &lt;- l1, z*z /= x, z /= 1]
                 where m = floor $ sqrt $ fromIntegral x
                       l1 = [y | y &lt;- [1..m] , mod x y == 0]
  
  -- list of all abundant numbers
  liste :: [Int]
  liste = [x | x &lt;- [12..limit] , x &lt; sum (divisors x)]
  
  -- nested list with sums of abundent numbers
  sumliste :: [[Int]]
  sumliste = [[x+y | x &lt;- takeWhile (&lt;=y) liste, x + y &lt;= limit] | y &lt;- liste]
  
  -- reduced list
  rsl :: [[Int]] -&gt; [Int]
  rsl (hl:[]) = hl
  rsl (hl:l) = mergelists hl (rsl l)
  
  -- build a sorted union of two sorted lists
  mergelists :: [Int] -&gt; [Int] -&gt; [Int]
  mergelists [] [] = []
  mergelists [] b = b
  mergelists a [] = a
  mergelists as@(a:at) bs@(b:bt)
  --  | a == b = a : mergelists at bt
  --  | a &gt; b = b : mergelists as bt
  --  | a &lt; b = a : mergelists at bs
    | a == b = if a == hl1
                 then trace "1" l1
                 else a : l1
    | a &gt; b = if b == hl2
                then trace "2" l2
                else b : l2
    | a &lt; b = if a == hl3
                then trace "3" l3
                else a : l3
                  where l1 = mergelists at bt
                        hl1 = if null l1 then a + 1 else head l1
                        l2 = mergelists as bt
                        hl2 = head l2
                        l3 = mergelists at bs
                        hl3 = head l3
  
  -- build the sum of target numbers by subtracting sum of nontarget numbers from all numbers
  main = print $ (summe limit) - (sum $ rsl sumliste)
  </code></pre>
  
  <p>My problem is the function <code>mergelists</code>. The body of this functions contains some useless if clauses (as can be seen by the missing trace output) and could be refactored to the three commented lines. The problem with this is an increase of execution time from 3.4s to 5.8s what I can't understand.</p>
  
  <p>Why is the shorter code slower?</p>