www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - D's exception cost (happy path) is large than G++

reply redsea <redsea 163.com> writes:
D 1.018

loop 8G times, without try/catch

real    0m24.452s
user   0m23.911s
sys     0m0.005s

loop 8G times, with try/catch
real    0m31.455s
user   0m30.749s
sys     0m0.008s

add 28.6% cost
----------------
g++  (gcc version 4.1.3 20070812 (prerelease) (Debian 4.1.2-15)
loop 8G times, without try/catch

real    0m20.971s
user   0m20.623s
sys     0m0.004s

loop 8G times, with try/catch
real    0m24.481s
user   0m23.949s
sys     0m0.004s

add 16.7% cost

-------------------
D's cost is larger.


-------------------
code:
D:

module test;

import tango.io.Stdout;

int turn=0;
int count;


void fooFunc()
{
    ++ count;
};

alias void function() foo_t;


void test(int c1, int c2, foo_t foo)
{
    for (int j=0;j<c1;j++)
    {
        ++ turn;
        count = 0;
        for (int i=0;i<c2;i++)
        {
            try
            {
                foo();
            }
            catch (Exception e)
            {

            }
        }
    }

    Stdout("\nturn ")(turn)(" count ")(count);

}


int main()
{
    test(8*1024, 1048576, &fooFunc);
    return 0;
}

compile with -O -release -inline

no catch version, comment the try & catch line
-------------------
code:
c++

#include <stdio.h>
#include <exception>

volatile int turn=0;
volatile int count;


void fooFunc(void)
{
    ++ count;
};

typedef void (*foo_t)(void);


void test(int c1, int c2, foo_t foo)
{
    for (int j=0;j<c1;j++)
    {
        ++ turn;
        count = 0;
        for (int i=0;i<c2;i++)
        {
            try
            {
                foo();
            }
            catch (std::exception &e)
            {

            }
        }
    }

    printf("\nturn %d, count %d", turn, count);

}


int main()
{
    test(8*1024, 1048576, fooFunc);
    return 0;
}

compile with -O2
no catch version, comment the try & catch line
Sep 06 2007
parent redsea <redsea 163.com> writes:
[td]root uname -a

GNU/Linux

[td]root g++ -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v
--enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr
--enable-shared --with-system-zlib --libexecdir=/usr/lib
--without-included-gettext --enable-threads=posix --enable-nls
--with-gxx-include-dir=/usr/include/c++/4.1.3 --program-suffix=-4.1
--enable-__cxa_atexit --enable-clocale=gnu --enable-libstdcxx-debug
--enable-mpfr --enable-checking=release i486-linux-gnu
Thread model: posix
gcc version 4.1.3 20070812 (prerelease) (Debian 4.1.2-15)

[td]root dmd 
Digital Mars D Compiler v1.018
Copyright (c) 1999-2007 by Digital Mars written by Walter Bright
Sep 07 2007