[Haskell-cafe] performance question

Aleksey Khudyakov alexey.skladnoy at gmail.com
Fri Feb 8 22:34:30 CET 2013


On 09.02.2013 01:02, Nicolas Bock wrote:
> Yes, a histogram. The binning code is really a little awkward. I haven't
> gotten used to thinking in terms of inmutable objects yet and this list
> appending is really a pretty bad hack to kind of allow me to increment
> the bin counts. How would one do this more haskellishish?
>
Histogramming is a bit awkward in haskell. If we want to stick to 
immutable data types best choice is to have map bin → number of entries. 
For every new entry select bin and add 1 to its content.

But if we want to store data in array we have to deal with mutable 
state. It's not realistic to copy array on every update. For that case I 
wrote I library histogram-fill[1]. Below is program which does 
approximately same thing.

 > import Data.Histogram.Fill
 > import Data.Histogram      (Histogram)
 >
 > hb :: HBuilder String (Histogram LogBinD Int)
 > hb = forceInt -<< mkSimple (logBinDN 1e-8 10 10) <<- read
 >
 > main :: IO ()
 > main = do
 >   l <- getContents
 >   print $ fillBuilder hb $ lines l

I cheated and used sed to strip unused data. It uses String so it's 
still slower than python.


$ time (python gen.py -N 300 | sed 's/.*=//' | ./printMatrixDecay )
real    0m0.958s
user    0m2.096s
sys     0m0.052s

$ time (python gen.py -N 300 | python printMatrixDecay.py -)
real    0m0.590s
user    0m0.952s
sys     0m0.016s


[1] http://hackage.haskell.org/package/histogram-fill



More information about the Haskell-Cafe mailing list