<?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[std::vector, dinamic size array allocation, Compiler flags to improve speed ?]]></title><description><![CDATA[<p dir="auto">Hi i am making a software for multivariable fuzzy k means cluster.</p>
<p dir="auto">It work over big matrix´s 10 variables by 50.000 observations of double data types.<br />
Write the same code two times one with std::vector the other with dynamic array allocation.<br />
Now when use std::vector i get 20 minutes of process time.<br />
when use dynamic array allocation double *a = new double[size] the proccess time<br />
is very fast, a few seconds, but some time i get std::bad_alloc.</p>
<p dir="auto">The matrix not need to grow up o shrink or bounds check.<br />
Only make a resize to the needed size, load items and then make a lot of access.</p>
<p dir="auto">In the web i see many low quality answer about comparation between that or what container<br />
but not have a clear idea.<br />
Questions:</p>
<ol>
<li>
<p dir="auto">There is any flag for the mingw compiler to improvee std::vector speed.</p>
</li>
<li>
<p dir="auto">I am running from the Run command of Qt creator, i read that some sanity checking is performed by some compilers,<br />
but in release will be more faster.<br />
So i put the cluster.exe file in the qt dll folder "C:\Qt\Qt5.4.1\5.4\mingw491_32\bin"<br />
and run but the std::vector performance does not change<br />
Question: if it is true, How make the release in the QtCreator</p>
</li>
<li>
<p dir="auto">What is the origin of std::bad_alloc, there is any way to make a memory reservation<br />
to avoid std::bad_alloc?</p>
</li>
<li>
<p dir="auto">Memory usage comparision,<br />
When the program run using the same dataset, i open the "Resource Monitor" and see:<br />
64mb with std::vector<br />
400mb!!! with dynamic allocation ?<br />
also if run again without closing, the memory increase 600,<br />
then 1.0gb then 1.4 then crash.<br />
I make the propers deletes, but delete does not free the memory.</p>
</li>
</ol>
<p dir="auto">Any help will be welcome.</p>
]]></description><link>https://forum.qt.io/topic/56703/std-vector-dinamic-size-array-allocation-compiler-flags-to-improve-speed</link><generator>RSS for Node</generator><lastBuildDate>Tue, 17 Nov 2026 16:29:35 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/56703.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 24 Jul 2015 14:51:07 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to std::vector, dinamic size array allocation, Compiler flags to improve speed ? on Fri, 24 Jul 2015 21:34:48 GMT]]></title><description><![CDATA[<p dir="auto">Hi<br />
I tried this test program in Release mode and vector was faster than the "new" array?!<br />
(qt 5.5)</p>
<pre><code>#include &lt;QCoreApplication&gt;
#include &lt;QDebug&gt;
#include &lt;QElapsedTimer&gt;
#include &lt;vector&gt;


int main ( int argc, char *argv[] )
{
    QElapsedTimer timer;

    const long int ArrayTestSize = 100000L;
    double *array = new double[ArrayTestSize];
    std::vector&lt;double&gt; array2 ( ArrayTestSize );

    timer.start();

    for ( int c = 0; c &lt; ArrayTestSize; c++ )
    { array[c] = 10.0; }

    qDebug() &lt;&lt; "new nsecs\t" &lt;&lt; timer.nsecsElapsed();

    timer.restart();

    for ( int c = 0; c &lt; ArrayTestSize; c++ )
    { array2[c] = 11.0; }

    qDebug() &lt;&lt; "vector nsecs\t" &lt;&lt; timer.nsecsElapsed();


    delete [] array;
}
</code></pre>
<p dir="auto">The reason for std::bad_alloc can also be that it cannot get a continuous block of the size you request. This can<br />
happen quite fast on 32 bit systems due to fragmentation and 2gb limit pr process.</p>
<p dir="auto">Often when Vector is slower is when one inserts objects due to constructors etc, but you are only inserting doubles?</p>
<p dir="auto">Also<br />
if you not using new, will it still fail?</p>
<p dir="auto">long int ArrayTestSize = 100000L;<br />
double array[ArrayTestSize];</p>
<p dir="auto">-Why delete does not free the memory.<br />
It should free it on delete.<br />
Note. using "Resource Monitor" does not always tell the complete story as might<br />
still show memory use for cached files etc.</p>
<p dir="auto">Where you get your data from ? Loading from files or ?</p>
]]></description><link>https://forum.qt.io/post/283692</link><guid isPermaLink="true">https://forum.qt.io/post/283692</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Fri, 24 Jul 2015 21:34:48 GMT</pubDate></item><item><title><![CDATA[Reply to std::vector, dinamic size array allocation, Compiler flags to improve speed ? on Fri, 24 Jul 2015 18:18:23 GMT]]></title><description><![CDATA[<p dir="auto">Thanks for the reply:</p>
<p dir="auto">Before to load the vector i do:<br />
std::vector&lt;double&gt; U(NumClusters * NumObs,0.0);<br />
Then load with the index:  U[index]=10.0;</p>
<ol start="3">
<li>When calling to new the needed size can by of 100.000 items.<br />
I know hot to catch, but does not know how can reserve memory<br />
to avoid the  std::bad_alloc.</li>
</ol>
<p dir="auto">For the same dataset  the memory usage with std::vector always is 64mb,<br />
with std::vector does not have memory problems.<br />
But in the other version, using dynamic allocation  double *a = new double[size];<br />
and making the proper delete statament : delete [] a;<br />
grow very high 600mb, then if run again 1.0Gb then 1.4Gb and then crash.<br />
Why delete does not free the memory.</p>
<p dir="auto">So the alternatives are speed up std::vector or try to avoid  std::bad_alloc.</p>
<p dir="auto">Greetings</p>
]]></description><link>https://forum.qt.io/post/283667</link><guid isPermaLink="true">https://forum.qt.io/post/283667</guid><dc:creator><![CDATA[marceloarguello700]]></dc:creator><pubDate>Fri, 24 Jul 2015 18:18:23 GMT</pubDate></item><item><title><![CDATA[Reply to std::vector, dinamic size array allocation, Compiler flags to improve speed ? on Fri, 24 Jul 2015 15:18:33 GMT]]></title><description><![CDATA[<p dir="auto">Hi<br />
Did you try with std::vector reserve()  to pre-allocate.<br />
Or test with std::array</p>
<ol>
<li>
<p dir="auto">not that i know of.</p>
</li>
<li>
<p dir="auto">In the Projects tab, you must switch to Release and rebuild all.</p>
</li>
<li>
<p dir="auto">If you call new with a size it can not give you it will throw  std::bad_alloc<br />
You could catch this error and have a look at size at that moment.</p>
</li>
<li>
<p dir="auto">maybe your Size in "double *a = new double[size] " is bigger that it needs to be and<br />
std::vector reflect the real count/size?</p>
</li>
</ol>
<blockquote>
<p dir="auto">also if run again without closing, the memory increase 600<br />
Without code , its hard to really think of something if you delete "a"<br />
and/or call vector.clear()</p>
</blockquote>
<p dir="auto">you could use a tool like<br />
<a href="https://technet.microsoft.com/en-us/library/ff700229.aspx" target="_blank" rel="noopener noreferrer nofollow ugc">https://technet.microsoft.com/en-us/library/ff700229.aspx</a><br />
to double check  the values from "Resource Monitor"</p>
]]></description><link>https://forum.qt.io/post/283652</link><guid isPermaLink="true">https://forum.qt.io/post/283652</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Fri, 24 Jul 2015 15:18:33 GMT</pubDate></item></channel></rss>