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

  • Home
  • SEARCH
  • 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 8520857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:38:57+00:00 2026-06-11T06:38:57+00:00

Consider this simple benchmark: n :: Int n = 1000 main = do print

  • 0

Consider this simple “benchmark”:

n :: Int
n = 1000
main = do
    print $ length [(a,b,c) | a<-[1..n],b<-[1..n],c<-[1..n],a^2+b^2==c^2]

and appropriate C version:

#include <stdio.h>

int main(void)
{
    int a,b,c, N=1000;
    int cnt = 0;

    for (a=1;a<=N;a++)
        for (b=1;b<=N;b++)
            for (c=1;c<=N;c++)
                if (a*a+b*b==c*c) cnt++;
    printf("%d\n", cnt);
}

Compilation:

  • Haskell version is compiled as: ghc -O2 triangle.hs (ghc 7.4.1)
  • C version is compiled as: gcc -O2 -o triangle-c triangle.c (gcc 4.6.3)

Run times:

  • Haskell: 4.308s real
  • C: 1.145s real

Is it OK behavior even for such a simple and maybe well optimizable program that Haskell is almost 4 times slower? Where does Haskell waste time?

  • 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-11T06:38:59+00:00Added an answer on June 11, 2026 at 6:38 am

    The Haskell version is wasting time allocating boxed integers and tuples.

    You can verify this by for example running the haskell program with the flags +RTS -s. For me the outputted statistics include:

      80,371,600 bytes allocated in the heap
    

    A straightforward encoding of the C version is faster since the compiler can use unboxed integers and skip allocating tuples:

    n :: Int
    n = 1000
    main = do
      print $ f n
    
    f :: Int -> Int
    f max = go 0 1 1 1
      where go cnt a b c
              | a > max = cnt
              | b > max = go cnt (a+1) 1 1
              | c > max = go cnt a (b+1) 1
              | a^2+b^2==c^2 = go (cnt+1) a b (c+1)
              | otherwise = go cnt a b (c+1)
    

    See:

      51,728 bytes allocated in the heap
    

    The running time of this version is 1.920s vs. 1.212s for the C version.

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

Sidebar

Related Questions

Let's consider this simple test program: #include <stdio.h> #include <string.h> int main(int argc, char
consider this simple and pointless code. #include <iostream> struct A { template<int N> void
Consider this: #include <iostream> using namespace std; class A{ protected: void some_function(int params) {
consider this simple function def foo(l=[]): if not l: print List is empty else
Consider this simple code: int foo = 4; double d1 = sin (foo); double
Consider this simple Scala class: class A(val d: Int) Is there a difference in
consider this simple servlet sample: protected void doGet(HttpServletRequest request, HttpServletResponse response){ Cookie cookie =
Consider this simple python code, which demonstrates a very simple version control design for
Consider this simple model: A base Location table: +-------------------------------+ | Locations | +-------------------------------+ |(PK)
Consider this simple example - public class Person { private String name; private Date

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.