<?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[Schnelles lesen von großen Dateien in den Speicher]]></title><description><![CDATA[<p>Hallo Leute,</p>
<p>ich wollte eine ascii matrizen in den Speicher laden. Leider ist mein Code (irgendwie) sehr langsam. Eigentlich sollte es dank SSD in wenigen Sekunden gehen was auch beim reinen Kopieren der Fall ist. Bremst der cast so stark aus? Wäre für jeden Tipp dankbar wie man es beschleunigen könnte.</p>
<pre><code class="language-cpp">#include &lt;string&gt;
#include &lt;cstdlib&gt; // Für EXIT_SUCCESS
#include &lt;fstream&gt;
#include &lt;iostream&gt;

// Matrizenrechnung
#include &lt;boost/numeric/ublas/io.hpp&gt;
#include &lt;boost/numeric/ublas/matrix.hpp&gt;
#include &lt;boost/numeric/ublas/matrix_proxy.hpp&gt; // Zeilenzuordnung

// String tokenizer
#include &lt;boost/foreach.hpp&gt;
#include &lt;boost/tokenizer.hpp&gt;

// Double Caster
#include &lt;boost/lexical_cast.hpp&gt;

// Abkürzungen
using std::string;
using std::ifstream;
using namespace boost::numeric::ublas;

int main(int arc, char **argv) 
{
	string line;
	std::size_t row = 0;
	boost::char_separator&lt;char&gt; sep(&quot;\t&quot;);

	ifstream infile_N(&quot;../N_rand_10k_x_10k.txt&quot;,ifstream::in);
	ifstream infile_w(&quot;../w_rand_10k_x_10k.txt&quot;,ifstream::in);

	vector&lt;double&gt; v (10000);
	matrix&lt;double&gt; N (10000,10000);

	while( ! infile_N.eof() )
	{
		getline(infile_N,line);		
		boost::tokenizer&lt; boost::char_separator&lt;char&gt;&gt; tokens(line,sep);

		matrix_row&lt;matrix&lt;double&gt; &gt; mr (N, row); 

		BOOST_FOREACH(string t, tokens)
		{
			double elem;

			try
			{
				elem = boost::lexical_cast&lt;double&gt;(t);
			}
			catch (const boost::bad_lexical_cast&amp;)
			{
				return EXIT_FAILURE;
			}
			catch ( ... )
			{
				return EXIT_FAILURE;
			}

			v[row] = elem;
		}

		mr = v;

		row++;
	}

	infile_N.close();

	matrix&lt;double&gt; w (10000,1);

	row = 0;

	while( ! infile_w.eof() )
	{
		getline(infile_w,line);		

		try
		{
			w(row++,1) = boost::lexical_cast&lt;double&gt;(line);
		}
		catch (const boost::bad_lexical_cast&amp;)
		{
			return EXIT_FAILURE;
		}
		catch ( ... )
		{
			return EXIT_FAILURE;
		}
	}

	infile_w.close();

	return EXIT_SUCCESS;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/294067/schnelles-lesen-von-großen-dateien-in-den-speicher</link><generator>RSS for Node</generator><lastBuildDate>Sun, 16 Aug 2026 02:01:34 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/294067.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 17 Oct 2011 06:46:25 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Schnelles lesen von großen Dateien in den Speicher on Mon, 17 Oct 2011 06:46:25 GMT]]></title><description><![CDATA[<p>Hallo Leute,</p>
<p>ich wollte eine ascii matrizen in den Speicher laden. Leider ist mein Code (irgendwie) sehr langsam. Eigentlich sollte es dank SSD in wenigen Sekunden gehen was auch beim reinen Kopieren der Fall ist. Bremst der cast so stark aus? Wäre für jeden Tipp dankbar wie man es beschleunigen könnte.</p>
<pre><code class="language-cpp">#include &lt;string&gt;
#include &lt;cstdlib&gt; // Für EXIT_SUCCESS
#include &lt;fstream&gt;
#include &lt;iostream&gt;

// Matrizenrechnung
#include &lt;boost/numeric/ublas/io.hpp&gt;
#include &lt;boost/numeric/ublas/matrix.hpp&gt;
#include &lt;boost/numeric/ublas/matrix_proxy.hpp&gt; // Zeilenzuordnung

// String tokenizer
#include &lt;boost/foreach.hpp&gt;
#include &lt;boost/tokenizer.hpp&gt;

// Double Caster
#include &lt;boost/lexical_cast.hpp&gt;

// Abkürzungen
using std::string;
using std::ifstream;
using namespace boost::numeric::ublas;

int main(int arc, char **argv) 
{
	string line;
	std::size_t row = 0;
	boost::char_separator&lt;char&gt; sep(&quot;\t&quot;);

	ifstream infile_N(&quot;../N_rand_10k_x_10k.txt&quot;,ifstream::in);
	ifstream infile_w(&quot;../w_rand_10k_x_10k.txt&quot;,ifstream::in);

	vector&lt;double&gt; v (10000);
	matrix&lt;double&gt; N (10000,10000);

	while( ! infile_N.eof() )
	{
		getline(infile_N,line);		
		boost::tokenizer&lt; boost::char_separator&lt;char&gt;&gt; tokens(line,sep);

		matrix_row&lt;matrix&lt;double&gt; &gt; mr (N, row); 

		BOOST_FOREACH(string t, tokens)
		{
			double elem;

			try
			{
				elem = boost::lexical_cast&lt;double&gt;(t);
			}
			catch (const boost::bad_lexical_cast&amp;)
			{
				return EXIT_FAILURE;
			}
			catch ( ... )
			{
				return EXIT_FAILURE;
			}

			v[row] = elem;
		}

		mr = v;

		row++;
	}

	infile_N.close();

	matrix&lt;double&gt; w (10000,1);

	row = 0;

	while( ! infile_w.eof() )
	{
		getline(infile_w,line);		

		try
		{
			w(row++,1) = boost::lexical_cast&lt;double&gt;(line);
		}
		catch (const boost::bad_lexical_cast&amp;)
		{
			return EXIT_FAILURE;
		}
		catch ( ... )
		{
			return EXIT_FAILURE;
		}
	}

	infile_w.close();

	return EXIT_SUCCESS;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2132351</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132351</guid><dc:creator><![CDATA[wm]]></dc:creator><pubDate>Mon, 17 Oct 2011 06:46:25 GMT</pubDate></item><item><title><![CDATA[Reply to Schnelles lesen von großen Dateien in den Speicher on Mon, 17 Oct 2011 07:00:17 GMT]]></title><description><![CDATA[<p>Blinde Vermutungen, wo und warum es langsam ist, ist nicht so professionell. Am besten benutzt man für sowas einen Profiler, der einem das schön analysiert.</p>
<p>Aber man kann natürlich auch erstmal so drauf schauen. Und da fällt mir getline auf. Beim Kopieren einer Datei per wird im optimalen Fall alles komplett gelesen und dann kopiert. Bei getline wird häppschenweise gelesen und dadurch der Buffer des OS oder Hardware nicht genutzt.</p>
<p>Wenn das reine Lesen vom Speichermedium so schnell wie beim kopieren gehen soll, mußt du die Datei komplett lesen und dann erst analysieren.</p>
<p>Das wäre so meine erste Vorkehrung/Versuch, wenn ich keinen Profiler habe.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132352</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132352</guid><dc:creator><![CDATA[Artchi]]></dc:creator><pubDate>Mon, 17 Oct 2011 07:00:17 GMT</pubDate></item><item><title><![CDATA[Reply to Schnelles lesen von großen Dateien in den Speicher on Mon, 17 Oct 2011 07:17:50 GMT]]></title><description><![CDATA[<p>Wenn ich nun dass hier probiere:</p>
<pre><code class="language-cpp">infile_N.seekg(0,std::ios::end);
std::streampos length = infile_N.tellg();
infile_N.seekg(0,std::ios::beg);

vector&lt;char&gt; buffer(length,0);
infile_N.read(&amp;buffer[0],length);
</code></pre>
<p>bekomme ich bad_alloc in Zeile 4. Length ist 1500001881. Die Datei selbst nicht nicht allzu groß. 1.5GB müsste locker in den Speicher gehen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132361</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132361</guid><dc:creator><![CDATA[wm]]></dc:creator><pubDate>Mon, 17 Oct 2011 07:17:50 GMT</pubDate></item><item><title><![CDATA[Reply to Schnelles lesen von großen Dateien in den Speicher on Mon, 17 Oct 2011 07:21:52 GMT]]></title><description><![CDATA[<p>wm schrieb:</p>
<blockquote>
<p>Wenn ich nun dass hier probiere:</p>
<pre><code class="language-cpp">infile_N.seekg(0,std::ios::end);
std::streampos length = infile_N.tellg();
infile_N.seekg(0,std::ios::beg);

vector&lt;char&gt; buffer(length,0);
infile_N.read(&amp;buffer[0],length);
</code></pre>
<p>bekomme ich bad_alloc in Zeile 4. Length ist 1500001881. Die Datei selbst nicht nicht allzu groß. 1.5GB müsste locker in den Speicher gehen.</p>
</blockquote>
<p>Ok das hat sich auch erledigt mit /LARGEADDRESSAWARE<br />
Ich dachte eigentlich dass man diesen Flag nur ab 3GB braucht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132363</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132363</guid><dc:creator><![CDATA[wm]]></dc:creator><pubDate>Mon, 17 Oct 2011 07:21:52 GMT</pubDate></item><item><title><![CDATA[Reply to Schnelles lesen von großen Dateien in den Speicher on Mon, 17 Oct 2011 07:51:08 GMT]]></title><description><![CDATA[<p>Jetzt muss ich doch nochmal fragen in wieweit sich getline verändert.</p>
<pre><code class="language-cpp">infile_N.seekg(0,std::ios::end);
std::streampos length = infile_N.tellg();
infile_N.seekg(0,std::ios::beg);

vector&lt;char&gt; buffer(length,0);
infile_N.read(&amp;buffer[0],length);

std::stringstream localStream;
localStream.rdbuf()-&gt;pubsetbuf(&amp;buffer[0],length);

while( getline( localStream , line ) )
</code></pre>
<p>Im Moment ist es so dass line &quot;&quot; ist. Kann es sein dass die Zeile 9 so nicht in Ordnung ist?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132377</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132377</guid><dc:creator><![CDATA[wm]]></dc:creator><pubDate>Mon, 17 Oct 2011 07:51:08 GMT</pubDate></item><item><title><![CDATA[Reply to Schnelles lesen von großen Dateien in den Speicher on Mon, 17 Oct 2011 09:15:38 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">vector&lt;char&gt; buffer(length,0);
infile_N.read(&amp;buffer[0],length);
</code></pre>
<p>Ist nicht besonders vorteilhaft, wenn es dir darum geht, dass du schnell lesen willst.</p>
<pre><code class="language-cpp">std::vector&lt;char&gt; buffer;
buffer.reserve(length);
</code></pre>
<p>Ist sicher besser.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132413</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132413</guid><dc:creator><![CDATA[Mentras]]></dc:creator><pubDate>Mon, 17 Oct 2011 09:15:38 GMT</pubDate></item><item><title><![CDATA[Reply to Schnelles lesen von großen Dateien in den Speicher on Mon, 17 Oct 2011 09:22:12 GMT]]></title><description><![CDATA[<p>Mentras schrieb:</p>
<blockquote>
<pre><code class="language-cpp">vector&lt;char&gt; buffer(length,0);
infile_N.read(&amp;buffer[0],length);
</code></pre>
<p>Ist nicht besonders vorteilhaft, wenn es dir darum geht, dass du schnell lesen willst.</p>
<pre><code class="language-cpp">std::vector&lt;char&gt; buffer;
buffer.reserve(length);
</code></pre>
<p>Ist sicher besser.</p>
</blockquote>
<p>Nicht nur unnötig, sondern auch falsch. Und die erste Variante sollte unter dem schnellsten sein, was geht.</p>
<p>edit: Und an den Threadersteller: Eine Datei in einen Stringstream lesaen geht auch ohne Umwege:</p>
<pre><code class="language-cpp">stringstream parser;
ifstream fiel;
file &gt;&gt; parser;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2132414</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132414</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 17 Oct 2011 09:22:12 GMT</pubDate></item><item><title><![CDATA[Reply to Schnelles lesen von großen Dateien in den Speicher on Mon, 17 Oct 2011 09:25:09 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>Nicht nur unnötig, sondern auch falsch. Und die erste Variante sollte unter dem schnellsten sein, was geht.</p>
</blockquote>
<p>Dann scheine ich die Dokumentation nicht zu verstehen.</p>
<p>Der Konstruktoraufruf vegrößert den vector um length Bytes und initialisiert jedes Element mit 0 (Auf sein Beispiel bezogen). Also das Äquivalent zu .resize() Methode.</p>
<p>Deswegen dachte ich mir:</p>
<p>Einen riesen großen vector (1500001881 Bytes) mit Nullen zu initialisieren ist sicher langsamer als bloß den Speicher dafür zu reservieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132417</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132417</guid><dc:creator><![CDATA[Mentras]]></dc:creator><pubDate>Mon, 17 Oct 2011 09:25:09 GMT</pubDate></item><item><title><![CDATA[Reply to Schnelles lesen von großen Dateien in den Speicher on Mon, 17 Oct 2011 09:22:52 GMT]]></title><description><![CDATA[<p>Mentras schrieb:</p>
<blockquote>
<p>SeppJ schrieb:</p>
<blockquote>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f62e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_open_mouth"
      title=":open_mouth:"
      alt="😮"
    /> Was? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /> <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /> <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
</blockquote>
<p>Einen riesen großen vector (1500001881 Bytes) mit Nullen zu initialisieren ist sicher langsamer als bloß den Speicher dafür zu reservieren.</p>
</blockquote>
<p>Und wenn du dann drauf zugreifst, kracht es.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132419</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132419</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 17 Oct 2011 09:22:52 GMT</pubDate></item><item><title><![CDATA[Reply to Schnelles lesen von großen Dateien in den Speicher on Mon, 17 Oct 2011 09:30:54 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>Und wenn du dann drauf zugreifst, kracht es.</p>
</blockquote>
<p>Stimmt. Daran habe ich nicht gedacht.<br />
Danke!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132420</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132420</guid><dc:creator><![CDATA[Mentras]]></dc:creator><pubDate>Mon, 17 Oct 2011 09:30:54 GMT</pubDate></item><item><title><![CDATA[Reply to Schnelles lesen von großen Dateien in den Speicher on Mon, 17 Oct 2011 09:28:53 GMT]]></title><description><![CDATA[<p>Mentras schrieb:</p>
<blockquote>
<p>SeppJ schrieb:</p>
<blockquote>
<p>Und wenn du dann drauf zugreifst, kracht es.</p>
</blockquote>
<p>Verstehe ich nicht, könntest du es mir bitte erklären? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f62e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_open_mouth"
      title=":open_mouth:"
      alt="😮"
    /></p>
</blockquote>
<p>Mit std::vector&lt;..&gt;::reserve(..) wird nur der Speicher reserviert, d.h. std::vector&lt;..&gt;::capacity() verändert, nicht aber std::vector&lt;..&gt;::size().</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132422</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132422</guid><dc:creator><![CDATA[theta]]></dc:creator><pubDate>Mon, 17 Oct 2011 09:28:53 GMT</pubDate></item><item><title><![CDATA[Reply to Schnelles lesen von großen Dateien in den Speicher on Mon, 17 Oct 2011 09:36:37 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<pre><code class="language-cpp">stringstream parser;
ifstream fiel;
file &gt;&gt; parser;
</code></pre>
</blockquote>
<p>Das habe ich als erstes ausprobiert, funktioniert so leider nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132427</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132427</guid><dc:creator><![CDATA[wm]]></dc:creator><pubDate>Mon, 17 Oct 2011 09:36:37 GMT</pubDate></item><item><title><![CDATA[Reply to Schnelles lesen von großen Dateien in den Speicher on Mon, 17 Oct 2011 09:39:47 GMT]]></title><description><![CDATA[<p>wm schrieb:</p>
<blockquote>
<p>SeppJ schrieb:</p>
<blockquote>
<pre><code class="language-cpp">stringstream parser;
ifstream fiel;
file &gt;&gt; parser;
</code></pre>
</blockquote>
<p>Das habe ich als erstes ausprobiert, funktioniert so leider nicht.</p>
</blockquote>
<p>Nein wie doof es geht exakt andersherum.</p>
<pre><code class="language-cpp">std::stringstream &lt;&lt; std::ifstream
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2132428</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132428</guid><dc:creator><![CDATA[wm]]></dc:creator><pubDate>Mon, 17 Oct 2011 09:39:47 GMT</pubDate></item><item><title><![CDATA[Reply to Schnelles lesen von großen Dateien in den Speicher on Mon, 17 Oct 2011 10:42:12 GMT]]></title><description><![CDATA[<p>Geht das ganze auch irgendwie ASCII? Irgendwie bekomme ich nur &quot;0040F7D0&quot;</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132456</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132456</guid><dc:creator><![CDATA[wm]]></dc:creator><pubDate>Mon, 17 Oct 2011 10:42:12 GMT</pubDate></item><item><title><![CDATA[Reply to Schnelles lesen von großen Dateien in den Speicher on Mon, 17 Oct 2011 11:08:33 GMT]]></title><description><![CDATA[<p>wm schrieb:</p>
<blockquote>
<p>Geht das ganze auch irgendwie ASCII? Irgendwie bekomme ich nur &quot;0040F7D0&quot;</p>
</blockquote>
<p>Was genau? Stell die Frage mal so, dass man nicht den kompletten Thread lesen muss, um sie zu verstehen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132472</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132472</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 17 Oct 2011 11:08:33 GMT</pubDate></item><item><title><![CDATA[Reply to Schnelles lesen von großen Dateien in den Speicher on Mon, 17 Oct 2011 11:11:10 GMT]]></title><description><![CDATA[<p>Kurze Frage: Ist die Anzahl der Werte je Zeile immer gleich?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132474</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132474</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Mon, 17 Oct 2011 11:11:10 GMT</pubDate></item><item><title><![CDATA[Reply to Schnelles lesen von großen Dateien in den Speicher on Mon, 17 Oct 2011 12:02:35 GMT]]></title><description><![CDATA[<p>Tachyon schrieb:</p>
<blockquote>
<p>Kurze Frage: Ist die Anzahl der Werte je Zeile immer gleich?</p>
</blockquote>
<p>Ich habe verschiedene Dateien mit Matrizen unterschiedlicher Größe. Je Datei sind bleibt die Größe konstant.</p>
<p>SeppJ schrieb:</p>
<blockquote>
<p>Stell die Frage mal so, dass man nicht den kompletten Thread lesen muss, um sie zu verstehen.</p>
</blockquote>
<pre><code class="language-cpp">mit localStream &lt;&lt; inFile_N;

while( getline( localStream , line ) )
{
</code></pre>
<p>kann ich getline nutzen aber die ausgelesenen Werte scheinen nicht ascii codiert zu sein.</p>
<pre><code>&quot;0027F728ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍýýýý««««««««îþîþ&quot;
&quot;0027F728ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍýýýý««««««««îþîþ&quot;
</code></pre>
<p>Wenn ich aber:</p>
<pre><code class="language-cpp">vector&lt;char&gt; buffer(length,0);
infile_N.read(&amp;buffer[0],length);

std::stringstream localStream;
localStream.rdbuf()-&gt;pubsetbuf(&amp;buffer[0],length);

while( getline( localStream , line ) )
{
</code></pre>
<p>benutze dann ist der Inhalt richtig aber getline bringt immer wieder einen leeren string.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132499</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132499</guid><dc:creator><![CDATA[wm]]></dc:creator><pubDate>Mon, 17 Oct 2011 12:02:35 GMT</pubDate></item><item><title><![CDATA[Reply to Schnelles lesen von großen Dateien in den Speicher on Mon, 17 Oct 2011 12:19:26 GMT]]></title><description><![CDATA[<p>wm schrieb:</p>
<blockquote>
<p>Tachyon schrieb:</p>
<blockquote>
<p>Kurze Frage: Ist die Anzahl der Werte je Zeile immer gleich?</p>
</blockquote>
<p>Ich habe verschiedene Dateien mit Matrizen unterschiedlicher Größe. Je Datei sind bleibt die Größe konstant.</p>
</blockquote>
<p>Dann vielleicht so als primitiver Ansatz:</p>
<pre><code class="language-cpp">int main()
{
    std::size_t columns = 0;
    //Anzahl Spalten ermitteln
    {
        std::ifstream reader(&quot;N_rand_10k_x_10k.txt&quot;);
        std::string line;
        std::getline(reader, line);
        std::istringstream isstr(line);
        double dummy;
        while(isstr &gt;&gt; dummy)
        {
            ++columns;
        }
    }
    //Hier beginnt das eigentliche Einlesen
    std::ifstream reader(&quot;N_rand_10k_x_10k.txt&quot;);
    std::vector&lt;double&gt; row(columns);
    std::size_t current_column = 0;
    std::size_t current_row = 0;
    while(reader &gt;&gt; row[current_column])
    {
        ++current_column;
        if(current_column == columns)
        {
            current_column = 0;
            //assign to matrix here
            ++current_row;
        }        
    }

}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2132504</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132504</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Mon, 17 Oct 2011 12:19:26 GMT</pubDate></item><item><title><![CDATA[Reply to Schnelles lesen von großen Dateien in den Speicher on Mon, 17 Oct 2011 12:24:22 GMT]]></title><description><![CDATA[<p>Ach, oben habe ich bei dem Beispiel auch das rdbuf() vergessen. 'Tschuldigung. So geht das:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;

using namespace std;

int main()
{
  ifstream infile(&quot;test.cc&quot;);
  stringstream buffer;
  buffer &lt;&lt; infile.rdbuf();
  string line;
  while (getline(buffer,line))
    cout &lt;&lt; line &lt;&lt; '\n';
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2132508</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132508</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 17 Oct 2011 12:24:22 GMT</pubDate></item><item><title><![CDATA[Reply to Schnelles lesen von großen Dateien in den Speicher on Mon, 17 Oct 2011 12:33:46 GMT]]></title><description><![CDATA[<p>Ich habe in der Zwischenzeit mal mapped_file ausprobiert</p>
<pre><code class="language-cpp">#include &lt;string&gt;
#include &lt;cstdlib&gt; // Für EXIT_SUCCESS
#include &lt;iostream&gt;

#include &lt;boost/foreach.hpp&gt;
#include &lt;boost/tokenizer.hpp&gt;
#include &lt;boost/lexical_cast.hpp&gt;
#include &lt;boost/numeric/ublas/io.hpp&gt;
#include &lt;boost/iostreams/stream.hpp&gt;
#include &lt;boost/numeric/ublas/matrix.hpp&gt;
#include &lt;boost/numeric/ublas/matrix_proxy.hpp&gt;
#include &lt;boost/iostreams/device/mapped_file.hpp&gt;

// Abkürzungen
using boost::iostreams::stream;
using namespace boost::numeric::ublas;
using boost::iostreams::mapped_file_source;

int main( int argc , char **argv ) 
{
	std::string line;
	std::size_t row = 0;
	boost::char_separator&lt;char&gt; sep( &quot;\t&quot; );

	stream&lt;mapped_file_source&gt; infile_N(mapped_file_source(&quot;../N_rand_10k_x_10k.txt&quot;)); 

	if ( ! infile_N.is_open() )
	{
		return EXIT_FAILURE;
	}

	stream&lt;mapped_file_source&gt; infile_w(mapped_file_source(&quot;../w_rand_10k_x_10k.txt&quot;));

	if ( ! infile_w.is_open() )
	{
		return EXIT_FAILURE;
	}

	vector&lt;double&gt; v ( 10000 );
	matrix&lt;double&gt; N ( 10000 , 10000 );

	while( getline( infile_N , line ) )
	{
		boost::tokenizer&lt;boost::char_separator&lt;char&gt;&gt; tokens( line , sep );

		matrix_row&lt;matrix&lt;double&gt; &gt; mr ( N , row ); 

		BOOST_FOREACH( std::string t , tokens )
		{
			double elem;

			try
			{
				elem = boost::lexical_cast&lt;double&gt;( t );
			}
			catch ( const boost::bad_lexical_cast &amp; )
			{
				return EXIT_FAILURE;
			}
			catch ( ... )
			{
				return EXIT_FAILURE;
			}

			v[row] = elem;
		}

		mr = v;

		row++;
	}

	infile_N.close();

	matrix&lt;double&gt; w ( 10000 , 1 );

	row = 0;

	while( ! infile_w.eof() )
	{
		getline(infile_w,line);		

		try
		{
			w( row++ , 1 ) = boost::lexical_cast&lt;double&gt;( line );
		}
		catch ( const boost::bad_lexical_cast &amp; )
		{
			return EXIT_FAILURE;
		}
		catch ( ... )
		{
			return EXIT_FAILURE;
		}
	}

	infile_w.close();

	return EXIT_SUCCESS;
}
</code></pre>
<p>Ist aber immer noch sehr langsam. Mir scheint als ob er diese mapped_file nicht wirklich in den Speicher lädt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132513</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132513</guid><dc:creator><![CDATA[wm]]></dc:creator><pubDate>Mon, 17 Oct 2011 12:33:46 GMT</pubDate></item><item><title><![CDATA[Reply to Schnelles lesen von großen Dateien in den Speicher on Mon, 17 Oct 2011 13:07:49 GMT]]></title><description><![CDATA[<p>Tachyon schrieb:</p>
<blockquote>
<p>Dann vielleicht so als primitiver Ansatz:</p>
</blockquote>
<p>[quote=&quot;SeppJ&quot;So geht das:[/quote]</p>
<p>Habe jetzt mal beide Ansätze ausprobiert und alle laufen ungefähr gleich schnell. Der cast von 10.000 double Werten scheint nicht so schnell zu sein.</p>
<p>Zumindest zeigt der Profiler dass getline() so gut wie keine Zeit mehr kostet.</p>
<p>Danke für eure Hilfe !</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132530</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132530</guid><dc:creator><![CDATA[wm]]></dc:creator><pubDate>Mon, 17 Oct 2011 13:07:49 GMT</pubDate></item><item><title><![CDATA[Reply to Schnelles lesen von großen Dateien in den Speicher on Mon, 17 Oct 2011 13:15:54 GMT]]></title><description><![CDATA[<p>wm schrieb:</p>
<blockquote>
<p>Tachyon schrieb:</p>
<blockquote>
<p>Dann vielleicht so als primitiver Ansatz:</p>
</blockquote>
<p>Habe jetzt mal beide Ansätze ausprobiert und alle laufen ungefähr gleich schnell. Der cast von 10.000 double Werten scheint nicht so schnell zu sein.</p>
<p>Zumindest zeigt der Profiler dass getline() so gut wie keine Zeit mehr kostet.</p>
<p>Danke für eure Hilfe !</p>
</blockquote>
<p>Hmm, 10000 Werte merkt man bei mit gar nicht. Die sind quasi instant verfügbar. Arbeitest Du im Debug-Modus?</p>
<p>Außerdem könntest Du noch irgendwie so versuchen, direkt in die Matrix zu lasen:</p>
<pre><code class="language-cpp">bool is_whitespace(char c)
{
    std::locale loc;
    return std::isspace(c, loc);
}

int main()
{
    std::ifstream reader(&quot;N_rand_10k_x_10k.txt&quot;);
    //Anzahl Zeilen
    std::istreambuf_iterator&lt;char&gt; end;
    std::istreambuf_iterator&lt;char&gt; i(reader.rdbuf());
    std::size_t rows = std::count(i, end, '\n');
    reader.seekg(0);
    //Anzahl Spalten
    std::string line;
    std::getline(reader, line);
    std::size_t cols = 1 + std::count_if(line.begin(), line.end(), is_whitespace);
    reader.seekg(0);
    //Matrix erstellen
    typedef boost::numeric::ublas::matrix&lt;double&gt; matrix_type;
    typedef boost::numeric::ublas::matrix_row&lt;matrix_type&gt; matrix_row_type;
    std::size_t current_row = 0;
    matrix_type m(rows, cols);
    matrix_row_type row(m, current_row);
    std::size_t current_col = 0;
    //Einlesen
    while( (current_row + 1) &lt; m.size2() &amp;&amp; (reader &gt;&gt; row[current_col]))
    {
        ++current_col;
        if(current_col == cols)
        {
            current_col = 0;
            ++current_row;
            row = matrix_row_type(m, current_row);
        }
    }
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2132533</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132533</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Mon, 17 Oct 2011 13:15:54 GMT</pubDate></item><item><title><![CDATA[Reply to Schnelles lesen von großen Dateien in den Speicher on Mon, 17 Oct 2011 13:20:15 GMT]]></title><description><![CDATA[<p>Tachyon schrieb:</p>
<blockquote>
<p>Hmm, 10000 Werte merkt man bei mit gar nicht. Die sind quasi instant verfügbar. Arbeitest Du im Debug-Modus?</p>
</blockquote>
<p>Ich meine 10000 casts je Zeile. Auf meinem NB sind es etwa 100 Zeilen je Sekunde.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132537</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132537</guid><dc:creator><![CDATA[wm]]></dc:creator><pubDate>Mon, 17 Oct 2011 13:20:15 GMT</pubDate></item><item><title><![CDATA[Reply to Schnelles lesen von großen Dateien in den Speicher on Mon, 17 Oct 2011 14:08:36 GMT]]></title><description><![CDATA[<p>Hast du eigentlich mal einen Profiler benutzt, was da so langsam ist? Oftmals ist das Ergebnis recht überraschend.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132560</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132560</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 17 Oct 2011 14:08:36 GMT</pubDate></item><item><title><![CDATA[Reply to Schnelles lesen von großen Dateien in den Speicher on Mon, 17 Oct 2011 14:33:44 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>Hast du eigentlich mal einen Profiler benutzt, was da so langsam ist? Oftmals ist das Ergebnis recht überraschend.</p>
</blockquote>
<p>Ja, im Moment ist es das lexical_cast ... getline braucht &lt; 5% soweit ich es noch in Erinnerung habe.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132569</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132569</guid><dc:creator><![CDATA[wm]]></dc:creator><pubDate>Mon, 17 Oct 2011 14:33:44 GMT</pubDate></item><item><title><![CDATA[Reply to Schnelles lesen von großen Dateien in den Speicher on Mon, 17 Oct 2011 14:44:50 GMT]]></title><description><![CDATA[<p>wm schrieb:</p>
<blockquote>
<p>SeppJ schrieb:</p>
<blockquote>
<p>Hast du eigentlich mal einen Profiler benutzt, was da so langsam ist? Oftmals ist das Ergebnis recht überraschend.</p>
</blockquote>
<p>Ja, im Moment ist es das lexical_cast ... getline braucht &lt; 5% soweit ich es noch in Erinnerung habe.</p>
</blockquote>
<p>Ja, Zahlen parsen ist eben langsam. Wenn das Format nicht zu ungewöhnlich ist, kannst du ja mal das gute alte atof ausprobieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132572</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132572</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 17 Oct 2011 14:44:50 GMT</pubDate></item></channel></rss>