<?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[Performantes mehrdimensionales Array gesucht]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich bin auf der Suche nach einem performanten mehrdimensionalen Array, dessen Größe nicht zur Compilezeit feststeht. Ich habe die mir bekannten Implementationen im Stil</p>
<pre><code class="language-cpp">start_time = clock();
  for (int i=0; i&lt;iter; ++i) 
    for (int x=0; x&lt;s1; ++x) 
      for (int y=0; y&lt;s2; ++y) 
	    for (int z=0; z&lt;s3; ++z) 
	      test[x][y][z] = 2.341;
  end_time = clock();
</code></pre>
<p>getestet und es kam raus</p>
<p>Blitz++: 116.37 s<br />
Boost-Multiarray: 8.83 s / 3.68 s (abhängig von der Zugriffsart)<br />
STL-Vector: 0.69 s<br />
C++-eigene Arrays (Mehrfachzeiger): 0.28 s</p>
<p>Daher die Frage: Kennt jemand eine Alternative, die schneller als ein STL-Vector ist, aber mehr Komfort bietet als die C++-eigenen Arrays?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/263968/performantes-mehrdimensionales-array-gesucht</link><generator>RSS for Node</generator><lastBuildDate>Sat, 05 Sep 2026 04:59:52 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/263968.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 30 Mar 2010 15:08:43 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Performantes mehrdimensionales Array gesucht on Tue, 30 Mar 2010 15:08:43 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich bin auf der Suche nach einem performanten mehrdimensionalen Array, dessen Größe nicht zur Compilezeit feststeht. Ich habe die mir bekannten Implementationen im Stil</p>
<pre><code class="language-cpp">start_time = clock();
  for (int i=0; i&lt;iter; ++i) 
    for (int x=0; x&lt;s1; ++x) 
      for (int y=0; y&lt;s2; ++y) 
	    for (int z=0; z&lt;s3; ++z) 
	      test[x][y][z] = 2.341;
  end_time = clock();
</code></pre>
<p>getestet und es kam raus</p>
<p>Blitz++: 116.37 s<br />
Boost-Multiarray: 8.83 s / 3.68 s (abhängig von der Zugriffsart)<br />
STL-Vector: 0.69 s<br />
C++-eigene Arrays (Mehrfachzeiger): 0.28 s</p>
<p>Daher die Frage: Kennt jemand eine Alternative, die schneller als ein STL-Vector ist, aber mehr Komfort bietet als die C++-eigenen Arrays?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1875551</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1875551</guid><dc:creator><![CDATA[ingobulla]]></dc:creator><pubDate>Tue, 30 Mar 2010 15:08:43 GMT</pubDate></item><item><title><![CDATA[Reply to Performantes mehrdimensionales Array gesucht on Tue, 30 Mar 2010 15:18:16 GMT]]></title><description><![CDATA[<p>Hast du die Debug-Informationen weggelassen und Optimierung eingeschaltet?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1875558</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1875558</guid><dc:creator><![CDATA[Janjan]]></dc:creator><pubDate>Tue, 30 Mar 2010 15:18:16 GMT</pubDate></item><item><title><![CDATA[Reply to Performantes mehrdimensionales Array gesucht on Tue, 30 Mar 2010 16:06:50 GMT]]></title><description><![CDATA[<p>Janjan schrieb:</p>
<blockquote>
<p>Hast du die Debug-Informationen weggelassen und Optimierung eingeschaltet?</p>
</blockquote>
<p>Ja, und BOOST_DISABLE_ASSERTS ist gesetzt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1875591</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1875591</guid><dc:creator><![CDATA[ingobulla]]></dc:creator><pubDate>Tue, 30 Mar 2010 16:06:50 GMT</pubDate></item><item><title><![CDATA[Reply to Performantes mehrdimensionales Array gesucht on Tue, 30 Mar 2010 16:09:25 GMT]]></title><description><![CDATA[<p>Kannst du bitte den kompletten Test zum download anbieten, damit man sich von dem Ergebnis selbst ueberzeugen kann?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1875595</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1875595</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Tue, 30 Mar 2010 16:09:25 GMT</pubDate></item><item><title><![CDATA[Reply to Performantes mehrdimensionales Array gesucht on Tue, 30 Mar 2010 16:11:26 GMT]]></title><description><![CDATA[<p>Und falls Du MSVC benutzt, hast Du dann auch _SECURE_SCL auf 0 gesetzt?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1875599</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1875599</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Tue, 30 Mar 2010 16:11:26 GMT</pubDate></item><item><title><![CDATA[Reply to Performantes mehrdimensionales Array gesucht on Tue, 30 Mar 2010 17:54:22 GMT]]></title><description><![CDATA[<p>das taugt als Benchmark nicht! Da Du eine Konstante Zuweisung machst, kann es sein, dass der Compiler für ein C-Array einen Memset macht (zumindest für die letzte Dimension).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1875663</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1875663</guid><dc:creator><![CDATA[gastgast]]></dc:creator><pubDate>Tue, 30 Mar 2010 17:54:22 GMT</pubDate></item><item><title><![CDATA[Reply to Performantes mehrdimensionales Array gesucht on Fri, 02 Apr 2010 07:01:47 GMT]]></title><description><![CDATA[<p>knivil schrieb:</p>
<blockquote>
<p>Kannst du bitte den kompletten Test zum download anbieten, damit man sich von dem Ergebnis selbst ueberzeugen kann?</p>
</blockquote>
<p>Hierbei ist ein seltsames Phänomen aufgetaucht, aber erstmal der Code (es sind 4 Dateien):</p>
<p>test_running_time.cpp</p>
<pre><code class="language-cpp">/* running time comparison: 
   - native matrix
   - boost multi_array
   - vector 
*/

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;ctime&gt;
#include &lt;sys/time.h&gt;
#include &quot;test_class.h&quot;

#define BOOST_DISABLE_ASSERTS
#include &quot;boost/multi_array.hpp&quot;

using namespace boost;
using namespace std;

int main() {
  clock_t start_time;
  clock_t end_time;
  const int ITERATIONS = 10000;
  int X = 10;
  int Y = 3000;
  int Z = 3;

  cout &lt;&lt; &quot;Main: &quot; &lt;&lt; endl;

  // boost multi_array:
  multi_array&lt;double, 3&gt; test_running_time1;
  multi_array&lt;double, 3&gt;::extent_gen ext;
  test_running_time1.resize(ext[X][Y][Z]);

  start_time = clock();
  for (int i=0; i&lt;ITERATIONS; ++i) {
    for (int x=0; x&lt;X; ++x) {
      for (int y=0; y&lt;Y; ++y) {
        for (int z=0; z&lt;Z; ++z) {
          test_running_time1[x][y][z] = 2.341;
        }
      }
    }
  }
  end_time = clock();
  cout &lt;&lt; &quot;Boost: &quot; &lt;&lt; double(end_time-start_time) /CLOCKS_PER_SEC*1000 &lt;&lt; &quot; ms&quot; &lt;&lt; endl;

  // Boost multi_array, different access to elements:
  start_time = clock();
  for (int i=0; i&lt;ITERATIONS; ++i) {
    for (int x=0; x&lt;X; ++x) {
      for (int y=0; y&lt;Y; ++y) {
        for (int z=0; z&lt;Z; ++z) {
          boost::array&lt;multi_array&lt;double,3&gt;::index,3&gt; idx = {{x,y,z}};
          test_running_time1(idx) = 2.341;
        }
      }
    }
  }
  end_time = clock();
  cout &lt;&lt; &quot;Boost diff array access: &quot; &lt;&lt; double(end_time-start_time) /CLOCKS_PER_SEC*1000 &lt;&lt; &quot; ms&quot; &lt;&lt; endl;

  // Native:
  double*** test_running_time2 = new double**[X];
  for (int x=0; x&lt;X; ++x) {
    test_running_time2[x] = new double*[Y];
    for (int y=0; y&lt;Y; ++y) {
      test_running_time2[x][y] = new double[Z];
    }
  }

  start_time = clock();
  for (int i=0; i&lt;ITERATIONS; ++i) {
    for (int x=0; x&lt;X; ++x) {
      for (int y=0; y&lt;Y; ++y) {
        for (int z=0; z&lt;Z; ++z) {
          test_running_time2[x][y][z] = 2.341;
        }
      }
    }
  }
  end_time = clock();
  cout &lt;&lt; &quot;Native: &quot; &lt;&lt; double(end_time-start_time) /CLOCKS_PER_SEC*1000 &lt;&lt; &quot; ms&quot; &lt;&lt; endl;

  // Vectors:
  vector&lt;vector&lt;vector&lt;double&gt; &gt; &gt; test_running_time3(X, vector&lt;vector&lt;double&gt; &gt;(Y, vector&lt;double&gt;(Z)));
  start_time = clock();
  for (int i=0; i&lt;ITERATIONS; ++i) {
    for (int x=0; x&lt;X; ++x) {
      for (int y=0; y&lt;Y; ++y) {
        for (int z=0; z&lt;Z; ++z) {
          test_running_time3[x][y][z] = 2.341;
        }
      }
    }
  }
  end_time = clock();
  cout &lt;&lt; &quot;Vector: &quot; &lt;&lt; double(end_time-start_time) /CLOCKS_PER_SEC*1000 &lt;&lt; &quot; ms&quot; &lt;&lt; endl &lt;&lt; endl;

  TestClass t = TestClass();

  return 0;
}
</code></pre>
<p>test_class.h</p>
<pre><code class="language-cpp">#ifndef _TESTCLASS_H
#define _TESTCLASS_H

using namespace std;

class TestClass {
public:
  TestClass();
  ~TestClass(){};
};

#endif
</code></pre>
<p>test_class.cpp</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;ctime&gt;
#include &lt;sys/time.h&gt;
#include &quot;test_class.h&quot;

#define BOOST_DISABLE_ASSERTS
#include &quot;../../libs/boost/boost/multi_array.hpp&quot;

using namespace boost;
using namespace std;

TestClass::TestClass(){
  clock_t start_time;
  clock_t end_time;
  const int ITERATIONS = 10000;
  int X = 10;
  int Y = 3000;
  int Z = 3;

  cout &lt;&lt; &quot;Test class: &quot; &lt;&lt; endl;

  // boost multi_array:
  multi_array&lt;double, 3&gt; test_running_time1;
  multi_array&lt;double, 3&gt;::extent_gen ext;
  test_running_time1.resize(ext[X][Y][Z]);

  start_time = clock();
  for (int i=0; i&lt;ITERATIONS; ++i) {
    for (int x=0; x&lt;X; ++x) {
      for (int y=0; y&lt;Y; ++y) {
        for (int z=0; z&lt;Z; ++z) {
          test_running_time1[x][y][z] = 2.341;
        }
      }
    }
  }
  end_time = clock();
  cout &lt;&lt; &quot;Boost: &quot; &lt;&lt; double(end_time-start_time) /CLOCKS_PER_SEC*1000 &lt;&lt; &quot; ms&quot; &lt;&lt; endl;

  // Boost multi_array, different access to elements:
  start_time = clock();
  for (int i=0; i&lt;ITERATIONS; ++i) {
    for (int x=0; x&lt;X; ++x) {
      for (int y=0; y&lt;Y; ++y) {
        for (int z=0; z&lt;Z; ++z) {
          boost::array&lt;multi_array&lt;double,3&gt;::index,3&gt; idx = {{x,y,z}};
          test_running_time1(idx) = 2.341;
        }
      }
    }
  }
  end_time = clock();
  cout &lt;&lt; &quot;Boost diff array access: &quot; &lt;&lt; double(end_time-start_time) /CLOCKS_PER_SEC*1000 &lt;&lt; &quot; ms&quot; &lt;&lt; endl;

  // Native:
  double*** test_running_time2 = new double**[X];
  for (int x=0; x&lt;X; ++x) {
    test_running_time2[x] = new double*[Y];
    for (int y=0; y&lt;Y; ++y) {
      test_running_time2[x][y] = new double[Z];
    }
  }

  start_time = clock();
  for (int i=0; i&lt;ITERATIONS; ++i) {
    for (int x=0; x&lt;X; ++x) {
      for (int y=0; y&lt;Y; ++y) {
        for (int z=0; z&lt;Z; ++z) {
          test_running_time2[x][y][z] = 2.341;
        }
      }
    }
  }
  end_time = clock();
  cout &lt;&lt; &quot;Native: &quot; &lt;&lt; double(end_time-start_time) /CLOCKS_PER_SEC*1000 &lt;&lt; &quot; ms&quot; &lt;&lt; endl;

  // Vectors:
  vector&lt;vector&lt;vector&lt;double&gt; &gt; &gt; test_running_time3(X, vector&lt;vector&lt;double&gt; &gt;(Y, vector&lt;double&gt;(Z)));
  start_time = clock();
  for (int i=0; i&lt;ITERATIONS; ++i) {
    for (int x=0; x&lt;X; ++x) {
      for (int y=0; y&lt;Y; ++y) {
        for (int z=0; z&lt;Z; ++z) {
          test_running_time3[x][y][z] = 2.341;
        }
      }
    }
  }
  end_time = clock();
  cout &lt;&lt; &quot;Vector: &quot; &lt;&lt; double(end_time-start_time) /CLOCKS_PER_SEC*1000 &lt;&lt; &quot; ms&quot; &lt;&lt; endl;
}
</code></pre>
<p>makefile</p>
<pre><code class="language-cpp">#
# Makefile for jpHMM.cpp
#

CC	        = g++
OBJ_DIR     = .
CFLAGS	    = -Wall -ansi -pedantic -g0 -ggdb -O3 -DDEBUG
TARGET 	    = test_running_time

INCLS   = -I/home/ingo/libs/boost/ 

SOURCES      = $(wildcard *.cpp)
SOURCES     := $(filter-out test_running_time.cpp, $(SOURCES))
OBJS	     = $(patsubst %.cpp, $(OBJ_DIR)/%.o, $(SOURCES))

$(TARGET): test_running_time.cpp $(OBJS)
	<a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/9067">@echo</a> &quot;$(OBJS)\n&quot;
	$(CC) $(CFLAGS) -o $@ $&lt; $(OBJS) $(INCLS)

$(OBJ_DIR)/%.o : %.cpp
	$(CC) -c $&lt; -o $@  $(INCLS)

clean:
	rm -f $(TARGET) $(TEST_TARGET) $(OBJS)
</code></pre>
<p>Das Programm führt in test_running_time.cpp den Geschwindigkeitsvergleich durch und ruft dann den Konstruktor von TestClass auf, wodurch derselbe Geschwindigkeitsvergleich nochmal ausgeführt wird.</p>
<p>Das Seltsame sind nun die Ergebnisse. Bei mir erhalte ich:</p>
<p>Main:<br />
Boost: 490 ms<br />
Boost diff array access: 540 ms<br />
Native: 640 ms<br />
Vector: 740 ms</p>
<p>Test class:<br />
Boost: 137560 ms<br />
Boost diff array access: 53830 ms<br />
Native: 4440 ms<br />
Vector: 9770 ms</p>
<p>Kann jemand das erklären?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1876503</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1876503</guid><dc:creator><![CDATA[ingobulla]]></dc:creator><pubDate>Fri, 02 Apr 2010 07:01:47 GMT</pubDate></item><item><title><![CDATA[Reply to Performantes mehrdimensionales Array gesucht on Thu, 01 Apr 2010 18:05:52 GMT]]></title><description><![CDATA[<p>wie wärs denn, erst mal den quellcode leserlicher zu gestalten?<br />
Evtl auch die Messung etwas von der eigentlichen Aufgabe abzukoppeln?</p>
<p>und dann so was:</p>
<pre><code class="language-cpp">for (int i=0; i&lt;ITERATIONS; ++i)
{
  for (int x=0; x&lt;X; ++x)
  {
    for (int y=0; y&lt;Y; ++y)
    {
      for (int z=0; z&lt;Z; ++z)
      {
        boost::array&lt;multi_array&lt;double,3&gt;::index,3&gt; idx = {{x,y,z}};
        test_running_time1(idx) = 2.341;
      }
    }
  }
}
</code></pre>
<p>(ich habs mal ordnetlich eingerückt)<br />
wird kein guter compiler mit eingeschalteter optimierung berücksichtigen - die äußere schleife wird einfach wegoptimiert...</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1876545</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1876545</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Thu, 01 Apr 2010 18:05:52 GMT</pubDate></item><item><title><![CDATA[Reply to Performantes mehrdimensionales Array gesucht on Fri, 02 Apr 2010 07:15:01 GMT]]></title><description><![CDATA[<p>Ich habe die Einrückung korrigiert (da kommt die Eingabe wohl nicht mit der gemischten Verwendung von Leerzeichen und Tabs klar).</p>
<p>Unabhängig davon, ob Schleifen wegoptimiert werden: Warum sind die Zeiten zwischen Ausführung in main und Ausführung im Konstruktor einer Klasse so verschieden?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1876708</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1876708</guid><dc:creator><![CDATA[ingobulla]]></dc:creator><pubDate>Fri, 02 Apr 2010 07:15:01 GMT</pubDate></item><item><title><![CDATA[Reply to Performantes mehrdimensionales Array gesucht on Sat, 03 Apr 2010 09:20:07 GMT]]></title><description><![CDATA[<p>Die Ursache ist klar: Im makefile fehlen die CFLAGS beim OBJ_DIR-Ziel.<br />
Korrekt ist:</p>
<pre><code>$(OBJ_DIR)/%.o : %.cpp
	$(CC) $(CFLAGS) -c $&lt; -o $@  $(INCLS)
</code></pre>
<p>In dem Zusammenhang ist vielleicht auch Array2d aus<br />
(<a href="http://www.cppbuch.de/code.html" rel="nofollow">http://www.cppbuch.de/code.html</a>, unten bei 24.10) interessant.<br />
Vorteil: hat im Ggs. zu Boost und Blitz überladene Operatoren,<br />
Nachteil: A) nur 2-dimensional <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f60e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--smiling_face_with_sunglasses"
      title="B)"
      alt="😎"
    /> nur ab G++ 4.4 (muss mit<br />
-std=c++0x statt -ansi übersetzt werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1877069</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1877069</guid><dc:creator><![CDATA[make it right]]></dc:creator><pubDate>Sat, 03 Apr 2010 09:20:07 GMT</pubDate></item></channel></rss>