caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: John Whitington <john@coherentgraphics.co.uk>
To: Helmut Brandl <helmut.brandl@gmx.net>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Performance penalty of exception handling
Date: Wed, 16 Sep 2015 20:15:47 +0100	[thread overview]
Message-ID: <55F9BFE3.2070904@coherentgraphics.co.uk> (raw)
In-Reply-To: <55F9BE16.1020502@gmx.net>

Hi,

Helmut Brandl wrote:
> I use exeption handling quite frequently even in inner loops. A common
> pattern:
>
> let some_function ... =
> ...
> if cannot_compute then raise Not_found else value
>
> try
> let value = some_function ... in
> ...
> with Not_found ->
> handle_exception
>
> I use this pattern even if the direct caller of "some_function" handles
> the exception. In many cases I could design "some_function" in a way
> that it returns an optional value and check the optional value in the
> caller. Is this significantly (at least 10%) faster?

Optional values often require allocation (and, therefore, garbage 
collection). Unless your process is so short-lived that the garbage 
collector never runs, exceptions are going to be faster. So, if you 
benchmark this, make sure your benchmarks run for a while.

Someone who knows the compiler better than I will be able to tell you if 
(Some x) when immediately pattern-matched across a function call causes 
an allocation.

Constant exception constructors like Not_found, I believe, require no 
allocation in recent versions of OCaml.

Cheers,

-- 
John Whitington
Director, Coherent Graphics Ltd
http://www.coherentpdf.com/


  reply	other threads:[~2015-09-16 19:15 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-16 19:08 Helmut Brandl
2015-09-16 19:15 ` John Whitington [this message]
     [not found]   ` <CAKR7PS9iibAcWVMbjahdkVn6KBXUZa0U88KwZEkqc-LzfaBUKg@mail.gmail.com>
2015-09-16 19:51     ` Milan Stanojević
2015-09-16 20:00 ` David Rajchenbach-Teller
2015-09-16 19:26 "Mark Adams"

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=55F9BFE3.2070904@coherentgraphics.co.uk \
    --to=john@coherentgraphics.co.uk \
    --cc=caml-list@inria.fr \
    --cc=helmut.brandl@gmx.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).