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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:43:12+00:00 2026-06-14T02:43:12+00:00

Another microbenchmark: Why is this loop (compiled with ghc -O2 -fllvm , 7.4.1, Linux

  • 0

Another microbenchmark: Why is this “loop” (compiled with ghc -O2 -fllvm, 7.4.1, Linux 64bit 3.2 kernel, redirected to /dev/null)

mapM_ print [1..100000000]

about 5x slower than a simple for-cycle in plain C with write(2) non-buffered syscall? I am trying to gather Haskell gotchas.

Even this slow C solution is much faster than Haskell

int i;
char buf[16];
for (i=0; i<=100000000; i++) {
    sprintf(buf, "%d\n", i);
    write(1, buf, strlen(buf));
}
  • 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-14T02:43:14+00:00Added an answer on June 14, 2026 at 2:43 am

    Okay, on my box the C code, compiled per gcc -O3 takes about 21.5 seconds to run, the original Haskell code about 56 seconds. So not a factor of 5, a bit above 2.5.

    The first nontrivial difference is that

    mapM_ print [1..100000000]
    

    uses Integers, that’s a bit slower because it involves a check upfront, and then works with boxed Ints, while the Show instance of Int does the conversion work on unboxed Int#s.

    Adding a type signature, so that the Haskell code works on Ints,

    mapM_ print [1 :: Int .. 100000000]
    

    brings the time down to 47 seconds, a bit above twice the time the C code takes.

    Now, another big difference is that show produces a linked list of Char and doesn’t just fill a contiguous buffer of bytes. That is slower too.

    Then that linked list of Chars is used to fill a byte buffer that then is written to the stdout handle.

    So, the Haskell code does more, and more complicated things than the C code, thus it’s not surprising that it takes longer.

    Admittedly, it would be desirable to have an easy way to output such things more directly (and hence faster). However, the proper way to handle it is to use a more suitable algorithm (that applies to C too). A simple change to

    putStr . unlines $ map show [0 :: Int .. 100000000]
    

    almost halves the time taken, and if one wants it really fast, one uses the faster ByteString I/O and builds the output efficiently as exemplified in applicative’s answer.

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

Sidebar

Related Questions

another newbie-question regarding rails and bootstrap. I am using something like this: <div class=tabbable
another sql problem of mine .. this time the reader doesn't work properly (
Another question related to this one . I have a List<SortableObjects> that is the
Another problem I try to solve ( NOTE this is not a homework but
another day, another problem. Honestly, it would be rather boring if this ever stopped,
another Objective-C one for you, probably pretty obvious but I have been at this
Another thanks to SLaks for helping me get the code this far. I'm not
Another collinear-points question. This one's twist is, I'm using integer arithmetic, and I'm looking
Another thread had this quote Erlang VM BEAM and HiPE is written mostly in
Another hair-puller. AFter two days of fighting with this I cannot figure out what

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.