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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:31:29+00:00 2026-05-25T12:31:29+00:00

Consider this block of code: isPrime primes’ n = foldr (\p r -> p

  • 0

Consider this block of code:

isPrime primes' n = foldr (\p r -> p * p > n || (n `rem` p /= 0 && r)) True primes'

primes = 2 : filter (isPrime primes) [3..]

main = putStrLn $ show $ sum $ takeWhile (< 1000000) primes

which calculates the sum of all primes below one million. It takes 0.468 seconds to print the result on my machine. But if the definitions of isPrime and primes are extracted into another module, the time cost is 1.23 sec, it’s almost 3x slower.

Of course I can copy/paste the difinitions everywhere it’s required, but I’m also curious about why this is happening, and how to solve it.


[Edit]
I’m using GHC 7.0.3 (Windows 7 + MinGW). The code is written in EclipseFP (It uses Scion as IDE back-end), and built into an executable file with -O2 flags.

I also tried building the package outside the IDE:

executable test
  hs-source-dirs:  src
  main-is:         Main.hs
  build-depends:   base >= 4
  ghc-options:     -O2
  other-modules:   Primes

executable test2
  hs-source-dirs:  src2
  main-is:         Main.hs
  build-depends:   base >= 4
  ghc-options:     -O2

Here’s the result:

$ time test/test
37550402023

real    0m1.296s
user    0m0.000s
sys     0m0.031s

$ time test2/test2
37550402023

real    0m0.520s
user    0m0.015s
sys     0m0.015s
  • 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-25T12:31:29+00:00Added an answer on May 25, 2026 at 12:31 pm

    I can reproduce this if I put isPrime and primes in different modules. (If they are in the same module, but still separate from main, I see no difference).

    Adding {-# INLINE isPrime #-} gives back the same performance as having all three in one module, so it would appear that GHC needed a nudge to do cross-module inlining in this case.

    This is on GHC 7.0.2, Ubuntu 11.04, 64-bit

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

Sidebar

Related Questions

Consider the following code: def create_class(class_name, superclass, &block) klass = Class.new superclass, &block Object.const_set
consider this code block public void ManageInstalledComponentsUpdate() { IUpdateView view = new UpdaterForm(); BackgroundWorker
Consider this code: public class Main { static String s = -; public static
Let’s consider this very short snippet of code: #include <stdlib.h> int main() { char*
Consider this code block: struct Animal { public string name = ; // Error
Consider this code: try { ISomeObject pObj(__uuidof(SomeClass)); ISomeObject pObj2(__uuidof(SomeOtherClass)); } catch ( _com_error& e
Consider this code: new Ajax.Request('?service=example', { parameters : {tags : 'exceptions'}, onSuccess : this.dataReceived.bind(this)
Consider this piece of code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using
Consider this code for loading, modifying and saving a Bitmap image: using (Bitmap bmp
Consider: if (condition1) { // Code block 1 } else { // Code block

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.