title: What is taking so long?
body: <p>I am playing a round with a lib for general purpose industrial automation. For that I need a general purpose IO class.</p>
  
  <p><strong>IO.h</strong></p>
  
  <pre><code>template&lt;class T&gt; class IOImpl {}; // pimpl idiom
  template&lt;class T&gt; class Input;
  
  /** IO Class
   * A thread safe variable that can be reduced to a read only object
   */
  template&lt;class T&gt;
  class IO : public AbstractHMIVar
  {
  public:
      IO( std::string name = "" );
      IO( const IO&lt;T&gt; &amp;io );
      const T value() const;
      void setValue( const T value );
      Input&lt;T&gt; toInput() const;
      IO&lt;T&gt; operator-=( const T &amp;other );
      IO&lt;T&gt; operator+=( const T &amp;other );
      IO&lt;T&gt; operator--();
      IO&lt;T&gt; operator++();
      const std::string stringval() const;
      void fromString( const std::string val );
  
  
  private:
      std::shared_ptr&lt;IOImpl&lt;T&gt;&gt; pimpl_;
      std::shared_ptr&lt;std::pair&lt;T, std::mutex&gt;&gt; data;
  };
  </code></pre>
  
  <p><strong>IO.cpp</strong></p>
  
  <pre><code>/** Default constructor
   * Create data object
   */
  template&lt;class T&gt;
  IO&lt;T&gt;::IO( std::string name ) : AbstractHMIVar( name ),
                                  pimpl_( std::make_shared&lt;IOImpl&lt;T&gt;&gt;() ),
                                  data( std::make_shared&lt;std::pair&lt;T, std::mutex&gt;&gt;() )
  {
  }
  
  /** Copy constructor
   * Don't allow deep copies. Only copy the shared pointer.
   */
  template&lt;class T&gt;
  IO&lt;T&gt;::IO( const IO&lt;T&gt; &amp;io ) : pimpl_( io.pimpl_ ), data( io.data )
  {
  }
  
  /** Return the value part of data pair
   */
  template&lt;class T&gt;
  const T IO&lt;T&gt;::value() const
  {
      std::lock_guard&lt;std::mutex&gt; lock{ data-&gt;second };
  
      return data-&gt;first;
  }
  
  /** Return a read only version of this object
   */
  template&lt;class T&gt;
  Input&lt;T&gt; IO&lt;T&gt;::toInput() const
  {
      return Input&lt;T&gt;( *this );
  }
  
  template&lt;class T&gt;
  IO&lt;T&gt; IO&lt;T&gt;::operator--()
  { 
      static_assert( std::is_arithmetic&lt;T&gt;::value, "Type needs to be arithmetic." );
  
      std::lock_guard&lt;std::mutex&gt; lock{ data-&gt;second };
      data-&gt;first -= 1;
  
      return *this;
  }
  
  template class IO&lt;int32_t&gt;;
  </code></pre>
  
  <p><strong>The problem</strong></p>
  
  <p>I composed a little program to test the performance of this IO class.</p>
  
  <pre><code>for( auto i = 0; i &lt; n_tests; i++ )
  {
      IO&lt;uint32_t&gt; io_test( "Testvar" ); //unsigned int test
      io_test.setValue( n_iterations );  //test = n_iterations
      high_resolution_clock::time_point before = high_resolution_clock::now();
      while( io_test.value() &gt; 0 ) //while( test &gt; 0 )
          --io_test;               //--test
  
      results.push_back( duration_cast&lt;milliseconds&gt;( high_resolution_clock::now() - before ) );
  }
  </code></pre>
  
  <p>Running 80 000 000 iterations takes 11.7 seconds.
  Using a normal uint32_t without the IO class runs the same code in 0.0662 seconds.</p>
  
  <p>I tried to skip the shared pointer and only using a value without shared pointer and mutex locking. But that took just as long.</p>
  
  <p>I also tried different optimization levels while compiling. No difference.</p>
  
  <p>Why is it taking this long?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I tried changing the following:</p>
  
  <pre><code>template&lt;class T&gt;
  IO&lt;T&gt; IO&lt;T&gt;::operator--()
  { 
      testvar -= 1;
      return *this;
  }
  
  template&lt;class T&gt;
  const T IO&lt;T&gt;::value() const
  {
      return testvar;
  }
  </code></pre>