Efficiency of using field labels vs pattern matching

Brian Hulley brianh at metamilk.com
Sun Aug 20 09:15:41 EDT 2006


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.
-- 
Logic empowers us and Love gives us purpose.
Yet still phantoms restless for eras long past,
congealed in the present in unthought forms,
strive mightily unseen to destroy us.

http://www.metamilk.com 



More information about the Glasgow-haskell-users mailing list