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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:01:44+00:00 2026-05-28T00:01:44+00:00

Recently I was trying to determine the time needed to calculate a waveform using

  • 0

Recently I was trying to determine the time needed to calculate a waveform using the vector storage type.

I wanted to do so without requiring to print the length or something like that. Finally I came up with the following two definitions. It seems simple enough, and from what I can tell it prints a non-zero computation time as expected the first time I run the function, but I’m wondering if there are any laziness caveats here that I’ve missed.

import System.IO
import System.CPUTime
import qualified Data.Vector.Storable as V

timerIO f = do
  start <- getCPUTime
  x <- f
  let !y = x
  end <- getCPUTime
  let diff = (fromIntegral (end - start)) / (10^12)
  print $ "Computation time: " ++ show diff ++ " sec\n"

timer f = timerIO $ do return f

main :: IO ()
main = do
  let sr = 1000.0
      time = V.map (/ sr) $ V.enumFromN 0 120000 :: V.Vector Float
      wave = V.map (\x -> sin $ x * 2 * pi * 10) time :: V.Vector Float

  timer wave
  timer wave

prints,

Computation time: 0.16001 sec
Computation time: 0.0 sec

Are there any hidden bugs here? I’m really not sure that the let with strictness flag is really the best way to go here. Is there a more concise way to write this? Are there any standard functions that already do this that I should know about?

Edit: I should mention that I had read about criterion but in this case I was not looking for a robust way to calculate average timing for profiling-only purposes; rather I was looking for a simple / low-overhead way to integrate single timers into my program for tracing the timing of some computations during normal running of the application. Criterion is cool, but this was a slightly different use case.

  • 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-28T00:01:44+00:00Added an answer on May 28, 2026 at 12:01 am

    If evaluating to weak head normal form is enough – for strict Vectors or UArrays it is -, then your timing code works well¹, however, instead of the bang pattern in the let-binding, you could put a bang on the monadic bind,

    start <- getCPUTime
    !x <- f
    end <- getCPUTime
    

    which to me looks nicer, or you could use Control.Exception.evaluate

    start <- getCPUTime
    evaluate f
    end <- getCPUTime
    

    which has the advantage of (supposed) portability, whereas bang patterns are a GHC extension. If WHNF is not enough, you would need to force full evaluation, for example using rnf or deepseq, like

    start <- getCPUTime
    !x <- rnf `fmap` f
    end <- getCPUTime
    

    However, repeatedly timing the same computation with that is hairy. If, as in your example, you give the thing a name, and call it

    timer wave
    timer wave
    

    the compiler shares the computation, so it’s only done once and all but the first timer calls return zero (or very close to zero) times. If you call it with code instead of a name,

    timer (V.map (\x -> sin $ x * 2 * pi * 10) time :: V.Vector Float)
    timer (V.map (\x -> sin $ x * 2 * pi * 10) time :: V.Vector Float)
    

    the compiler can still share the computation, if it does common subexpression elimination. And although GHC doesn’t do much CSE, it does some and I’m rather confident it would spot and share this (when compiling with optimisations). To reliably make the compiler repeat the computations, you need to hide the fact that they are the same from it (or use some low-level internals), which is not easy to do without influencing the time needed for the computation.

    ¹ It works well if the computation takes a significant amount of time. If it takes only a short time, the jitter introduced by outside influences (CPU load, scheduling, …) will make single timings far too unreliable. Then you should do multiple measurements, and for that, as has been mentioned elsewhere, the criterion library is an excellent way to relieve you of the burden of writing robust timing code.

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

Sidebar

Related Questions

Recently I've been trying to save on render time by minimizing the amount of
I've been recently trying to connect to a hosted MySQL using Java but can't
I've been spending quite alot of time recently trying to learn XAML/C#/WPF/Silverlight, trying to
I was recently trying to build a HTML5 webpage (using the <!DOCTYPE html> tag)
I've been recently trying to print a form in a C# application.Taking the form
I was recently trying to explain to a programmer why, in ASP.Net, they should
Weve recently been trying to work on an application that uses pandastream to encode
I've recently been trying to create units tests for some legacy code. I've been
I've recently being trying to teach myself how parsers (for languages/context-free grammars) work, and
I've recently been trying to port a C++ application. I believe I have all

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.