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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:54:27+00:00 2026-06-17T19:54:27+00:00

I have an algorithm for calculating the nth Fibonacci number, in Python it’s expressed

  • 0

I have an algorithm for calculating the nth Fibonacci number, in Python it’s expressed as:

def fib(n):
    if n == 0:
        return 1
    if n == 1:
        return 1
    else:
        return fib(n-1) + fib(n-2)

and in Haskell:

fib :: Integer -> Integer
fib 0 = 1
fib 1 = 1
fib n = fib (n-1) + fib (n-2)

I would have expected Haskell to evaluate faster or around the same time, but if using a number above say n=40, python code evaluates much (~x3) faster. I’m using GHCi and Ipython but I didn’t think that should make a difference.

  • 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-17T19:54:29+00:00Added an answer on June 17, 2026 at 7:54 pm

    You said that you ran the Haskell code in GHCI, which means that you ran it without optimizations. That means that no strictness analysis was done, so the whole thing was evaluated lazily, creating a lot of unnecessary thunks. That would explain why it was slower.

    Also as delnan pointed out in a comment, ghci is much slower than compiling the code with ghc and then running it – even without optimizations. When I test your code on my PC, running after compiling without optimizations takes twice as long as with optimizations, but still less time than running the Python code. Running in ghci takes a whole lot longer than that.

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

Sidebar

Related Questions

I have algorithm of calculating average speed in pure python: speed = [...] avg_speed
I have algorithm of calculation of the difference between neighboring elements in pure python:
I have written an algorithm that calculates the number of zero-crossings within a signal.
Given an arbitrary floating point number, say -0.13 , suppose we have an algorithm
I have an algorithm for calculating whether a player's hand holds a straight in
Please help with the algorithm of calculating the slope So we have a Cartesian
Is it known what is the fastest Java algorithm for calculating nth term of
Does anyone have a decent algorithm for calculating axis minima and maxima? When creating
I have algorithm/computation in Java and unit test for it. The unit test expects
I have an algorithm that finds the minimum of function/n on Rn. And I

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.