[Haskell-cafe] Timing execution of individual functions in ghci?

11 views
Skip to first unread message

Magnus Therning

unread,
Oct 22, 2014, 6:06:26 AM10/22/14
to Haskell Cafe
When playing around with various implementations of a single function
it would be nice to time the execution. Is there some way to do that
easily from within GHCi?

Or am I forced to look to criterion for that?

/M

--
Magnus Therning OpenPGP: 0xAB4DFBA4
email: mag...@therning.org jabber: mag...@therning.org
twitter: magthe http://therning.org/magnus

Heuristic is an algorithm in a clown suit. It’s less predictable, it’s more
fun, and it comes without a 30-day, money-back guarantee.
-- Steve McConnell, Code Complete

Brandon Allbery

unread,
Oct 22, 2014, 6:14:26 AM10/22/14
to Magnus Therning, Haskell Cafe
On Wed, Oct 22, 2014 at 6:05 AM, Magnus Therning <mag...@therning.org> wrote:
When playing around with various implementations of a single function
it would be nice to time the execution.  Is there some way to do that
easily from within GHCi?

:set +s (possibly also +r; http://www.haskell.org/ghc/docs/latest/html/users_guide/ghci-set.html#idp7320496 )

--
brandon s allbery kf8nh                               sine nomine associates
allb...@gmail.com                                  ball...@sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net

Daniel Trstenjak

unread,
Oct 22, 2014, 6:49:05 AM10/22/14
to haskel...@haskell.org

Hi Magnus,

> When playing around with various implementations of a single function
> it would be nice to time the execution. Is there some way to do that
> easily from within GHCi?

ghci normally only interprets your code, without much optimizations,
so your timings might be quite misleading.

> Or am I forced to look to criterion for that?

I think so, if you want to trust your timings.


Greetings,
Daniel
_______________________________________________
Haskell-Cafe mailing list
Haskel...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Magnus Therning

unread,
Oct 23, 2014, 10:05:27 AM10/23/14
to haskel...@haskell.org
On Wed, Oct 22, 2014 at 12:48:53PM +0200, Daniel Trstenjak wrote:
>
> Hi Magnus,
>
>> When playing around with various implementations of a single
>> function it would be nice to time the execution. Is there some way
>> to do that easily from within GHCi?
>
> ghci normally only interprets your code, without much optimizations,
> so your timings might be quite misleading.

Well, it might be enough to confirm or disprove intuitions about time
complexity of an implementation; is it O(n) or O(n^2)? And that's
really what I'd like to see at a first step.

Given that all I'm interested in is relative timings it might even be
enough to do a rough comparison of two implementations too.

>> Or am I forced to look to criterion for that?
>
> I think so, if you want to trust your timings.

It's just a bit too much ceremony when all I want is to confirm that
"inserting in reversed order followed by a `reverse`" has less time
complexity than "repeated appending."

Of course the only way to get something I can take to the bank is
using criterion.

Daniel Trstenjak

unread,
Oct 23, 2014, 10:35:56 AM10/23/14
to haskel...@haskell.org

Hi Magnus,

> Given that all I'm interested in is relative timings it might even be
> enough to do a rough comparison of two implementations too.

Optimizations - especially in Haskell - can change the runtime behaviour
quite dramatically. So comparing the runtime performance without turned
on optimizations is pretty much pointless.

Meaningful benchmarking is hard and adding any kind of uncertainty
doesn't make it in any way easier.

Carter Schonwald

unread,
Oct 23, 2014, 10:41:22 AM10/23/14
to haskell-cafe
indeed. My suggestion is 
1) use criterion, its awsome
2) run the criterion suite with a variety of optimization flag choices so you can understand how that changes performance
3) use criterion some more !

John Lato

unread,
Oct 23, 2014, 9:00:37 PM10/23/14
to Carter Schonwald, haskell-cafe
While criterion is really great, sometimes you do just want to measure timings.  For that, you might want to give my package chronograph (http://hackage.haskell.org/package/chronograph) a try.
Reply all
Reply to author
Forward
0 new messages