Re: [Haskell-cafe] Request for optimizing help.

2012-06-25 Thread Johannes Waldmann
First, why do you think your code is non-optimal?

you don't show your main program, so we don't know what you're measuring.

Just by looking at some types (and not analysing the algorithm):

11 data FilterState a = FilterState {
14   , taps :: [a] -- current delay tap stored values

the State really is taps only. (as  and  bs don't change ?)
so taps should be separate.

25         newTaps = wk : init (taps s)

init could be expensive (linear in the list length)
(but you have linear cost elsewhere, so maybe it does not hurt)

Use some different sequence type (instead of list)?
It seems you actually want a strict sequence (while (:) is lazy)
of strict values.

31 runFilter :: Kernel a - FilterState a - [a] - IO ([a], FilterState a)

why IO? there's no IO in the implementation. it looks like a simple fold.

the type is polymorphic, so ghc needs to be able 
to inline the dictionary arguments. (I think that it means that
it wants to see all the code when compiling main, but I'm not sure.
Experts can tell by studing  -ddump-simpl  output.)




___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Request for optimizing help.

2012-06-24 Thread Captain Freako
Does anyone have any advice for optimizing the code, below?
Currently, the profiling results look like this:

COST CENTREMODULE   %time %alloc

runFilter  Filter90.9   41.0
convT  Filter 9.1   58.6

Thanks for any help!
-db

 11 data FilterState a = FilterState {
 12 as   :: [a] -- transfer function denominator coefficients
 13   , bs   :: [a] -- transfer function numerator coefficients
 14   , taps :: [a] -- current delay tap stored values
 15   } deriving (Show)
 16
 17 type Kernel a = (a, FilterState a) - (a, FilterState a)
 18
 19 -- FILTER KERNELS
 20 -- Time domain convolution filter (FIR or IIR),
 21 -- expressed in direct form 2
 22 convT :: (Fractional a) = Kernel a
 23 convT (x, s) =
 24 let wk = (x - sum [a * t | (a, t) - zip (tail $ as s) (taps s)]) /
head (as s)
 25 newTaps = wk : init (taps s)
 26 s' = s {taps = newTaps}
 27 y  = sum [b * w | (b, w) - zip (bs s) (wk : taps s)]
 28 in (y, s')
 29
 30 -- FILTER APPLICATION
 31 runFilter :: Kernel a - FilterState a - [a] - IO ([a], FilterState a)
 32 runFilter f s0 [] = do
 33 return ([], s0)
 34 runFilter f s0 (x:xs) = do
 35 (y, s') - return (f (x, s0))
 36 (ys, s) - runFilter f s' xs
 37 return (y:ys, s)
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe