<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: C++ dynamic_cast Performance</title>
	<atom:link href="http://tinodidriksen.com/2010/04/14/cpp-dynamic-cast-performance/feed/" rel="self" type="application/rss+xml" />
	<link>http://tinodidriksen.com/2010/04/14/cpp-dynamic-cast-performance/</link>
	<description>The words and ideas of Tino Didriksen</description>
	<lastBuildDate>Tue, 17 Apr 2012 21:03:45 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: Cyril</title>
		<link>http://tinodidriksen.com/2010/04/14/cpp-dynamic-cast-performance/#comment-10083</link>
		<dc:creator>Cyril</dc:creator>
		<pubDate>Wed, 27 Oct 2010 12:52:08 +0000</pubDate>
		<guid isPermaLink="false">http://tinodidriksen.com/?p=269#comment-10083</guid>
		<description>Indeed.
Looks like on MSVC, dynamic_casts are implemented with ugly recursive strcmps. Btw, Qt users should always resort to qobject_cast instead, which only does a pointer comparison by inheritance level.

Thanks for your answer and your nice article!</description>
		<content:encoded><![CDATA[<p>Indeed.<br />
Looks like on MSVC, dynamic_casts are implemented with ugly recursive strcmps. Btw, Qt users should always resort to qobject_cast instead, which only does a pointer comparison by inheritance level.</p>
<p>Thanks for your answer and your nice article!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tino Didriksen</title>
		<link>http://tinodidriksen.com/2010/04/14/cpp-dynamic-cast-performance/#comment-10082</link>
		<dc:creator>Tino Didriksen</dc:creator>
		<pubDate>Wed, 27 Oct 2010 12:43:04 +0000</pubDate>
		<guid isPermaLink="false">http://tinodidriksen.com/?p=269#comment-10082</guid>
		<description>Part of the point was that it is much much faster to code around dynamic_cast any way you can, including designing the program so that you will always know the type.

So you did not lose the point. This is just to show new coders how horribly slow dynamic_cast is and why they should design to avoid it.</description>
		<content:encoded><![CDATA[<p>Part of the point was that it is much much faster to code around dynamic_cast any way you can, including designing the program so that you will always know the type.</p>
<p>So you did not lose the point. This is just to show new coders how horribly slow dynamic_cast is and why they should design to avoid it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cyril</title>
		<link>http://tinodidriksen.com/2010/04/14/cpp-dynamic-cast-performance/#comment-10078</link>
		<dc:creator>Cyril</dc:creator>
		<pubDate>Wed, 27 Oct 2010 08:16:25 +0000</pubDate>
		<guid isPermaLink="false">http://tinodidriksen.com/?p=269#comment-10078</guid>
		<description>I think it is worth noting that reinterpret_cast is a null-cost operation from the CPU point of view since it does not translate to any actual machine code after compilation. What you measure here is the cost of the benchmark loop itself as well as the call to getType(), which you don&#039;t need to do if you know the actual type of the object.
Where did I lose the point?</description>
		<content:encoded><![CDATA[<p>I think it is worth noting that reinterpret_cast is a null-cost operation from the CPU point of view since it does not translate to any actual machine code after compilation. What you measure here is the cost of the benchmark loop itself as well as the call to getType(), which you don&#8217;t need to do if you know the actual type of the object.<br />
Where did I lose the point?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steven Lubars</title>
		<link>http://tinodidriksen.com/2010/04/14/cpp-dynamic-cast-performance/#comment-9888</link>
		<dc:creator>Steven Lubars</dc:creator>
		<pubDate>Fri, 01 Oct 2010 16:06:19 +0000</pubDate>
		<guid isPermaLink="false">http://tinodidriksen.com/?p=269#comment-9888</guid>
		<description>Although to be avoided when possible, applications sometimes need to perform explicit RTTI.  Several techniques exist to improve upon the performance of using dynamic_cast for this purpose:

1) Wite a wrapper around type_info that allows comparison, assignment, etc. (see Alexandrescu).

2) I recently encountered another technique at http://ciaranm.wordpress.com/2010/05/24/runtime-type-checking-in-c-without-rtti/ that assigns a unique integer to each type:  

&lt;code&gt;
#include &lt;iostream&gt;
using namespace std;

short TypeId() {
&#160;&#160;&#160;&#160;static short typeId = 0;
&#160;&#160;&#160;&#160;return typeId++;
}

template &lt;typename T&gt;
short TypeId() {
&#160;&#160;&#160;&#160;static short typeId = TypeId();
&#160;&#160;&#160;&#160;return typeId;
}

int main() {
&#160;&#160;&#160;&#160;cout &lt;&lt; TypeId&lt;int&gt;() &lt;&lt; endl;
&#160;&#160;&#160;&#160;cout &lt;&lt; TypeId&lt;short&gt;() &lt;&lt; endl;
&#160;&#160;&#160;&#160;cout &lt;&lt; TypeId&lt;float&gt;() &lt;&lt; endl;
&#160;&#160;&#160;&#160;cout &lt;&lt; TypeId&lt;int&gt;() &lt;&lt; endl;
}
&lt;/code&gt;
&lt;code&gt;
0
1
2
0
&lt;/code&gt;

Of course this works for complex types as well.

I think it would be interesting to compare the performance of these techniques to dynamic_cast for the purpose of explicit RTTI.</description>
		<content:encoded><![CDATA[<p>Although to be avoided when possible, applications sometimes need to perform explicit RTTI.  Several techniques exist to improve upon the performance of using dynamic_cast for this purpose:</p>
<p>1) Wite a wrapper around type_info that allows comparison, assignment, etc. (see Alexandrescu).</p>
<p>2) I recently encountered another technique at <a href="http://ciaranm.wordpress.com/2010/05/24/runtime-type-checking-in-c-without-rtti/" rel="nofollow">http://ciaranm.wordpress.com/2010/05/24/runtime-type-checking-in-c-without-rtti/</a> that assigns a unique integer to each type:  </p>
<p><code><br />
#include &lt;iostream&gt;<br />
using namespace std;</p>
<p>short TypeId() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;static short typeId = 0;<br />
&nbsp;&nbsp;&nbsp;&nbsp;return typeId++;<br />
}</p>
<p>template &lt;typename T&gt;<br />
short TypeId() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;static short typeId = TypeId();<br />
&nbsp;&nbsp;&nbsp;&nbsp;return typeId;<br />
}</p>
<p>int main() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; TypeId&lt;int&gt;() &lt;&lt; endl;<br />
&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; TypeId&lt;short&gt;() &lt;&lt; endl;<br />
&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; TypeId&lt;float&gt;() &lt;&lt; endl;<br />
&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; TypeId&lt;int&gt;() &lt;&lt; endl;<br />
}<br />
</code><br />
<code><br />
0<br />
1<br />
2<br />
0<br />
</code></p>
<p>Of course this works for complex types as well.</p>
<p>I think it would be interesting to compare the performance of these techniques to dynamic_cast for the purpose of explicit RTTI.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tino Didriksen</title>
		<link>http://tinodidriksen.com/2010/04/14/cpp-dynamic-cast-performance/#comment-8738</link>
		<dc:creator>Tino Didriksen</dc:creator>
		<pubDate>Fri, 16 Apr 2010 17:12:56 +0000</pubDate>
		<guid isPermaLink="false">http://tinodidriksen.com/?p=269#comment-8738</guid>
		<description>I agree, but that would be a lot of extra work...

Right now, I can develop and run the tests simultaneously on my own Windows computer and via SSH on the Linux machine. Having to reboot to Linux just to re-run the tests would be painful. There is also the Cygwin or MinGW options, but while they are nice they really cannot be called a representative picture of how g++ performs.

Plus, the tests I do are not about compiler vs. compiler; they&#039;re about general methods and which ones reliably perform well cross-platform.</description>
		<content:encoded><![CDATA[<p>I agree, but that would be a lot of extra work&#8230;</p>
<p>Right now, I can develop and run the tests simultaneously on my own Windows computer and via SSH on the Linux machine. Having to reboot to Linux just to re-run the tests would be painful. There is also the Cygwin or MinGW options, but while they are nice they really cannot be called a representative picture of how g++ performs.</p>
<p>Plus, the tests I do are not about compiler vs. compiler; they&#8217;re about general methods and which ones reliably perform well cross-platform.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jorn</title>
		<link>http://tinodidriksen.com/2010/04/14/cpp-dynamic-cast-performance/#comment-8736</link>
		<dc:creator>Jorn</dc:creator>
		<pubDate>Fri, 16 Apr 2010 13:53:31 +0000</pubDate>
		<guid isPermaLink="false">http://tinodidriksen.com/?p=269#comment-8736</guid>
		<description>It would be nice to see windows tested on a comparable machine</description>
		<content:encoded><![CDATA[<p>It would be nice to see windows tested on a comparable machine</p>
]]></content:encoded>
	</item>
</channel>
</rss>

