Boost C++ Libraries: Ticket #2718: local_date_time noticeably faster than ptime https://svn.boost.org/trac10/ticket/2718 <p> We noticed recently that using a local_date_time in tight loops was several times faster than using a ptime object. As these are both based largerly on the same template code this was a surprise. It appears this is caused by the implementation of the operater+=/-= and was wondering if there was any justification for the difference or if this was just an oversight. </p> <div class="wiki-code"><div class="code"><pre> <span class="c1">//.\boost\date_time\local_time\local_date_time.hpp</span> <span class="n">local_date_time_base</span> <span class="k">operator</span><span class="o">+=</span><span class="p">(</span><span class="k">const</span> <span class="n">time_duration_type</span><span class="o">&amp;</span> <span class="n">td</span><span class="p">)</span> <span class="p">{</span> <span class="k">this</span><span class="o">-&gt;</span><span class="n">time_</span> <span class="o">=</span> <span class="n">time_system_type</span><span class="o">::</span><span class="n">add_time_duration</span><span class="p">(</span><span class="k">this</span><span class="o">-&gt;</span><span class="n">time_</span><span class="p">,</span><span class="n">td</span><span class="p">);</span> <span class="k">return</span> <span class="o">*</span><span class="k">this</span><span class="p">;</span> <span class="p">}</span> </pre></div></div><div class="wiki-code"><div class="code"><pre> <span class="c1">//.\boost\date_time\time.hpp</span> <span class="n">time_type</span> <span class="k">operator</span><span class="o">+=</span><span class="p">(</span><span class="k">const</span> <span class="n">time_duration_type</span><span class="o">&amp;</span> <span class="n">td</span><span class="p">)</span> <span class="p">{</span> <span class="n">time_</span> <span class="o">=</span> <span class="p">(</span><span class="n">time_system</span><span class="o">::</span><span class="n">get_time_rep</span><span class="p">(</span><span class="n">date</span><span class="p">(),</span> <span class="n">time_of_day</span><span class="p">()</span> <span class="o">+</span> <span class="n">td</span><span class="p">));</span> <span class="k">return</span> <span class="nf">time_type</span><span class="p">(</span><span class="n">time_</span><span class="p">);</span> <span class="p">}</span> </pre></div></div><p> It looks like the ptime implementation is doing extra work to split the ptime into date and time_duration components and if it is changed to follow a similar pattern to that used in the local time the performance is then indistinguishable. </p> <div class="wiki-code"><div class="code"><pre> <span class="c1">//.\boost\date_time\time.hpp</span> <span class="n">time_type</span> <span class="k">operator</span><span class="o">+=</span><span class="p">(</span><span class="k">const</span> <span class="n">time_duration_type</span><span class="o">&amp;</span> <span class="n">td</span><span class="p">)</span> <span class="p">{</span> <span class="k">this</span><span class="o">-&gt;</span><span class="n">time_</span> <span class="o">=</span> <span class="n">time_system</span><span class="o">::</span><span class="n">add_time_duration</span><span class="p">(</span><span class="k">this</span><span class="o">-&gt;</span><span class="n">time_</span><span class="p">,</span> <span class="n">td</span><span class="p">);</span> <span class="k">return</span> <span class="nf">time_type</span><span class="p">(</span><span class="n">time_</span><span class="p">);</span> <span class="p">}</span> </pre></div></div><p> I can only think that maybe this was done to work around an issue and in which case should the same then be applied to the local time implementation? </p> <p> <a class="ext-link" href="http://www.nabble.com/user/SendEmail.jtp?type=user&amp;user=627065"><span class="icon">​</span>email via nabble</a> </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/2718 Trac 1.4.3