[Haskell-cafe] How to speed things up
haskell at sleepingsquirrel.org
Thu Apr 28 11:18:23 EDT 2005
Dmitry Vyal wrote:
> Hello everybody.
> I have a long list consisted of a small number (about a dozen) of
> elements repeating in random pattern. I know all the possible elements.
> I need to count number of occurences of each particular element and to
> do i quickly.
> For example
> quick_func Eq a => [a] -> [(a,Int)]
> quick_func [1,2,3,1,2,9,1,9] == [(1,3),(2,2),(3,1),(9,2)]
> According to profiler this function is the bottle-neck in my sluggish
> program so I really need to speed it up.
What's been tried so far? Below is a snippet using arrays. You'd
probably get a faster program with Unboxed arrays and unsafeAccumArray.
> import Data.Array
> main = print $ quick_func [1,2,3,1,2,9,1,9]
> quick_func is = assocs $ accumArray (+) 0 (1,12) [(i, 1) | i<-is]
More information about the Haskell-Cafe