[Dune] [Dune-Commit] dune-grid r7837 - trunk/dune/grid/albertagrid

Oliver Sander sander at mi.fu-berlin.de
Sat Jan 14 23:57:23 CET 2012


I forgot the compiler flags: plain -O3 did it.
--
Oliver

Am 14.01.2012 23:19, schrieb sander at dune-project.org:
> Author: sander
> Date: 2012-01-14 23:19:15 +0100 (Sat, 14 Jan 2012)
> New Revision: 7837
>
> Modified:
>     trunk/dune/grid/albertagrid/indexstack.hh
> Log:
> Use only stl container methods to implement the method topAndPop(),
> rather than relying on the method top_and_pop_back() of ReservedVector,
> which was only implemented there to be used here.
>
> The new implementation contained in this patch uses a temporary
> variable.  However, the compiler completely removes the temporary,
> and hence no slowdown results at all.  I measured this with a test
> programm, which I append at the end of this commit message.
> Running it on my
>
>      gcc (Debian 4.6.2-11) 4.6.2
>
> gave me the following output: (irrelevant parts deleted)
>
> sander at igel:~/dune/dune-common/dune/common/test$ ./toptest
> presumably fast: 10.4207
> presumably slow: 10.3566
>
> Since the special purpose method in ReservedVector does not
> produce any benefit I see not reason for having it, and I will
> consequently remove it again.
>
> Here is the benchmark code:
>
> #include "config.h"
>
> #include<iostream>
> #include<dune/common/reservedvector.hh>
> #include<dune/common/timer.hh>
>
> template<class T, int N>
> struct MyFiniteStack : Dune::ReservedVector<T,N>
> {
>      T top_and_pop_builtin()
>      {
>          return this->top_and_pop_back();
>      }
>
>      T top_and_pop_manual()
>      {
>          T tmp = this->back();
>          this->pop_back();
>          return tmp;
>      }
> };
>
> int main(int argc, char** argv)
> {
>      int n = 1e9;
>
>      MyFiniteStack<double,20>  stack;
>      stack.resize(10);
>      for (int j=0; j<10; j++)
>          stack[j] = j;
>
>      Dune::Timer watch;
>      double sum = 0;
>      for (int i=0; i<n; i++) {
>          stack.resize(10);
>          for (int j=0; j<10; j++)
>              sum += stack.top_and_pop_builtin();
>      }
>
>      std::cout<<  "presumably fast: "<<  watch.elapsed()<<  std::endl;
>      // Use the variable so it doesn't get removed completely
>      std::cout<<  "sum: "<<  sum<<  std::endl;
>
>      ///////////////////////////////////////////////////
>      watch.reset();
>      sum = 0;
>      for (int i=0; i<n; i++) {
>          stack.resize(10);
>          for (int j=0; j<10; j++)
>              sum += stack.top_and_pop_manual();
>      }
>
>      std::cout<<  "presumably slow: "<<  watch.elapsed()<<  std::endl;
>      // Use the variable so it doesn't get removed completely
>      std::cout<<  "sum: "<<  sum<<  std::endl;
>
>    return 0;
> }
>
>
>
> Modified: trunk/dune/grid/albertagrid/indexstack.hh
> ===================================================================
> --- trunk/dune/grid/albertagrid/indexstack.hh	2012-01-13 15:52:18 UTC (rev 7836)
> +++ trunk/dune/grid/albertagrid/indexstack.hh	2012-01-14 22:19:15 UTC (rev 7837)
> @@ -34,7 +34,12 @@
>       {
>         assert( !this->empty() );
>         assert( this->size()<= length );
> -      return this->top_and_pop_back();
> +      // This code is not slower than using the array structure directly.
> +      // The compiler removes the temporary completely.  I measured this.
> +      // See the commit message for revision 7837 for more details.
> +      T tmp = this->back();
> +      this->pop_back();
> +      return tmp;
>       }
>     };
>
>
>
> _______________________________________________
> Dune-Commit mailing list
> Dune-Commit at dune-project.org
> http://lists.dune-project.org/mailman/listinfo/dune-commit




More information about the Dune mailing list