<?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[C++ Streams deutlich langsamer als C Streams?]]></title><description><![CDATA[<p>Beim Einlesen grosser Datenmengen konkurrieren beide untenstehende vereinfachte Funktionen. Wobei die Variante mit dem C-Stream etwa 30% flotter ist. Verwende ich die C++ Version nicht korrekt? da es nicht vorhersehbar ist, wie lang eine Eingabezeile ist, bevorzuge ich bisher die C++ Variante.</p>
<p><strong>Version mit C++ Streams</strong></p>
<pre><code class="language-cpp">bool Parse(const std::string&amp; filename) {
  std::ifstream ifs(filename.c_str());
  if (ifs.fail()) {
    return false;
  }

  std::string input;
  for (int line = 1; !getline(ifs, input, '\n').eof(); ++line) {
    // Do some work...
  }
  return true;
}
</code></pre>
<p><strong>Version mit C Streams</strong></p>
<pre><code class="language-cpp">bool Parse(const std::string&amp; filename) {
  FILE* ifs = fopen(filename.c_str(), &quot;r&quot;);
  if (ifs == NULL) {
    return false;
  }

  char buffer[1 &lt;&lt; 16];  // Fixed size :-(
  for (int line = 1; fgets(buffer, 1 &lt;&lt; 16, ifs) != NULL; ++line) {
    std::string input(buffer);
    // Do some work...
  }
  return true;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/286823/c-streams-deutlich-langsamer-als-c-streams</link><generator>RSS for Node</generator><lastBuildDate>Thu, 20 Aug 2026 17:24:52 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/286823.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 17 May 2011 07:42:26 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to C++ Streams deutlich langsamer als C Streams? on Tue, 17 May 2011 11:49:40 GMT]]></title><description><![CDATA[<p>Beim Einlesen grosser Datenmengen konkurrieren beide untenstehende vereinfachte Funktionen. Wobei die Variante mit dem C-Stream etwa 30% flotter ist. Verwende ich die C++ Version nicht korrekt? da es nicht vorhersehbar ist, wie lang eine Eingabezeile ist, bevorzuge ich bisher die C++ Variante.</p>
<p><strong>Version mit C++ Streams</strong></p>
<pre><code class="language-cpp">bool Parse(const std::string&amp; filename) {
  std::ifstream ifs(filename.c_str());
  if (ifs.fail()) {
    return false;
  }

  std::string input;
  for (int line = 1; !getline(ifs, input, '\n').eof(); ++line) {
    // Do some work...
  }
  return true;
}
</code></pre>
<p><strong>Version mit C Streams</strong></p>
<pre><code class="language-cpp">bool Parse(const std::string&amp; filename) {
  FILE* ifs = fopen(filename.c_str(), &quot;r&quot;);
  if (ifs == NULL) {
    return false;
  }

  char buffer[1 &lt;&lt; 16];  // Fixed size :-(
  for (int line = 1; fgets(buffer, 1 &lt;&lt; 16, ifs) != NULL; ++line) {
    std::string input(buffer);
    // Do some work...
  }
  return true;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2064421</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2064421</guid><dc:creator><![CDATA[Tomahawk]]></dc:creator><pubDate>Tue, 17 May 2011 11:49:40 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Streams deutlich langsamer als C Streams? on Tue, 17 May 2011 07:46:10 GMT]]></title><description><![CDATA[<p>Das Thema kommt hier dauernd. Hier der Thread von letzter Woche als Anfangspunkt:</p>
<p><a href="http://www.c-plusplus.net/forum/286372" rel="nofollow">http://www.c-plusplus.net/forum/286372</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2064422</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2064422</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 17 May 2011 07:46:10 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Streams deutlich langsamer als C Streams? on Tue, 17 May 2011 09:10:51 GMT]]></title><description><![CDATA[<p>Zusammenfassung:<br />
C ist schnell, aber C# scheinbar schneller <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>
]]></description><link>https://www.c-plusplus.net/forum/post/2064496</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2064496</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Tue, 17 May 2011 09:10:51 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Streams deutlich langsamer als C Streams? on Tue, 17 May 2011 09:14:07 GMT]]></title><description><![CDATA[<p>Auch ein Grund, warum Google bei OpenSource-Projekten C++ I/O-Streams verbietet?</p>
<p>Aber ich glaube, die haben als Grund auch die angeblich bessere Lesbarkeit der C-Variante angeführt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2064498</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2064498</guid><dc:creator><![CDATA[Roger Wilco]]></dc:creator><pubDate>Tue, 17 May 2011 09:14:07 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Streams deutlich langsamer als C Streams? on Tue, 17 May 2011 11:16:56 GMT]]></title><description><![CDATA[<p>Tomahawk schrieb:</p>
<blockquote>
<p>Beim Einlesen grosser Datenmengen konkurrieren beide untenstehende vereinfachte Funktionen. Wobei die Variante mit dem C-Stream etwa 30% flotter ist. Verwende ich die C++ Version nicht korrekt? da es nicht vorhersehbar ist, wie lang eine Eingabezeile ist, bevorzuge ich bisher die C++ Variante.</p>
</blockquote>
<p>Aber bei diesem beispiel ist eben auch zu berücksichtigen, dass das C Beispiel in ein bereits allokiertes feld schreibt, das C++ Beispile während des lesens auch noch Speicher zu allokieren hat. Je nach dem wie deine reale Implementierung aussieht könnte dies bereits den Unterschied erklären.</p>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2064552</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2064552</guid><dc:creator><![CDATA[Drehleiter]]></dc:creator><pubDate>Tue, 17 May 2011 11:16:56 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Streams deutlich langsamer als C Streams? on Tue, 17 May 2011 11:48:58 GMT]]></title><description><![CDATA[<p>Drehleiter schrieb:</p>
<blockquote>
<p>Tomahawk schrieb:</p>
<blockquote>
<p>Beim Einlesen grosser Datenmengen konkurrieren beide untenstehende vereinfachte Funktionen. Wobei die Variante mit dem C-Stream etwa 30% flotter ist. Verwende ich die C++ Version nicht korrekt? da es nicht vorhersehbar ist, wie lang eine Eingabezeile ist, bevorzuge ich bisher die C++ Variante.</p>
</blockquote>
<p>Aber bei diesem beispiel ist eben auch zu berücksichtigen, dass das C Beispiel in ein bereits allokiertes feld schreibt, das C++ Beispile während des lesens auch noch Speicher zu allokieren hat. Je nach dem wie deine reale Implementierung aussieht könnte dies bereits den Unterschied erklären.</p>
<p>Gruß</p>
</blockquote>
<p>In der C-Variante muss der Speicher doch auch jedesmal dynamisch allokiert werden:</p>
<pre><code class="language-cpp">...
std::string input(buffer);
...
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2064577</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2064577</guid><dc:creator><![CDATA[Tomahawk]]></dc:creator><pubDate>Tue, 17 May 2011 11:48:58 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Streams deutlich langsamer als C Streams? on Tue, 17 May 2011 11:54:37 GMT]]></title><description><![CDATA[<p>Aber nur einmal, da der Buffer nicht größer wird. Die freie getline() Funktion muss da teilweise schon öfters neuen Speicher holen.</p>
<p>Ums zu testen, benutz mal die getline() Memberfunktion oder versuch den String außerhalb zu deklarieren und immer neu zu verwenden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2064580</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2064580</guid><dc:creator><![CDATA[Ethon]]></dc:creator><pubDate>Tue, 17 May 2011 11:54:37 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Streams deutlich langsamer als C Streams? on Tue, 17 May 2011 11:55:02 GMT]]></title><description><![CDATA[<p>Tomahawk schrieb:</p>
<blockquote>
<p>In der C-Variante muss der Speicher doch auch jedesmal dynamisch allokiert werden:</p>
<pre><code class="language-cpp">...
std::string input(buffer);
...
</code></pre>
</blockquote>
<p>Das ist wohl kaum eine C-Variante.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2064581</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2064581</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Tue, 17 May 2011 11:55:02 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Streams deutlich langsamer als C Streams? on Tue, 17 May 2011 12:01:12 GMT]]></title><description><![CDATA[<p>Tachyon schrieb:</p>
<blockquote>
<p>Tomahawk schrieb:</p>
<blockquote>
<p>In der C-Variante muss der Speicher doch auch jedesmal dynamisch allokiert werden:</p>
<pre><code class="language-cpp">...
std::string input(buffer);
...
</code></pre>
</blockquote>
<p>Das ist wohl kaum eine C-Variante.</p>
</blockquote>
<p>Siehe weiter oben, dort habe ich sie als Solche bezeichnet, wegen der Verwendung des File-Deskriptors nicht wegen des std::string.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2064586</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2064586</guid><dc:creator><![CDATA[Tomahawk]]></dc:creator><pubDate>Tue, 17 May 2011 12:01:12 GMT</pubDate></item></channel></rss>