Note that there are some explanatory texts on larger screens.

plurals
  1. POA class rewritten with templates makes a program slower (in run-time)
    primarykey
    data
    text
    <p>I have a class for a serial-memory 2D array that was initially an array of <code>int</code>s. Now that I need a similar array with another type, I've rewritten the class with templates; the only difference is in the type of stored objects:</p> <pre><code>template &lt;class T&gt; class Serial2DArray { ... T ** Content; } </code></pre> <p>I have a few test functions that deal with Content, for example, a one that nullifies all elements in the array (they're not class members, they're outside functions working with <code>Serial2DArray&lt;int&gt;</code> objects. I've noticed that now it works 1-2% slower - all the other code in the class is untouched, the only difference is that earlier it was just a regular class with <code>int ** Content</code> and now it's a template.</p> <p>A sort of similar question: <a href="https://stackoverflow.com/questions/2442358/do-c-templates-make-programs-slow">Do c++ templates make programs slow?</a> - has opinions that only compilation becomes slower (and I can see why, the compiler generates classes for each that it finds in the code), but here I see the program becoming slower in run-time - is there any rational explanation?</p> <p>Upd: issue narrowed down a little bit here: <a href="https://stackoverflow.com/a/11058672/1200000">https://stackoverflow.com/a/11058672/1200000</a></p> <p>Upd2: as mentioned in the comments, here's the function that became slower:</p> <pre><code>#include &lt;windows.h&gt; #include &lt;mmsystem.h&gt; ... int Size = G_Width * G_Height * sizeof(int); DWORD StartTime = timeGetTime(); for(int i=0; i&lt;100; ++i) { FillMemory(TestArray.Content[0], Size, 0); } MeasuredTime = timeGetTime() - StartTime; </code></pre> <p>And here's the actual class template:</p> <pre><code>#include &lt;malloc.h&gt; template &lt;class T&gt; class Serial2DArray { public: Serial2DArray() { Content = NULL; Width = 0; Height = 0; } Serial2DArray(int _Width, int _Height) { Initialize(_Width, _Height); } ~Serial2DArray() { Deinitialize(); } T ** Content; int GetWidth() { return Width; } int GetHeight() { return Height; } int Initialize(int _Width, int _Height) { // creating pointers to the beginning of each line if((Content = (T **)malloc(_Height * sizeof(T *))) != NULL) { // allocating a single memory chunk for the whole array if((Content[0] = (T *)malloc(_Width * _Height * sizeof(T))) != NULL) { // setting up line pointers' values T * LineAddress = Content[0]; for(int i=0; i&lt;_Height; ++i) { Content[i] = LineAddress; // faster than Content[i] = LineAddress += _Width; // Content[0] + i * _Width; } // everything went ok, setting Width and Height values now Width = _Width; Height = _Height; // success return 1; } else { // insufficient memory available // need to delete line pointers free(Content); return 0; } } else { // insufficient memory available return 0; } } int Resize(int _Width, int _Height) { // deallocating previous array Deinitialize(); // initializing a new one return Initialize(_Width, _Height); } int Deinitialize() { // deleting the actual memory chunk of the array free(Content[0]); // deleting pointers to each line free(Content); // success return 1; } private: int Width; int Height; }; </code></pre> <p>As requested, binaries size comparison.</p> <p>Code with the following: </p> <pre><code>Serial2DArray&lt;int&gt; TestArray; Serial2DArray&lt;int&gt; ZeroArray; </code></pre> <ul> <li>1 016 832 bytes. </li> </ul> <p>Code with the following: </p> <pre><code>Serial2DArray TestArray; // NOT-template class with ints Serial2DArray ZeroArray; // methods are in class declaration </code></pre> <ul> <li>1 016 832 bytes </li> </ul> <p>Code with the following: </p> <pre><code>Serial2DArray&lt;int&gt; TestArray; Serial2DArray&lt;int&gt; ZeroArray; Serial2DArray&lt;double&gt; AnotherArray; Serial2DArray&lt;double&gt; YetAnotherArray; </code></pre> <ul> <li>1 017 344 bytes</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload