<?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[strlen eigenimplementierung?]]></title><description><![CDATA[<p>Hi,</p>
<p>hier wurde oft schonmal eine strlen eigenimplementierung gepostet, leider bringt die suchfunktion zum thema &quot;strlen&quot; nur mist aus.</p>
<p>Kann mir jemand das Topic posten oder ggf. den Code?</p>
<p>Großes Danke im voraus!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/121708/strlen-eigenimplementierung</link><generator>RSS for Node</generator><lastBuildDate>Sat, 22 Aug 2026 20:50:42 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/121708.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 26 Sep 2005 18:12:36 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to strlen eigenimplementierung? on Mon, 26 Sep 2005 18:12:36 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>hier wurde oft schonmal eine strlen eigenimplementierung gepostet, leider bringt die suchfunktion zum thema &quot;strlen&quot; nur mist aus.</p>
<p>Kann mir jemand das Topic posten oder ggf. den Code?</p>
<p>Großes Danke im voraus!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/880730</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/880730</guid><dc:creator><![CDATA[selfmadeboy]]></dc:creator><pubDate>Mon, 26 Sep 2005 18:12:36 GMT</pubDate></item><item><title><![CDATA[Reply to strlen eigenimplementierung? on Mon, 26 Sep 2005 18:24:16 GMT]]></title><description><![CDATA[<p>so wie ich das verstehe, suchst du nen bsp zu strlen.<br />
Hier findest du eins<br />
<a href="http://www.cplusplus.com/ref/cstring/strlen.html" rel="nofollow">lünck</a></p>
<p>und die <a href="http://www.hmug.org/man/3/strlen.php" rel="nofollow">man page</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/880732</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/880732</guid><dc:creator><![CDATA[shapeless]]></dc:creator><pubDate>Mon, 26 Sep 2005 18:24:16 GMT</pubDate></item><item><title><![CDATA[Reply to strlen eigenimplementierung? on Mon, 26 Sep 2005 18:31:10 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">size_t strlen(const char *s)
{
    size_t l = 0;
    while (*s++)
        l++;
    return l;
}
</code></pre>
<p>Ginge natürlich mit haufenweise Tricks noch viel schneller, aber dann würde es nicht mehr in ein paar Zeilen passen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/880739</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/880739</guid><dc:creator><![CDATA[Ringding]]></dc:creator><pubDate>Mon, 26 Sep 2005 18:31:10 GMT</pubDate></item><item><title><![CDATA[Reply to strlen eigenimplementierung? on Mon, 26 Sep 2005 18:31:11 GMT]]></title><description><![CDATA[<p>Kleine Bemerkung: strlen hat kein def. Verhalten bei der Uebergabe eines NULL-Pointers. Meiner Erfahrung nach gibt es dann einen Crash - also immer sicher sein, dass der Parameter nicht NULL ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/880740</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/880740</guid><dc:creator><![CDATA[MBCS-CITP]]></dc:creator><pubDate>Mon, 26 Sep 2005 18:31:11 GMT</pubDate></item><item><title><![CDATA[Reply to strlen eigenimplementierung? on Mon, 26 Sep 2005 19:21:05 GMT]]></title><description><![CDATA[<p>Ringding schrieb:</p>
<blockquote>
<pre><code class="language-cpp">size_t strlen(const char *s)
{
    size_t l = 0;
    while (*s++)
        l++;
    return l;
}
</code></pre>
<p>Ginge natürlich mit haufenweise Tricks noch viel schneller, aber dann würde es nicht mehr in ein paar Zeilen passen.</p>
</blockquote>
<p>ich kenne mindestens einen compiler der hier tatsächlich zweimal pro durchgang inkrementiert. günstiger ist</p>
<pre><code class="language-cpp">size_t strlen(const char *s)
{
    size_t l = 0;
    while (s[l++]);
    return l-1;
}
</code></pre>
<p>und wir haben sogar noch eine zeile gespart.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/880778</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/880778</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Mon, 26 Sep 2005 19:21:05 GMT</pubDate></item><item><title><![CDATA[Reply to strlen eigenimplementierung? on Mon, 26 Sep 2005 19:26:00 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>ich kenne mindestens einen compiler der hier tatsächlich zweimal pro durchgang inkrementiert.</p>
</blockquote>
<p>Was? Das darf aber wirklich nicht sein. So weit glaube ich den Standard doch zu kennen, dass ich meinen Code mit Sicherheit als korrekt hinstellen kann. Würde mich interessieren, welcher Compiler das ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/880783</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/880783</guid><dc:creator><![CDATA[Ringding]]></dc:creator><pubDate>Mon, 26 Sep 2005 19:26:00 GMT</pubDate></item><item><title><![CDATA[Reply to strlen eigenimplementierung? on Mon, 26 Sep 2005 19:43:23 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/6165">@Ringding</a>: Korrekter Code hat noch lange nichts mit performantem Code zu tun.</p>
<p>MfG SideWinder</p>
]]></description><link>https://www.c-plusplus.net/forum/post/880804</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/880804</guid><dc:creator><![CDATA[SideWinder]]></dc:creator><pubDate>Mon, 26 Sep 2005 19:43:23 GMT</pubDate></item><item><title><![CDATA[Reply to strlen eigenimplementierung? on Mon, 26 Sep 2005 19:53:20 GMT]]></title><description><![CDATA[<p>Was hat das mit Performanz zu tun? Ich habe ja extra geschrieben, dass man es viel schneller machen kann, aber dann wird's natürlich auch komplizierter.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/880815</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/880815</guid><dc:creator><![CDATA[Ringding]]></dc:creator><pubDate>Mon, 26 Sep 2005 19:53:20 GMT</pubDate></item><item><title><![CDATA[Reply to strlen eigenimplementierung? on Mon, 26 Sep 2005 19:55:01 GMT]]></title><description><![CDATA[<p>oder so ungefähr:</p>
<p>int strlen (char *p)<br />
{<br />
char *q = p;<br />
while (*p++)<br />
;<br />
return p-q;<br />
}</p>
]]></description><link>https://www.c-plusplus.net/forum/post/880817</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/880817</guid><dc:creator><![CDATA[net 0]]></dc:creator><pubDate>Mon, 26 Sep 2005 19:55:01 GMT</pubDate></item><item><title><![CDATA[Reply to strlen eigenimplementierung? on Mon, 26 Sep 2005 19:59:01 GMT]]></title><description><![CDATA[<p>Ist zwar genau so langsam wie die vorigen Varianten, aber im Unterschied dazu nicht einmal korrekt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/880825</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/880825</guid><dc:creator><![CDATA[Ringding]]></dc:creator><pubDate>Mon, 26 Sep 2005 19:59:01 GMT</pubDate></item><item><title><![CDATA[Reply to strlen eigenimplementierung? on Mon, 26 Sep 2005 20:01:44 GMT]]></title><description><![CDATA[<p>Ringding schrieb:</p>
<blockquote>
<p>Ist zwar genau so langsam wie die vorigen Varianten, aber im Unterschied dazu nicht einmal korrekt.</p>
</blockquote>
<p>nicht? zählt der eins zu weit oder zu wenig?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/880829</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/880829</guid><dc:creator><![CDATA[net 0]]></dc:creator><pubDate>Mon, 26 Sep 2005 20:01:44 GMT</pubDate></item><item><title><![CDATA[Reply to strlen eigenimplementierung? on Mon, 26 Sep 2005 20:02:51 GMT]]></title><description><![CDATA[<p>Eins zu weit.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/880831</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/880831</guid><dc:creator><![CDATA[Ringding]]></dc:creator><pubDate>Mon, 26 Sep 2005 20:02:51 GMT</pubDate></item><item><title><![CDATA[Reply to strlen eigenimplementierung? on Mon, 26 Sep 2005 20:05:14 GMT]]></title><description><![CDATA[<p>Ringding schrieb:</p>
<blockquote>
<p>Eins zu weit.</p>
</blockquote>
<p>aber wenn<br />
char *p = &quot;&quot;;<br />
dan gibt der doch 0 raus, oder?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/880833</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/880833</guid><dc:creator><![CDATA[net 0]]></dc:creator><pubDate>Mon, 26 Sep 2005 20:05:14 GMT</pubDate></item><item><title><![CDATA[Reply to strlen eigenimplementierung? on Mon, 26 Sep 2005 20:49:52 GMT]]></title><description><![CDATA[<p>Ich weiss, eigentlich kein C++, aber bei sowas juckt mir immer der Assembler-Finger:</p>
<pre><code class="language-cpp">size_t strlen( const char* str ) {
   _asm {
      push edi
      push ecx
      pushfd
      cld
      mov edi,str
      mov ecx,0XFFFFFFFF
      xor al,al
      repne scasb
      sub edi,str
      mov eax,edi
      popfd
      pop ecx
      pop edi
   }
}
</code></pre>
<p><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/880877</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/880877</guid><dc:creator><![CDATA[Power Off]]></dc:creator><pubDate>Mon, 26 Sep 2005 20:49:52 GMT</pubDate></item><item><title><![CDATA[Reply to strlen eigenimplementierung? on Mon, 26 Sep 2005 21:47:48 GMT]]></title><description><![CDATA[<p>Darf ich auch mal:</p>
<pre><code class="language-cpp">size_t strlen(const char* s)
{
    const char* p = s;
    while (*p != '\0')
        ++p;
    return p - s;
}
</code></pre>
<p>Ein Implementierung von strlen bringt aber nichts, weil viele Compiler sowas intrinsic drauf haben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/880904</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/880904</guid><dc:creator><![CDATA[groovemaster]]></dc:creator><pubDate>Mon, 26 Sep 2005 21:47:48 GMT</pubDate></item><item><title><![CDATA[Reply to strlen eigenimplementierung? on Tue, 27 Sep 2005 19:56:40 GMT]]></title><description><![CDATA[<p>EDIT: hat sich erledigt</p>
]]></description><link>https://www.c-plusplus.net/forum/post/881681</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/881681</guid><dc:creator><![CDATA[Konfusius]]></dc:creator><pubDate>Tue, 27 Sep 2005 19:56:40 GMT</pubDate></item><item><title><![CDATA[Reply to strlen eigenimplementierung? on Tue, 27 Sep 2005 20:43:06 GMT]]></title><description><![CDATA[<p>Power Off schrieb:</p>
<blockquote>
<pre><code class="language-cpp">size_t strlen( const char* str ) {
   _asm {
      push edi
      push ecx
      pushfd
      cld
      mov edi,str
      mov ecx,0XFFFFFFFF
      xor al,al
      repne scasb
      sub edi,str
      mov eax,edi
      popfd
      pop ecx
      pop edi
   }
}
</code></pre>
</blockquote>
<pre><code class="language-cpp">size_t optimized_strlen (const char* string)
{
  __asm
  {
	mov	eax,string
	test	al,0x03
	jnz	compare
main_loop:
	mov	edx,[eax]
	add	eax,0x04
	mov	ecx,edx
	sub	edx,0x01010101
	and	edx,0x80808080
	jz	main_loop
	not	ecx
	and	edx,ecx
	jz	main_loop
	test	dl,dl
	jnz	lret1
	test	dh,dh
	jnz	lret2
	test	edx,0x00FF0000
	jnz	lret3
	jmp	lret4
compare:
	add	eax,0x04
	test	byte ptr [eax-0x04],0xFF
	jz	lret1
	test	byte ptr [eax-0x03],0xFF
	jz	lret2
	test	byte ptr [eax-02h],0xFF
	jz	lret3
	dec	eax
	and	al,0xFC
	jmp	main_loop
lret1:
	dec	eax
lret2:
	dec	eax
lret3:
	dec	eax
lret4:
	mov	ecx,string
	dec	eax
	sub	eax,ecx
  };
}
</code></pre>
<p>SCNR <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/881710</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/881710</guid><dc:creator><![CDATA[audacia]]></dc:creator><pubDate>Tue, 27 Sep 2005 20:43:06 GMT</pubDate></item><item><title><![CDATA[Reply to strlen eigenimplementierung? on Tue, 27 Sep 2005 20:48:01 GMT]]></title><description><![CDATA[<p>audacia schrieb:</p>
<blockquote>
<pre><code class="language-cpp">size_t optimized_strlen (const char* string)
{
  __asm
  {
	mov	eax,string
	test	al,0x03
	jnz	compare
main_loop:
	mov	edx,[eax]
	add	eax,0x04
	mov	ecx,edx
	sub	edx,0x01010101
	and	edx,0x80808080
	jz	main_loop
	not	ecx
	and	edx,ecx
	jz	main_loop
	test	dl,dl
	jnz	lret1
	test	dh,dh
	jnz	lret2
	test	edx,0x00FF0000
	jnz	lret3
	jmp	lret4
compare:
	add	eax,0x04
	test	byte ptr [eax-0x04],0xFF
	jz	lret1
	test	byte ptr [eax-0x03],0xFF
	jz	lret2
	test	byte ptr [eax-02h],0xFF
	jz	lret3
	dec	eax
	and	al,0xFC
	jmp	main_loop
lret1:
	dec	eax
lret2:
	dec	eax
lret3:
	dec	eax
lret4:
	mov	ecx,string
	dec	eax
	sub	eax,ecx
  };
}
</code></pre>
<p>SCNR <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>Cool!! <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>
<p>Ist das schneller als &quot;rep scasb&quot;? <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="😮"
    /> (wegen der Anzahl der Micro-Ops?)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/881713</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/881713</guid><dc:creator><![CDATA[Power Off]]></dc:creator><pubDate>Tue, 27 Sep 2005 20:48:01 GMT</pubDate></item><item><title><![CDATA[Reply to strlen eigenimplementierung? on Tue, 27 Sep 2005 20:56:03 GMT]]></title><description><![CDATA[<p>Power Off schrieb:</p>
<blockquote>
<p>Ist das schneller als &quot;rep scasb&quot;? <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="😮"
    /> (wegen der Anzahl der Micro-Ops?)</p>
</blockquote>
<p>Sollte schneller sein, da die Daten nicht byteweise, sondern immer als Doublewords geladen und erst in den Registern näher untersucht werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/881717</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/881717</guid><dc:creator><![CDATA[audacia]]></dc:creator><pubDate>Tue, 27 Sep 2005 20:56:03 GMT</pubDate></item></channel></rss>