<?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[atof() 10 mal langsamer als eigene implementierung - warum?]]></title><description><![CDATA[<p>Ich hab gerade mal versucht atof() nachzuprogrammieren erstmal ohne erwartungen.<br />
Dann ist mir beim test jedoch aufgefallen, dass mein code 10 mal schneller ist als der von atof().<br />
Jetzt frage ich mich ob das normal ist und warum die funktion atof so langsam ist<br />
(habe ich etwas vergessen oder?)</p>
<p>hier der geschwindigkeitstest:</p>
<pre><code>speed test: atof() vs. my own to_f32()
----------------------------------------------

to_f32: finished after 1343 milliseconds
atof:   finished after 10270 milliseconds
</code></pre>
<p>und hier mein code:</p>
<pre><code>// convert char* to core::u32
	inline core::u32 to_u32(const char* in, const char **out=nullptr){
		core::u32 ret = 0; // return value
		while(*in &gt;= '0' &amp;&amp; *in &lt;= '9'){
			ret = (ret*10) + *in - int('0'); // add
			++in;  // next
		}
		if(out != nullptr) *out = in;
		return ret;
	}

	// convert std::string to core::f32
	inline core::f32 to_f32(const char* in, const char **out=nullptr){
		bool inv = false;
		core::f32 ret = 0.f;
		const char* t;

		// check if the number is negativ or positiv
		if(*in == '-'){
			inv = true;
			++in;
		}

		ret = (float) to_u32(in,&amp;in);

		if(*in == '.'){
			++in;

			core::f32 f = (core::f32) to_u32(in, &amp;t);
			f *= to_f32_table[t-in];

			ret += f;
			in = t;
		}

		if(*in == 'e' || *in == 'E'){
			++in;

			// check if the number is negativ or positiv
			bool e_inv = false;
			if(*in == '-'){
				inv = true;
				++in;
			}else if(*in == '+') ++in;

			core::f32 exp = (core::f32) to_u32(in, &amp;in);
			if(e_inv == true) exp *= -1.0f;

			ret *= (core::f32) pow(10.0f,exp);
		}

		if(out != nullptr) *out = in;
		return ret;
	}
</code></pre>
<pre><code>#include &lt;iostream&gt;
using namespace std;

int main(){
	cout &lt;&lt; &quot;speed test: atof() vs. my own to_f32()\n----------------------------------------------\n&quot; &lt;&lt; endl;

	core::timer t;
	t.start();

	for(__int64 i=0; i&lt;10000000; ++i){
		float f = core::string::to_f32(&quot;324.765e+005&quot;);
	}

	t.stop();
	cout &lt;&lt; &quot;to_f32: finished after &quot; &lt;&lt; t.get_time() &lt;&lt; &quot; milliseconds&quot; &lt;&lt; endl;

	t.reset();
	t.start();

	for(__int64 i=0; i&lt;10000000; ++i){
		float f = atof(&quot;324.765e+005&quot;);
	}

	t.stop();
	cout &lt;&lt; &quot;atof:   finished after &quot; &lt;&lt; t.get_time() &lt;&lt; &quot; milliseconds&quot; &lt;&lt; endl;

	getchar();
	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/314215/atof-10-mal-langsamer-als-eigene-implementierung-warum</link><generator>RSS for Node</generator><lastBuildDate>Sat, 01 Aug 2026 09:48:04 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/314215.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 24 Feb 2013 00:12:39 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to atof() 10 mal langsamer als eigene implementierung - warum? on Sun, 24 Feb 2013 00:12:39 GMT]]></title><description><![CDATA[<p>Ich hab gerade mal versucht atof() nachzuprogrammieren erstmal ohne erwartungen.<br />
Dann ist mir beim test jedoch aufgefallen, dass mein code 10 mal schneller ist als der von atof().<br />
Jetzt frage ich mich ob das normal ist und warum die funktion atof so langsam ist<br />
(habe ich etwas vergessen oder?)</p>
<p>hier der geschwindigkeitstest:</p>
<pre><code>speed test: atof() vs. my own to_f32()
----------------------------------------------

to_f32: finished after 1343 milliseconds
atof:   finished after 10270 milliseconds
</code></pre>
<p>und hier mein code:</p>
<pre><code>// convert char* to core::u32
	inline core::u32 to_u32(const char* in, const char **out=nullptr){
		core::u32 ret = 0; // return value
		while(*in &gt;= '0' &amp;&amp; *in &lt;= '9'){
			ret = (ret*10) + *in - int('0'); // add
			++in;  // next
		}
		if(out != nullptr) *out = in;
		return ret;
	}

	// convert std::string to core::f32
	inline core::f32 to_f32(const char* in, const char **out=nullptr){
		bool inv = false;
		core::f32 ret = 0.f;
		const char* t;

		// check if the number is negativ or positiv
		if(*in == '-'){
			inv = true;
			++in;
		}

		ret = (float) to_u32(in,&amp;in);

		if(*in == '.'){
			++in;

			core::f32 f = (core::f32) to_u32(in, &amp;t);
			f *= to_f32_table[t-in];

			ret += f;
			in = t;
		}

		if(*in == 'e' || *in == 'E'){
			++in;

			// check if the number is negativ or positiv
			bool e_inv = false;
			if(*in == '-'){
				inv = true;
				++in;
			}else if(*in == '+') ++in;

			core::f32 exp = (core::f32) to_u32(in, &amp;in);
			if(e_inv == true) exp *= -1.0f;

			ret *= (core::f32) pow(10.0f,exp);
		}

		if(out != nullptr) *out = in;
		return ret;
	}
</code></pre>
<pre><code>#include &lt;iostream&gt;
using namespace std;

int main(){
	cout &lt;&lt; &quot;speed test: atof() vs. my own to_f32()\n----------------------------------------------\n&quot; &lt;&lt; endl;

	core::timer t;
	t.start();

	for(__int64 i=0; i&lt;10000000; ++i){
		float f = core::string::to_f32(&quot;324.765e+005&quot;);
	}

	t.stop();
	cout &lt;&lt; &quot;to_f32: finished after &quot; &lt;&lt; t.get_time() &lt;&lt; &quot; milliseconds&quot; &lt;&lt; endl;

	t.reset();
	t.start();

	for(__int64 i=0; i&lt;10000000; ++i){
		float f = atof(&quot;324.765e+005&quot;);
	}

	t.stop();
	cout &lt;&lt; &quot;atof:   finished after &quot; &lt;&lt; t.get_time() &lt;&lt; &quot; milliseconds&quot; &lt;&lt; endl;

	getchar();
	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2301502</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2301502</guid><dc:creator><![CDATA[gamer8o4]]></dc:creator><pubDate>Sun, 24 Feb 2013 00:12:39 GMT</pubDate></item><item><title><![CDATA[Reply to atof() 10 mal langsamer als eigene implementierung - warum? on Sun, 24 Feb 2013 01:28:07 GMT]]></title><description><![CDATA[<p>wie sieht denn &quot;to_f32_table&quot; aus?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2301513</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2301513</guid><dc:creator><![CDATA[Optimir0r]]></dc:creator><pubDate>Sun, 24 Feb 2013 01:28:07 GMT</pubDate></item><item><title><![CDATA[Reply to atof() 10 mal langsamer als eigene implementierung - warum? on Sun, 24 Feb 2013 01:38:59 GMT]]></title><description><![CDATA[<p>Du hast auch eine sehr spezielle Variante von atof nachprogrammiert. Das &quot;echte&quot; atof macht noch deutlich mehr checks:<br />
<a href="http://en.cppreference.com/w/cpp/string/byte/atof" rel="nofollow">http://en.cppreference.com/w/cpp/string/byte/atof</a><br />
[<em>]prüfen, ob string mit &quot;0x&quot; oder &quot;0X&quot; beginnt und den string binär interpretieren<br />
[</em>]auf Infinity testen<br />
[*]auf NaN testen<br />
Ob sich das sonst gleich verhält, weiß ich nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2301515</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2301515</guid><dc:creator><![CDATA[asdfasd]]></dc:creator><pubDate>Sun, 24 Feb 2013 01:38:59 GMT</pubDate></item><item><title><![CDATA[Reply to atof() 10 mal langsamer als eigene implementierung - warum? on Sun, 24 Feb 2013 01:41:08 GMT]]></title><description><![CDATA[<p>1. atof erzeugt double, nicht float.<br />
2. atof behandelt unendlich und NaN.<br />
3. atof überspringt führende Leerzeichen.<br />
4. atof kann hexadezimale Darstellung parsen.<br />
5. Locales.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2301516</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2301516</guid><dc:creator><![CDATA[seldon]]></dc:creator><pubDate>Sun, 24 Feb 2013 01:41:08 GMT</pubDate></item><item><title><![CDATA[Reply to atof() 10 mal langsamer als eigene implementierung - warum? on Sun, 24 Feb 2013 02:23:51 GMT]]></title><description><![CDATA[<p>Optimir0r schrieb:</p>
<blockquote>
<p>wie sieht denn &quot;to_f32_table&quot; aus?</p>
</blockquote>
<p>Da werden wohl alle 10^-x drinstehen, wobei x der Anzahl der geparsten Nachkommastellen entspricht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2301522</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2301522</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sun, 24 Feb 2013 02:23:51 GMT</pubDate></item><item><title><![CDATA[Reply to atof() 10 mal langsamer als eigene implementierung - warum? on Sun, 24 Feb 2013 03:03:53 GMT]]></title><description><![CDATA[<p>Zum Thema:<br />
Ich wage zu bezweifeln, dass deine Version 1,798*10^308 in ausgeschriebener Weise parsen kann, und das auch noch so genau wie möglich umrechnet, unter Berücksichtigung von Spracheigenheiten.</p>
<p>Mal das Genauigkeitsbeispiel:</p>
<pre><code class="language-cpp">std::cout &lt;&lt; std::abs(to_f32(&quot;324.765e+005&quot;)-atof(&quot;324.765e+005&quot;))/atof(&quot;324.765e+005&quot;)*100 &lt;&lt; '\n';
</code></pre>
<p>Gibt bei mir einen relativen Fehler von 0.212%, unter der Prämisse, dass mein atof richtig rechnet <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2301525</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2301525</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sun, 24 Feb 2013 03:03:53 GMT</pubDate></item><item><title><![CDATA[Reply to atof() 10 mal langsamer als eigene implementierung - warum? on Sun, 24 Feb 2013 03:04:16 GMT]]></title><description><![CDATA[<p>gamer8o4 schrieb:</p>
<blockquote>
<p>Dann ist mir beim test jedoch aufgefallen, dass mein code 10 mal schneller ist als der von atof().<br />
Jetzt frage ich mich ob das normal ist und warum die funktion atof so langsam ist<br />
(habe ich etwas vergessen oder?)</p>
</blockquote>
<p>Das ist normal, aber ich hätte nur auf den Faktor 2 bis 5 geschätzt. Denn Du schreibst keine Standardbibliothek für alle 10Mrd Menschen, sondern eine für Dich und ein paartausend Freunde. Schlankere Schnittstellen, bescheidenere Wünsche und schon flutscht es.</p>
<p>gamer8o4 schrieb:</p>
<blockquote>
<pre><code>if(out != nullptr) *out = in;
</code></pre>
</blockquote>
<p>Sind wir wieder albern aufgelegt oder haben wir 13 Strafzyklen in der Tasche?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2301527</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2301527</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sun, 24 Feb 2013 03:04:16 GMT</pubDate></item><item><title><![CDATA[Reply to atof() 10 mal langsamer als eigene implementierung - warum? on Sun, 24 Feb 2013 04:18:19 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/28561">@gamer8o4</a><br />
Schreib eine Implementierung die genau das selbe tut wie <code>atof</code> , und dann vergleiche nochmal.</p>
<p>Davon abgesehen: ja, mit selber Parsen kann man einiges rausholen -- meist aber nur deswegen weil man die aktuelle Locale nicht ermitteln muss. Oder eben weil man, wie Du, haufenweise Dinge weglässt.</p>
<p>Ansonsten stell deine Funktion mal auf <code>double</code> um, und vergleich sie dann mit <code>_strtod_l</code> (MSVC -- andere Compiler werden was ähnliches haben).<br />
(Und natürlich ohne die Locale für <code>_strtod_l</code> bei jedem Schleifendurchlauf neu über <code>_get_current_locale</code> zu holen.)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2301533</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2301533</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Sun, 24 Feb 2013 04:18:19 GMT</pubDate></item><item><title><![CDATA[Reply to atof() 10 mal langsamer als eigene implementierung - warum? on Sun, 24 Feb 2013 04:25:45 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<p>Sind wir wieder albern aufgelegt oder haben wir 13 Strafzyklen in der Tasche?</p>
</blockquote>
<p>Dummerweise scheinen die Strafzyklen in Benchmarks vermutlich nicht auf. Weil er dort den TLB und die Branch-Prediction ganz für sich alleine hat, und daher das &quot;if&quot; kaum weh tut.<br />
In realen Programmen werden TLB + Branch-Prediction zwar auch helfen, aber weh tut so ein unnötiges &quot;if&quot; immer noch. Vor allem beim Lesen <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 />
(Und erst recht wenn der Schreiberling meint schnellen Code geschrieben zu haben)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2301534</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2301534</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Sun, 24 Feb 2013 04:25:45 GMT</pubDate></item><item><title><![CDATA[Reply to atof() 10 mal langsamer als eigene implementierung - warum? on Sun, 24 Feb 2013 08:25:56 GMT]]></title><description><![CDATA[<blockquote>
<p>wie sieht denn &quot;to_f32_table&quot; aus?</p>
</blockquote>
<pre><code>static const float to_f32_table[17] = {
		0.f,
		0.1f,
		0.01f,
		0.001f,
		0.0001f,
		0.00001f,
		0.000001f,
		0.0000001f,
		0.00000001f,
		0.000000001f,
		0.0000000001f,
		0.00000000001f,
		0.000000000001f,
		0.0000000000001f,
		0.00000000000001f,
		0.000000000000001f,
		0.0000000000000001f
	};
</code></pre>
<blockquote>
<p>Du hast auch eine sehr spezielle Variante von atof nachprogrammiert. Das &quot;echte&quot; atof macht noch deutlich mehr checks</p>
</blockquote>
<p>das erklärt es naürlich <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /> aber naja, für die meißten meiner anwendungszwecke reicht das so</p>
<blockquote>
<p>Sind wir wieder albern aufgelegt oder haben wir 13 Strafzyklen in der Tasche?</p>
</blockquote>
<p>wenn ich ehrlich bin hab ich mir dabei recht wenig gedacht, ich meine eine 'if'-schleife.... aber gut, ich ändere das mal und versuche noch ein bisschen performance aus der funktion zu bekommen. <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>
<blockquote>
<p>Schreib eine Implementierung die genau das selbe tut wie atof, und dann vergleiche nochmal.</p>
</blockquote>
<p>ehrlich gesagt, dazu fehlern mir schon teilweise die mathematischen kenntnisse. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /></p>
<blockquote>
<p>(Und erst recht wenn der Schreiberling meint schnellen Code geschrieben zu haben)</p>
</blockquote>
<p>nein, das wollte ich damit nicht erreichen, eher dass der parameter nicht angegeben werden muss. Aber warscheinlich löst man das besser, indem ich die funktion überlade!?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2301568</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2301568</guid><dc:creator><![CDATA[gamer8o4]]></dc:creator><pubDate>Sun, 24 Feb 2013 08:25:56 GMT</pubDate></item><item><title><![CDATA[Reply to atof() 10 mal langsamer als eigene implementierung - warum? on Sun, 24 Feb 2013 08:51:26 GMT]]></title><description><![CDATA[<p><a href="http://www.if-schleife.de/" rel="nofollow">http://www.if-schleife.de/</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2301572</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2301572</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sun, 24 Feb 2013 08:51:26 GMT</pubDate></item><item><title><![CDATA[Reply to atof() 10 mal langsamer als eigene implementierung - warum? on Sun, 24 Feb 2013 09:02:02 GMT]]></title><description><![CDATA[<blockquote>
<p><a href="http://www.if-schleife.de/" rel="nofollow">http://www.if-schleife.de/</a></p>
</blockquote>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f921.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--clown_face"
      title=":clown:"
      alt="🤡"
    /> <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f921.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--clown_face"
      title=":clown:"
      alt="🤡"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2301576</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2301576</guid><dc:creator><![CDATA[gamer8o4]]></dc:creator><pubDate>Sun, 24 Feb 2013 09:02:02 GMT</pubDate></item></channel></rss>