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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:28:39+00:00 2026-06-12T23:28:39+00:00

I am using criterion to benchmark my Haskell code. I’m doing some heavy computations

  • 0

I am using criterion to benchmark my Haskell code. I’m doing some heavy computations for which I need random data. I’ve written my main benchmark file like this:

main :: IO ()
main = newStdGen >>= defaultMain . benchmarks

benchmarks :: RandomGen g => g -> [Benchmark]
benchmarks gen =
   [
     bgroup "Group"
     [
       bench "MyFun" $ nf benchFun (dataFun gen)
     ]
   ]

I keep benchmarks and data genrators for them in different modules:

benchFun :: ([Double], [Double]) -> [Double]
benchFun (ls, sig) = fun ls sig

dataFun :: RandomGen g => g -> ([Double], [Double])
dataFun gen = (take 5 $ randoms gen, take 1024 $ randoms gen)

This works, but I have two concerns. First, is the time needed to generate random data included in the benchmark? I found a question that touches on that subject but honestly speaking I’m unable to apply it to my code. To check whether this happens I wrote an alternative version of my data generator enclosed within IO monad. I placed benchmarks list with main, called the generator, extracted the result with <- and then passed it to the benchmarked function. I saw no difference in performance.

My second concern is related to generating random data. Right now the generator once created is not updated, which leads to generating the same data within a single run. This is not a major problem, but nevertheless it would be nice to make it properly. Is there a neat way to generate different random data within each data* function? “Neat” means “without making data functions acquiring StdGen within IO”?

EDIT: As noted in comment below I don’t really care about data randomness. What is important to me is that the time needed to generate the data is not included in the benchmark.

  • 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-12T23:28:41+00:00Added an answer on June 12, 2026 at 11:28 pm

    This works, but I have two concerns. First, is the time needed to generate random data included in the benchmark?

    Yes it would. All of the random generation should be happening lazily.

    To check whether this happens I wrote an alternative version of my data generator enclosed within IO monad. I placed benchmarks list with main, called the generator, extracted the result with <- and then passed it to the benchmarked function. I saw no difference in performance.

    This is expected (if I understand what you mean); the random values from randoms gen aren’t going to be generated until they’re needed (i.e. inside your benchmark loop).

    Is there a neat way to generate different random data within each data* function? “Neat” means “without making data functions acquiring StdGen within IO”?

    You need either to be in IO or create an StdGen with an integer seed you supply, with mkStdGen.

    Re. your main question of how you should get the pRNG stuff out of your benchmarks, you should be able to evaluate the random input fully before your defaultMain (benchmarks g) stuff, with evaluate and force like:

    import Control.DeepSeq(force)
    import Control.Exception(evaluate)
    myBench g = do randInputEvaled <- evaluate $ force $ dataFun g
                   defaultMain [
                        bench "MyFun" $ nf benchFun randInputEvaled
                        ...
    

    where force evaluates its argument to normal form, but this will still happen lazily. So to get it to be evaluated outside of bench we use evaluate to leverage monadic sequencing. You could also do things like call seq on the tail of each of the lists in your tuple, etc. if you wanted to avoid the imports.

    That kind of thing should work fine, unless you need to hold a huge amount of test data in memory.

    EDIT: this method is also a good idea if you want to get your data from IO, like reading from the disk, and don’t want that mixed in to your benchmarks.

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

Sidebar

Related Questions

I'm trying to optimize some code, using criterion to try to compare, for example,
my code is like this: Using StateProv As XmlElement = CType(hotelSearch.SelectSingleNode(/ota:OTA_HotelSearchRQ/ota:Criteria/ota:Criterion/ota:Address/ota:StateProv, nsmgr), XmlElement) 'i
Say I had some data, for which I want to fit a parametrized model
Hi im retrieving some data from a database using JSON and ajax. When i
I'm using NHibernate to retrieve a list from the database based on some criteria.
I'm constructing a Hibernate Criterion, using a subselect as follows DetachedCriteria subselect = DetachedCriteria.forClass(NhmCode.class,
I'm trying to add custom fields to an InlineFormset using the following code, but
I did some Criterion benchmarks to estimate how much performance I lose by running
I need to replicate the following working HQL query using criteria API. session.CreateQuery( select
I have the following code: public interface IDrilldown { void AddCriteria<T>(T Criterion); } public

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.