?

Log in

No account? Create an account

Exceptions in D Vs other languages - leonardo
View:Recent Entries.
View:Archive.
View:Friends.
View:Profile.
View:Website (My Website).

Tags:, , , , , , ,
Security:
Subject:Exceptions in D Vs other languages
Time:11:59 pm
To test how efficiently the current main D compiler (DMD) supports exceptions (on Windows) I have used this "except" benchmark, code adapted from:
http://alioth.debian.org/plugins/scmcvs/cvsweb.php/shootout/bench/except/?cvsroot=shootout

You can find all the code used here in the directory "except" into this zip:
http://www.fantascienza.net/leonardo/js/slow_d.zip
The timings of "except" benchmark, best of 3 (seconds):
  N = 200_000:
    C:       0.07  (approx time)
    Java:    0.41
    C++  :   0.54
    Psyco:   1.80
    Python:  1.90
    D:       2.53  SLOW=false
    D:       4.44  SLOW=true

  N = 4_000_000:
    C:       0.36 
    Java:    5.59
    C++:     9.97
    Psyco:  29.28
    Python: 32.68
    D:      48.76  SLOW=false
    D:      88.45  SLOW=true
  
  N = 20_000_000:
    C:       1.57
    C:       1.61  (LLVM-GCC)
    Java:   27.43
    C++:    49.45


CPU: Core 2 at 2 GHz, 2 GB Ram, Windows XP.


D code compiled with:
  DMD v1.041
  -O -release -inline

C and C++ code compiled with:
  gcc version 4.3.3-dw2-tdm-1 (GCC)
  -O3 -s

Java:
  javac 1.6.0_12
  java HotSpot build 1.6.0_07-b06

Python:
  ActivePython 2.6.1.1
  (r261:67515, Dec  5 2008, 13:58:38)
  [MSC v.1500 32 bit (Intel)] on win32

Psyco for Python 2.6, V.1.6.0 final 0

Notes:
- Having fast exceptions is getting more important, for example it's important to have fast exceptions in Python because there they are sometimes used to control flow too. For example to convert a string to int/float the common Python idiom is to use a try/except.
- As you can see with DMD D exceptions are even slower than Python ones. When N=4_000_000 D exceptions (using writefln) are about 15.8 times slower than Java ones. This isn't good.
- In the D code I don't know why using printf speeds up the code about two times compared to using writefln.
- I'd like to know the timings on Linux with the LDC D compiler.
- The C code isn't fully fair, it uses a lower-level way to implement this program.
- The Java HotSpot VM shows to be quite faster than C++, about 1.8 times when N=20_000_000.
comments: Leave a comment Previous Entry Share Next Entry


scale
Link:(Link)
Time:2009-03-23 11:33 am (UTC)
From what I read, using exceptions to control program flow is considered very bad practice, because even if exceptions are fast they have side effects which are costly and cannot be avoided in most languages, because such effects are part of the definition of exceptions in that language. For example exceptions cause stack unwinding in C++, which can trigger chains of destructor calls with far-reaching side effects, lots of memory deallocation, etc. In languages with GC I think exceptions trigger special actions by the GC which depend on the context. So even in the low level mechanic behind exceptions is fast, in a real world scenario the speed of an exception's resolution depends on the situation, and using them to control the flow can have an unpredictable speed.
In addition exceptions can cross the program flow in any direction, by definition, so you would end up with a very loose flow which would be very hard to enforce and to document. For example changing a catch in one place might break unrelated parts of the program which rely on some specific exceptions passing through that catch untouched, and it might be impossible to know this if the method/function which does the catch is taken out of context (which happens quite often if you are working on a large code base). If the program is large this may easily turn into a huge mess. I think return values, dispatching, access modifiers and other traditional means of flow control allow to define much more predictable flows.
And I don't even want to think what happens if you try to do that in a multithreaded program, since exceptions can cross threads, force to exit critical sections, etc... :-P
(Reply) (Thread)


scale
Link:(Link)
Time:2009-03-23 11:48 am (UTC)
Clarification: as far as I can tell, exceptions are used mostly to control the flow of operations between modules which can be separated clearly. Most typical cases are the GUI sending a command when a button is pressed and catching exceptions which are thrown if the operation fail, or a database throwing an exception to an application if a query fails. Throwing them around between strictly related classes is not nice. (And it's not so different than returning values anyway, unless a language was designed specifically with this use of exceptions in mind.)
(Reply) (Thread)

(Anonymous)
Subject:curious about ldc
Link:(Link)
Time:2009-03-23 12:00 pm (UTC)
would like to see ldc timings on linux (I don't think llvm supports EH on windows yet).
(Reply) (Thread)

(Anonymous)
Subject:OS X gdc 0.24, using dmd 1.020
Link:(Link)
Time:2009-03-23 08:11 pm (UTC)
iMac 2.4Ghz Core 2 Duo. gdc/g++ with -O3, best of 3:

* C++ 1.051s
* D 0.881s
(Reply) (Thread)

(Anonymous)
Subject:Re: OS X gdc 0.24, using dmd 1.020
Link:(Link)
Time:2009-03-23 08:11 pm (UTC)
Forgot to add: n=200000, gcc version 4.0.1 (Apple Inc. build 5490)
(Reply) (Parent) (Thread)


leonardo_m
Subject:Re: OS X gdc 0.24, using dmd 1.020
Link:(Link)
Time:2009-03-23 08:36 pm (UTC)
Nice results, thank you for your timings. So it seems the 'fault' is just into the DMD backend. So LDC on Linux too may be fast.
(Reply) (Parent) (Thread)

Exceptions in D Vs other languages - leonardo
View:Recent Entries.
View:Archive.
View:Friends.
View:Profile.
View:Website (My Website).