<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[C++0x(VS2010) Move constructor slows down my program?]]></title><description><![CDATA[<p>Hello.</p>
<p>== NOTHING INTERESTING IN THIS BLOCK ==</p>
<p>I have computational program which operates on linear combinations of some objects. Linear combination is array of elements, each element consists of pointer and factor (of type double). Array is sorted by pointer values (duplicate pointer values are prohibited). Typical size of linear combination is 10 elements.</p>
<p>I store arrays in self-made containers similar to <code>std::vector</code> (which double their buffer each time the buffer is full).</p>
<p>I need to add two linear combinations. I do it by algorithm similar to mergesort algorithm (I create 3-rd linear combination for result).</p>
<p>== NOTHING INTERESTING IN THIS BLOCK ==</p>
<p>So, my <code>operator+</code> looks like this:</p>
<pre><code class="language-cpp">Linear Linear::operator+(Linear const &amp;t)
{
	Linear r;

	//... Here I merge *this and t into r

	return r; //Copy constructor called here
}
</code></pre>
<p>I decided to speed up my program by adding trivial move constructors to my classes. After I done this the program became faster in Debug mode (optimizations disabled), but slower in Release mode (optimizations enabled).</p>
<p>Investigation showed that when there is no move-constructor in the class then <code>return r</code> generates no code because <code>operator+</code> is inlined to the callers. But if I add move constructor to the class then this move constructor is always called!</p>
<p>That was the first problem, but it is not very serious (computation time with move constructor is 160 seconds, without it is 159.5 sec.)</p>
<p>The second problem is very strange. Somewhere in my program I have line <code>a = b + c;</code> , where <code>a</code> , <code>b</code> and <code>c</code> are objects of class <code>Linear</code> . I decided to add &quot;move assignment operator&quot; to <code>Linear</code> to speed up this thing. After that, my program slowed down from 160 to 168 seconds!</p>
<p>Why C++0x features slow down my program while they intended to speed it up?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/271270/c-0x-vs2010-move-constructor-slows-down-my-program</link><generator>RSS for Node</generator><lastBuildDate>Sat, 29 Aug 2026 14:58:43 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/271270.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 25 Jul 2010 20:55:14 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to C++0x(VS2010) Move constructor slows down my program? on Sun, 25 Jul 2010 20:55:14 GMT]]></title><description><![CDATA[<p>Hello.</p>
<p>== NOTHING INTERESTING IN THIS BLOCK ==</p>
<p>I have computational program which operates on linear combinations of some objects. Linear combination is array of elements, each element consists of pointer and factor (of type double). Array is sorted by pointer values (duplicate pointer values are prohibited). Typical size of linear combination is 10 elements.</p>
<p>I store arrays in self-made containers similar to <code>std::vector</code> (which double their buffer each time the buffer is full).</p>
<p>I need to add two linear combinations. I do it by algorithm similar to mergesort algorithm (I create 3-rd linear combination for result).</p>
<p>== NOTHING INTERESTING IN THIS BLOCK ==</p>
<p>So, my <code>operator+</code> looks like this:</p>
<pre><code class="language-cpp">Linear Linear::operator+(Linear const &amp;t)
{
	Linear r;

	//... Here I merge *this and t into r

	return r; //Copy constructor called here
}
</code></pre>
<p>I decided to speed up my program by adding trivial move constructors to my classes. After I done this the program became faster in Debug mode (optimizations disabled), but slower in Release mode (optimizations enabled).</p>
<p>Investigation showed that when there is no move-constructor in the class then <code>return r</code> generates no code because <code>operator+</code> is inlined to the callers. But if I add move constructor to the class then this move constructor is always called!</p>
<p>That was the first problem, but it is not very serious (computation time with move constructor is 160 seconds, without it is 159.5 sec.)</p>
<p>The second problem is very strange. Somewhere in my program I have line <code>a = b + c;</code> , where <code>a</code> , <code>b</code> and <code>c</code> are objects of class <code>Linear</code> . I decided to add &quot;move assignment operator&quot; to <code>Linear</code> to speed up this thing. After that, my program slowed down from 160 to 168 seconds!</p>
<p>Why C++0x features slow down my program while they intended to speed it up?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1931637</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1931637</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Sun, 25 Jul 2010 20:55:14 GMT</pubDate></item><item><title><![CDATA[Reply to C++0x(VS2010) Move constructor slows down my program? on Sun, 25 Jul 2010 21:25:25 GMT]]></title><description><![CDATA[<p>Did you take a look at the generated assembler code or did you just measure time? Because the difference between 160s and 159.5 doesn't imply anything at all. Even the eight seconds are possible (with a few samples taken).</p>
<p>Besides, I could imagine that the new C++0x features aren't perfectly implemented yet. What compiler do you use?</p>
<p><em>Edit: I just saw in the title that you are working with Visual Studio 2010.</em> <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1931650</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1931650</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sun, 25 Jul 2010 21:25:25 GMT</pubDate></item><item><title><![CDATA[Reply to C++0x(VS2010) Move constructor slows down my program? on Sun, 25 Jul 2010 23:38:31 GMT]]></title><description><![CDATA[<p>Try this:</p>
<pre><code class="language-cpp">Linear Linear::operator+(Linear t)
{
    // merge *this and t (in-place)
    return t;
}
</code></pre>
<p>And... maybe you didn't implement the move-ctor/move-assignment operator &quot;correctly&quot;?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1931704</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1931704</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Sun, 25 Jul 2010 23:38:31 GMT</pubDate></item><item><title><![CDATA[Reply to C++0x(VS2010) Move constructor slows down my program? on Mon, 26 Jul 2010 06:06:56 GMT]]></title><description><![CDATA[<p>what kind of class is Linear? What members does it have? how did you implement the move operations?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1931752</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1931752</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Mon, 26 Jul 2010 06:06:56 GMT</pubDate></item></channel></rss>