qobject_cast

Olivier Goffart ogoffart at kde.org
Sat Nov 12 11:18:28 CET 2005


Hello.

When browsing the kdelibs code, i seen sometimes in the code few qobject_cast 
when i would used dynamic_cast

So my question was, what's the difference between qobject_cast and 
dynamic_cast ?

After having doing some test, it seems that qobject_cast is 3 times faster 
than dynamic_cast.

So I'll change my habit, and use qobject_cast instead of dynamic_cast when 
casting objects.

Of course, I must continue to use static_cast which is still 10 times faster 
than qobject_cast when i know what my object is.


The attached test, compiled with -O2
no cast: 11 ms
static_cast: 19ms   ratio: 1.72727
dynamic_cast: 872ms   ratio: 79.2727
qobject_cast: 290ms   ratio: 26.3636   dynamic ratio :3.0069

note that when i compile with -O0 ,  static_cast is faster than no cast, 
strange.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_cast.cpp
Type: text/x-c++src
Size: 1167 bytes
Desc: not available
Url : http://mail.kde.org/pipermail/kde-optimize/attachments/20051112/2ccf3770/test_cast.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_cast.h
Type: text/x-c++hdr
Size: 150 bytes
Desc: not available
Url : http://mail.kde.org/pipermail/kde-optimize/attachments/20051112/2ccf3770/test_cast-0001.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://mail.kde.org/pipermail/kde-optimize/attachments/20051112/2ccf3770/attachment.pgp


More information about the Kde-optimize mailing list