[Haskell-beginners] Cheap lookup tables

Daniel Fischer daniel.is.fischer at googlemail.com
Tue Dec 14 18:28:53 CET 2010


On Tue, Dec 14, 2010 at 6:13 PM, Paul Sargent <psarge at gmail.com> wrote:

> I've just done some profiling on my application, and I've found that about
> 50% of my time and allocations is taken by two functions. All these are a
> functions which return constants from tables.
>
<snip>

>
> They tend to be called in pairs, and the first function is called about
> 100,000 times. The second about 50,000 times (which then causes 50,000 of
> the first function).
>
> What's my best strategy for optimising these functions?


Maybe looking it up in an array helps. You'd need an Ix instance for Gas,
which is easy (I'm not sure whether Ix can be derived).


> Is it the pattern match that's hurting me?


Hard to say, but with pattern matching, it may take a lot of sequential
comparisons to find the value, which *may* cause it to use a lot of time.


> Can I write them in a better way, or do I need to look at how I'm using
> them?
>
> Obviously calling them less often would help, but I'm not sure what I'd do
> to my code to achieve this. These constants are used in lots of fairly
> diverse areas of my code, and they're encapsulated into functions to stop
> magic numbers flying around.
>
> Thanks
>
> Paul
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20101214/3562777a/attachment.htm>


More information about the Beginners mailing list