<?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[Effektiver Code]]></title><description><![CDATA[<p>Hallo,<br />
habe 2 Funktionen um einen String in eine Zahl umzuwandeln. Welche der beiden würdet ihr empfehlen ? Welche ist schneller ? Vielen Dank</p>
<pre><code>int convertToInt( string wert ) {
	int val;
	istringstream is(wert);
	wert &gt;&gt; val;
	return val;
}

int convertToInt( string wert ) {
	return atoi( wert.c_str() );
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/325974/effektiver-code</link><generator>RSS for Node</generator><lastBuildDate>Sat, 11 Jul 2026 16:55:30 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/325974.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 26 May 2014 10:45:47 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Effektiver Code on Mon, 26 May 2014 10:45:47 GMT]]></title><description><![CDATA[<p>Hallo,<br />
habe 2 Funktionen um einen String in eine Zahl umzuwandeln. Welche der beiden würdet ihr empfehlen ? Welche ist schneller ? Vielen Dank</p>
<pre><code>int convertToInt( string wert ) {
	int val;
	istringstream is(wert);
	wert &gt;&gt; val;
	return val;
}

int convertToInt( string wert ) {
	return atoi( wert.c_str() );
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2400900</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2400900</guid><dc:creator><![CDATA[Marius369874]]></dc:creator><pubDate>Mon, 26 May 2014 10:45:47 GMT</pubDate></item><item><title><![CDATA[Reply to Effektiver Code on Mon, 26 May 2014 10:46:48 GMT]]></title><description><![CDATA[<p>Man soll ja C &amp; C++ nicht vermischen habe ich im Forum gelesen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2400901</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2400901</guid><dc:creator><![CDATA[Marius369874]]></dc:creator><pubDate>Mon, 26 May 2014 10:46:48 GMT</pubDate></item><item><title><![CDATA[Reply to Effektiver Code on Mon, 26 May 2014 10:57:36 GMT]]></title><description><![CDATA[<p>Also die FUnktionen atoi und seine Derivate sind zwar erfahrungsgemäßrecht flott, aber da hast du das Problem mit der Fehlererkennung. Z.B. gibt strtol 0 zurück, fall Fehler aufgetreten sind (sagt jedenfalls <a href="http://www.cplusplus.com/reference/cstdlib/strtol/" rel="nofollow">die Referenz</a>).</p>
<p>Ich selbst hab mir ein kleines Template geschrieben, was einen String in einen benutzerdefinierten Typ wandelt:</p>
<pre><code>template&lt;class T&gt;
T convertStr(std::string const&amp; str, T * target = nullptr)
{
    std::stringstream ss(str);
    T tmp;
    ss &gt;&gt; tmp;
    if ( !ss )
        throw std::exception();
    if ( target )
        *target = tmp;
    return tmp;
}
</code></pre>
<p>Ich würd dir also immer den Weg über die Stringstreams raten, einfach weil er im direkten Vergleich mehr C++'iger ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2400906</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2400906</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Mon, 26 May 2014 10:57:36 GMT</pubDate></item><item><title><![CDATA[Reply to Effektiver Code on Mon, 26 May 2014 11:06:34 GMT]]></title><description><![CDATA[<p>@Marius369874 Bei beiden Varianten fehlt die Fehlererkennung.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2400908</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2400908</guid><dc:creator><![CDATA[nwp3]]></dc:creator><pubDate>Mon, 26 May 2014 11:06:34 GMT</pubDate></item><item><title><![CDATA[Reply to Effektiver Code on Mon, 26 May 2014 11:35:49 GMT]]></title><description><![CDATA[<p><s>Wieso nicht <a href="http://en.cppreference.com/w/cpp/string/basic_string/to_string" rel="nofollow">std::to_string()</a> ??<br />
Wäre das nicht am einfachsten, oder überseh ich da was?</s></p>
<p>Blödsinn soll ja andersrum sein.<br />
Aber selbst dann hat der Standard schon Funktionen parat: <a href="http://en.cppreference.com/w/cpp/string/basic_string/stol" rel="nofollow">std::stoi()</a></p>
<p>floorball</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2400911</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2400911</guid><dc:creator><![CDATA[floorball]]></dc:creator><pubDate>Mon, 26 May 2014 11:35:49 GMT</pubDate></item><item><title><![CDATA[Reply to Effektiver Code on Mon, 26 May 2014 13:21:50 GMT]]></title><description><![CDATA[<p>Marius369874 schrieb:</p>
<blockquote>
<p>habe 2 Funktionen um einen String in eine Zahl umzuwandeln. Welche der beiden würdet ihr empfehlen ? Welche ist schneller ? Vielen Dank</p>
<pre><code>int convertToInt( string wert ) {
	int val;
	istringstream is(wert);
	wert &gt;&gt; val;
	return val;
}

int convertToInt( string wert ) {
	return atoi( wert.c_str() );
}
</code></pre>
</blockquote>
<p>Empfehlen würde ich die erstere, wenn Du noch eine Fehlerbehandlung hinzu fügst ..</p>
<pre><code>int convertToInt( string wert ) {
	int val;
	istringstream is(wert);
	is.exceptions( std::ios_base::failbit ); // throw on read error
	wert &gt;&gt; val;
	return val;
}
</code></pre>
<p>... und schneller ist die zweite. So hast Du schnellen Code, der meistens funktioniert <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2400920</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2400920</guid><dc:creator><![CDATA[Werner_logoff]]></dc:creator><pubDate>Mon, 26 May 2014 13:21:50 GMT</pubDate></item><item><title><![CDATA[Reply to Effektiver Code on Mon, 26 May 2014 16:57:25 GMT]]></title><description><![CDATA[<p>Geschwindigkeitsbarometer zur Orientierung (g++/libstdc++):<br />
<strong>stringstreams</strong>: sündhaft langsam, mit optionaler Fehlererkennung<br />
<strong>boost::lexical_cast</strong>: akzeptabel, mit Fehlererkennung<br />
<strong>atoi</strong>: schnell, keine Fehlererkennung<br />
<strong>std::stoi</strong>: schnell, mit teilweiser Fehlererkennung (ignoriert Zeichen nach Zahl)</p>
<p>Wenn du &quot;sehr schnell, mit Fehlererkennung&quot; möchtest, musst du es wohl selbst implementieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2400946</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2400946</guid><dc:creator><![CDATA[fern]]></dc:creator><pubDate>Mon, 26 May 2014 16:57:25 GMT</pubDate></item><item><title><![CDATA[Reply to Effektiver Code on Mon, 26 May 2014 18:21:24 GMT]]></title><description><![CDATA[<p>Wo braucht man eigentlich sehr schnelle Umwandlung von Zeichen zu Zahlen? Irgendwelche Interpreter? Sonst läd man doch beim Starten irgendwelche Strings ein (Configfile oder was auch immer) und wandelt die dann sofort um, welche dann zur Laufzeit schon als Zahlen vorliegen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2400967</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2400967</guid><dc:creator><![CDATA[Namenloser324]]></dc:creator><pubDate>Mon, 26 May 2014 18:21:24 GMT</pubDate></item><item><title><![CDATA[Reply to Effektiver Code on Mon, 26 May 2014 18:34:10 GMT]]></title><description><![CDATA[<p>Namenloser324 schrieb:</p>
<blockquote>
<p>Wo braucht man eigentlich sehr schnelle Umwandlung von Zeichen zu Zahlen? Irgendwelche Interpreter? Sonst läd man doch beim Starten irgendwelche Strings ein (Configfile oder was auch immer) und wandelt die dann sofort um, welche dann zur Laufzeit schon als Zahlen vorliegen.</p>
</blockquote>
<p>Es gibt immer Gründe, warum man Geschwindigkeit braucht. Zum einen könnte das an einer hohen Datenmenge liegen. Dann kann es natürlich auch sein, dass ein Programm schnell starten muss und die Daten aus irgendwelchen Gründen nicht als Binärdaten zur Verfügung gestellt werden können, z.B. weil sie aus einer anderen Quelle kommen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2400968</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2400968</guid><dc:creator><![CDATA[Marthog]]></dc:creator><pubDate>Mon, 26 May 2014 18:34:10 GMT</pubDate></item><item><title><![CDATA[Reply to Effektiver Code on Mon, 26 May 2014 18:35:48 GMT]]></title><description><![CDATA[<p>Namenloser324 schrieb:</p>
<blockquote>
<p>Wo braucht man eigentlich sehr schnelle Umwandlung von Zeichen zu Zahlen? Irgendwelche Interpreter? Sonst läd man doch beim Starten irgendwelche Strings ein (Configfile oder was auch immer) und wandelt die dann sofort um, welche dann zur Laufzeit schon als Zahlen vorliegen.</p>
</blockquote>
<p>Schaden kanns nicht... Ich kann mir viele Stellen vorstellen, wo das mehr oder weniger eine Rolle spielt. Werden zwar wahrscheinlich fast alles irgendwelche &quot;Interpreter&quot; sein, aber das kann ja alles mögliche sein, z.B. große CSV Dateien mit Messergebnissen, irgendwelche Netzwerkprotokolle usw. Und wenns eine versteckte Funktion in einer internen Bibliothek ist, würde ich mich einfach mal drüber freuen, wenn sie so schnell wie möglich funktioniert und nicht einfach aus irgendwelchen fragwürdigen Gründen Rechenzeit verschwendet.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2400969</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2400969</guid><dc:creator><![CDATA[Mechanics]]></dc:creator><pubDate>Mon, 26 May 2014 18:35:48 GMT</pubDate></item><item><title><![CDATA[Reply to Effektiver Code on Mon, 26 May 2014 19:12:06 GMT]]></title><description><![CDATA[<p>Ah okay, ich danke euch <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2400980</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2400980</guid><dc:creator><![CDATA[Namenloser324]]></dc:creator><pubDate>Mon, 26 May 2014 19:12:06 GMT</pubDate></item><item><title><![CDATA[Reply to Effektiver Code on Mon, 26 May 2014 20:57:28 GMT]]></title><description><![CDATA[<p>Mechanics schrieb:</p>
<blockquote>
<p>Schaden kanns nicht...</p>
</blockquote>
<p>Wenn man mit der Zeit, die man benötigt, um was unnötig zu optimieren, bessere Dinge anstellen könnte, ist das ja auch eine Art Schaden. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";-)"
      alt="😉"
    /><br />
Wenn der Code dadurch dann auch noch länger/unleserlicher/schwerer zu warten wird, sowieso. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61b.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_tongue"
      title=":P"
      alt="😛"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2401001</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2401001</guid><dc:creator><![CDATA[Dobi]]></dc:creator><pubDate>Mon, 26 May 2014 20:57:28 GMT</pubDate></item><item><title><![CDATA[Reply to Effektiver Code on Mon, 26 May 2014 21:21:37 GMT]]></title><description><![CDATA[<p>Exakt. Wenn man etwas optimiert damit das Programm 10µs schneller läuft dann ist das ziemlich vergeudet.</p>
<p>Zum Thema: <a href="http://www.kumobius.com/2013/08/c-string-to-int/" rel="nofollow">http://www.kumobius.com/2013/08/c-string-to-int/</a></p>
<p>Für maximale Performance einfach Boost.Spirit nehmen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2401005</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2401005</guid><dc:creator><![CDATA[Ethon]]></dc:creator><pubDate>Mon, 26 May 2014 21:21:37 GMT</pubDate></item><item><title><![CDATA[Reply to Effektiver Code on Mon, 26 May 2014 21:40:06 GMT]]></title><description><![CDATA[<p>Und, treffen diese Argumente hier zu? Ist es deutlich mehr Aufwand oder unleserlicher, hier eine performantere Variante zu verwenden?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2401007</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2401007</guid><dc:creator><![CDATA[Mechanics]]></dc:creator><pubDate>Mon, 26 May 2014 21:40:06 GMT</pubDate></item><item><title><![CDATA[Reply to Effektiver Code on Mon, 26 May 2014 21:44:21 GMT]]></title><description><![CDATA[<p>Kann gut sein, dass es hier nicht zutrifft. Ich wollte ja auch nur was an deiner absoluten Aussage rumzickeln. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /><br />
Wenn es sich allerdings auf 'Wenn du &quot;sehr schnell, mit Fehlererkennung&quot; möchtest, musst du es wohl selbst implementieren.' von fern bezog, könnte ich mir schon vorstellen, dass es zutrifft.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2401008</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2401008</guid><dc:creator><![CDATA[Dobi]]></dc:creator><pubDate>Mon, 26 May 2014 21:44:21 GMT</pubDate></item></channel></rss>