Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get evaluation timings in GHCi

I have a relatively slow procedure (aptly named slow), and I would like to do something like

time $ slow [1,2,3,4,5]

in the console (REPL) to get the time, instead of having to compile the program and then run time.

Can this be done?

like image 794
Christian Neverdal Avatar asked Apr 26 '12 22:04

Christian Neverdal


People also ask

How do I quit Ghci?

Quits GHCi. You can also quit by typing control-D at the prompt. Attempts to reload the current target set (see :load ) if any of the modules in the set, or any dependent module, has changed.

How do I run Haskell with Ghci?

Open a command window and navigate to the directory where you want to keep your Haskell source files. Run Haskell by typing ghci or ghci MyFile. hs. (The "i" in "GHCi" stands for "interactive", as opposed to compiling and producing an executable file.)

How does GHCi evaluate the whole line?

GHCi interprets the whole line as an expression to evaluate. The expression may not span several lines - as soon as you press enter, GHCi will attempt to evaluate it. In Haskell, a letexpression is followed by in.

How do I display the history of evaluation steps in GHCi?

See also: :trace, :history, :back. Displays a list of the available commands. Repeat the previous command. Display the history of evaluation steps. With a number, displays that many steps (default: 20). For use with :trace; see Tracing and history. To set the number of history entries stored by GHCi, use the -fghci-hist-size=⟨n⟩ flag.

How do I get the current date and time in GHCi?

To start with, here’s a new GHCi command which doesn’t take any arguments or produce any results, it just outputs the current date and time: Prelude> let date _ = Data.Time.getZonedTime >>= print >> return "" Prelude> :def date date Prelude> :date 2017-04-10 12:34:56.93213581 UTC Here’s an example of a command that takes an argument.

How do I use compiled code with GHCi?

Note the -dynamic flag to GHC: GHCi uses dynamically-linked object code (if you are on a platform that supports it), and so in order to use compiled code with GHCi it must be compiled for dynamic linking. At any time you can use the command :show modules to get a list of the modules currently loaded into GHCi:


1 Answers

If you enter :set +s in GHCi, then timing and memory info will be printed after the evaluation of every expression.

Example:

Prelude> :set +s
Prelude> sum [1..2^20]
549756338176
it :: (Num a, Enum a) => a
(0.34 secs, 169,197,008 bytes)

Note that this will be the timing of the expression as evaluated in the interpreter, without optimisation, so it won't necessarily be an accurate measure of how long things take, or even which of two versions of the same code will be faster, in actual compiled code. For that, take a look at the criterion benchmarking library.

like image 79
ehird Avatar answered Oct 08 '22 22:10

ehird