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 6928275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:09:55+00:00 2026-05-27T11:09:55+00:00

I tried to test Haskell performance, but got some unxepectedly poor results: — main

  • 0

I tried to test Haskell performance, but got some unxepectedly poor results:

-- main = do
--  putStrLn $ show $ sum' [1..1000000]

sum' :: [Int] -> Int
sum' [] = 0
sum' (x:xs) = x + sum' xs

I first ran it from ghci -O2:

> :set +s
> :sum' [1..1000000]
1784293664
(4.81 secs, 163156700 bytes)

Then I complied the code with ghc -O3, ran it using time and got this:

1784293664

real    0m0.728s
user    0m0.700s
sys     0m0.016s

Needless to say, these results are abysmal compared to the C code:

#include <stdio.h>

int main(void)
{
    int i, n;
    n = 0;
    for (i = 1; i <= 1000000; ++i)
        n += i;
    printf("%d\n", n);
}

After compiling it with gcc -O3 and running it with time I got:

1784293664

real    0m0.022s
user    0m0.000s
sys     0m0.000s

What is the reason for such poor performance? I assumed that Haskell would never actually construct the list, am I wrong in that assumption? Is this something else?

UPD: Is the problem that Haskell doesn’t know that addition is associative? Is there a way to make it see and use that?

  • 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-05-27T11:09:56+00:00Added an answer on May 27, 2026 at 11:09 am

    First, don’t bother to discuss GHCi when you’re talking about performance. It’s nonsense to use -Ox flags with GHCi.

    You’re Building Up A Huge Computation

    Using GHC 7.2.2 x86-64 with -O2 I get:

    Stack space overflow: current size 8388608 bytes.
    Use `+RTS -Ksize -RTS' to increase it.
    

    The reason this uses so much stack space is upon every loop you build an expression of i+..., so your computation is transformed into a huge thunk:

    n = 1 + (2 + (3 + (4 + ...
    

    That’s going to take a lot of memory. There is a reason the standard sum isn’t defined like your sum'.

    With A Reasonable Definition for sum

    If I change your sum' to sum or an equivalent such as foldl' (+) 0 then I get:

    $  ghc -O2 -fllvm so.hs
    $ time ./so
    500000500000
    
    real    0m0.049s
    

    Which seems entirely reasonable to me. Keep in mind that, with such a short-running piece of code much of your measured time is noise (loading the binary, starting up the RTS and GC nursery, misc initializations, etc). Use Criterion (a benchmarking tool) if you want accurate measurements of small-ish Haskell computations.

    Comparing to C

    My gcc -O3 time is immeasurably low (reported as 0.002 seconds) because the main routine consists of 4 instructions – the entire computation is evaluated at compile time and the constant of 0x746a5a2920 is stored in the binary.

    There is a rather long Haskell thread (here, but be ware it’s something of an epic flame war that still burns in peoples minds almost 3 years later) where people discuss the realities of doing this in GHC starting from your exact benchmark – it isn’t there yet but they did come up with some Template Haskell work that would do this if you wish to achieve the same results selectively.

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

Sidebar

Related Questions

I tried to test an example of C++11 threads in Eclipse. But I got
I got strange behavior when I tried to test my navigator.geolocation.getCurrentPosition web page. Here
Tried examples from 'php.net' but don't understand what's the problem. Any suggestions? <?php $_SESSION['test']
I downloaded the jira4r-jh gem and tried to test it from irb, but as
I tried - Test\d\d\d and it works but problem is, String in expression could
I need to set the highest bit of some label address/offset. I tried: test.nasm:
I tried to test this myself before asking on the forum but my simple
I tried a test file which is working but this file isn't. I also
I've tried to test socket connection in Java, but failed. Here is my code
I just started with Haskell and tried to do write some tests first. Basically,

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.