Efficiency of using field labels vs pattern matching
Sigbjorn Finne
sof at galois.com
Sun Aug 20 10:04:56 EDT 2006
If you let the Simplifier have a crack at your code (i.e., compile with -O
or better), the same code should be generated for the two defns of
'foo'. Try compiling with -ddump-simpl to verify.
The first version is stricter, so it'll be preferable in -Onot mode.
--sigbjorn
Brian Hulley wrote:
> Hi,
> I'm wondering if it is more efficient to pattern match against the
> components of a record instead of using the field functions, though
> I'd prefer to just use the field functions.
>
> For example, if I write:
>
> data R = R {_xRef :: !(IORef Int)}
>
> foo :: Int -> R -> IO ()
> foo i R{_xRef = xRef} = writeIORef xRef i
>
> is the above more efficient than:
>
> foo i r = writeIORef (_xRef r) i
>
> I know this is probably a bit silly to worry about the overhead of an
> extra function call etc but I've at the moment got a lot of code using
> the first version and I think it would look much neater just using
> field labels but I don't want to modify it if it will be less
> efficient since speed is quite critical here.
>
> Thanks, Brian.
More information about the Glasgow-haskell-users
mailing list