Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8034691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:06:04+00:00 2026-06-05T02:06:04+00:00

I’ve written code for the Project Euler’s Challenge 14 , in both Haskell and

  • 0

I’ve written code for the Project Euler’s Challenge 14, in both Haskell and C++ (ideone links). They both remember any calculations they have previously done in an array.

Using ghc -O2 and g++ -O3 respectively, the C++ runs 10-15 times faster than the Haskell version.

Whilst I understand the Haskell version may run slower, and that Haskell is a nicer language to write in, it would be nice to know some code changes I can make to the Haskell version to make it run faster (ideally within a factor of 2 or 3 of the C++ version)?


Haskell code is here:

import Data.Array
import Data.Word
import Data.List

collatz_array = 
  let
    upperbound = 1000000
    a = array (1, upperbound) [(i :: Word64, f i :: Int) | i <- [1..upperbound]]
    f i = i `seq`
      let
        check_f i = i `seq` if i <= upperbound then a ! i else f i
      in
        if (i == 1) then 0 else (check_f ((if (even i) then i else 3 * i + 1) `div` 2)) + 1
  in a

main = 
  putStrLn $ show $ 
   foldl1' (\(x1,x2) (y1,y2) -> if (x2 >= y2) then (x1, x2) else (y1, y2)) $! (assocs collatz_array)

Edit:

I’ve now also done a version using unboxed mutable arrays. It is still 5 times slower than the C++ version, but a significant improvement. The code is on ideone here.

I’d like to know improvements to the mutable array version which bring it closer to the C++ version.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T02:06:05+00:00Added an answer on June 5, 2026 at 2:06 am

    Some problems with your (mutable array) code:

    • You use a fold to find the maximal chain length, for that the array has to be converted to an association list, that takes time and allocation the C++ version doesn’t need.
    • You use even and div for testing resp dividing by 2. These are slow. g++ optimises both operations to the faster bit operations (on platforms where that is supposedly faster, at least), but GHC doesn’t do these low-level optimisations (yet), so for the time being, they have to be done by hand.
    • You use readArray and writeArray. The extra bounds-checking that isn’t done in the C++ code also takes time, once the other problems are dealt with, that amounts to a significant portion of the running time (ca. 25% on my box), since there are done a lot of reads and writes in the algorithm.

    Incorporating that into the implementation, I get

    import Data.Array.ST
    import Data.Array.Base
    import Control.Monad.ST
    import Data.Bits
    
    collatz_array :: ST s (STUArray s Int Int)
    collatz_array = do
        let upper = 10000000
        arr <- newArray (0,upper) 0
        unsafeWrite arr 2 1
        let check i
                | upper < i = return arr
                | i .&. 1 == 0 = do
                    l <- unsafeRead arr (i `shiftR` 1)
                    unsafeWrite arr i (l+1)
                    check (i+1)
                | otherwise = do
                    let j = (3*i+1) `shiftR` 1
                        find k l
                            | upper < k = find (next k) $! l+1
                            | k < i     = do
                                m <- unsafeRead arr k
                                return (m+l)
                            | otherwise = do
                                m <- unsafeRead arr k
                                if m == 0
                                  then do
                                      n <- find (next k) 1
                                      unsafeWrite arr k n
                                      return (n+l)
                                  else return (m+l)
                              where
                                next h
                                    | h .&. 1 == 0 = h `shiftR` 1
                                    | otherwise = (3*h+1) `shiftR` 1
                    l <- find j 1
                    unsafeWrite arr i l
                    check (i+1)
        check 3
    
    collatz_max :: ST s (Int,Int)
    collatz_max = do
        car <- collatz_array
        (_,upper) <- getBounds car
        let find w m i
                | upper < i = return (w,m)
                | otherwise = do
                    l <- unsafeRead car i
                    if m < l
                      then find i l (i+1)
                      else find w m (i+1)
        find 1 0 2
    
    main :: IO ()
    main = print (runST collatz_max)
    

    And the timings (both for 10 million):

    $ time ./cccoll
    8400511 429
    
    real    0m0.210s
    user    0m0.200s
    sys     0m0.009s
    $ time ./stcoll
    (8400511,429)
    
    real    0m0.341s
    user    0m0.307s
    sys     0m0.033s
    

    which doesn’t look too bad.

    Important note: That code only works on 64-bit GHC (so, in particular, on Windows, you need ghc-7.6.1 or later, previous GHCs were 32-bit even on 64-bit Windows) since intermediate chain elements exceed 32-bit range. On 32-bit systems, one would have to use Integer or a 64-bit integer type (Int64 or Word64) for following the chains, at a drastic performance cost, since the primitive 64-bit operations (arithmetic and shifts) are implemented as foreign calls to C functions in 32-bit GHCs (fast foreign calls, but still much slower than direct machine ops).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.