
On 20/04/2004, at 9:48 PM, Bernard James POPE wrote:
To test out the various possible ways of implementing a global counter I wrote some test cases (shown below). I hope the test cases are useful, and provide some indication of the relative performance. However, if you spot something bogus please let me know.
Each program computes the equivalent of:
sum ([1..100000000] :: [Int])
There are four different ways that I tried:
1) pure: this is just pure functional code and should be fast. This test case is only here as a control example, it is not a candidate solution because I need a global counter.
2) ioref: this uses a global mutable counter using IORefs and unsafePerformIO
3) fastMut: this uses the fast mutable integer library from GHC that was suggested by Simon Marlow.
4) ffi: this implements the counter in C using the FFI.
There's another way which you missed: using implicit parameters. I remember reading a paper a while ago called Global Variables in Haskell (sorry, don't remember the author -- Jones, perhaps?) which did similar benchmarking to yours, and carrying around the global variable with an implicit parameter was faster than using a global mutable counter via "unsafePerformIO $ newIORef ...". -- % Andre Pang : trust.in.love.to.save