title: How to improve the zipWith's performance in Haskell
body: <p>I've written a code for Clustering with Data.Clustering.Hierarchical, but it's slow.</p>
  
  <p>I tried profiling and changing some of the code, but I don't know why <code>zipWith</code> is taking so many time? (even if, I change list to vector.)</p>
  
  <pre><code>import Data.Clustering.Hierarchical
  import qualified Data.Vector.Primitive as DV
  import System.Random
  import Control.Monad
  
  
  main = do
      vectorList &lt;- genTestdata
      let cluster = dendrogram SingleLinkage vectorList getVectorDistance  
      putStrLn $ show cluster
  
  genZero x 
      | x&lt;5 = x
      |otherwise = 0
  
  genVector::IO (DV.Vector Int)
  genVector = do
      listRandom &lt;- mapM (\x -&gt; randomRIO (1,30) ) [1..20]
      let intOut = DV.fromList $ map genZero listRandom
      return intOut
  
  genTestdata = do 
      r &lt;- sequence  $ map (\x -&gt; liftM (\y -&gt; (x,y)) genVector) [1..1000]
      return r
  
  getExp2 v1 v2 = d*d
      where
          d = v1 - v2
  
  getExp v1 v2
      | v1 == v2 = 0
      | otherwise = getExp2 v1 v2
  
  tfoldl  d = DV.foldl1' (+) d
  
  changeDataType:: Int -&gt; Double
  changeDataType d = fromIntegral d
  
  getVectorDistance::(a,DV.Vector Int)-&gt;(a, DV.Vector Int )-&gt;Double
  getVectorDistance v1 v2 = fromIntegral $ tfoldl dat
      where
          l1 = snd v1
          l2 = snd v2
          dat = DV.zipWith getExp l1 l2
  </code></pre>
  
  <p>To build it, use: <code>ghc -prof -fprof-auto -rtsopts -O2 log_cluster.hs</code></p>
  
  <p>Run with <code>log_cluster.exe +RTS -p</code></p>
  
  <p>The profiling result on my machine follows -- note the result for <code>getVectorDistance.dat</code>:</p>
  
  <pre><code>&gt; log_cluster.exe +RTS -p -RTS
  
  total time  =        8.43 secs   (8433 ticks @ 1000 us, 1 processor)
  total alloc = 1,614,252,224 bytes  (excludes profiling overheads)
  
  COST CENTRE            MODULE  %time %alloc
  getVectorDistance.dat  Main     49.4   37.8  &lt;------
  tfoldl                 Main      5.7    0.0
  getExp                 Main      4.5    0.0
  getExp2                Main      0.5    1.5
  </code></pre>