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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:21:41+00:00 2026-06-08T01:21:41+00:00

I am fresh in haskell, and I defined a func in Haskell : febs

  • 0

I am fresh in haskell, and I defined a func in Haskell :

febs :: (Integral a)=> a -> a
febs n 
    | n<=0 =0
    | n==1 =1
    | n==2 =1
    | otherwise =febs(n-1)+febs(n-2)

but, it runs so slow, and when I do “febs 30”, it will take about 10s,
and I do the same func in C++, it runs very fast.

int febs(int n)
{
    if(n == 1 || n ==2)
    {
        return 1;
    }
    return febs(n-1)+febs(n-2);
}

Is there any way to promote my haskell func speed?

  • 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-08T01:21:43+00:00Added an answer on June 8, 2026 at 1:21 am

    This is an odd comparison, for the following reasons:

    1. You don’t say whether you’re compiling the Haskell code, or with what options. If you’re just running it in ghci, then of course it will be slow – you’re comparing interpreted code with compiled code.

    2. Your Haskell code is polymorphic whereas your C++ code is monomorphic (that is, you’ve used a type class Integral a => a -> a instead of the concrete type Int -> Int). Your Haskell code is therefore more general than your C++ code, because it can handle arbitrarily large integers instead of being restricted to the range of an Int. It’s possible that the compiler will optimize this away, but I’m not certain.

    If I put the following code in a file fib.hs

    fibs :: Int -> Int
    fibs n = if n < 3 then 1 else fibs (n-1) + fibs (n-2)
    
    main = print (fibs 30)
    

    and compile it with ghc -O2 fib.hs then it runs fast enough that it appears instantaneous to me. You should try that, and see how it compares with the C++ code.

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

Sidebar

Related Questions

I'm pretty fresh to database handling, but now completely without experience. However, I have
I am pretty fresh into working with mobile at all here but i am
I've got a fresh copy of PHPUnit installed on my system (Ubuntu 11), but
I just did a fresh install of Ubuntu 12.04 pulling down haskell-platform from apt-get
I'm very fresh with web dev, but am putting together a simple software catalog
I did a fresh git cvsimport into a new folder, after about 30 minutes
I'm fresh to F#, but I really want to learn it. I know Python,
I've re-uploaded fresh copy of Prestashop v1.4.0.17 many times to my hosting, but I'm
Was hoping for some fresh eyes on this. I've got a page which will
I am fresh to android development.I have created a android application works properly.But my

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.