Couldn't help it...

The original algo that Hutch posted by Jens I believe came out as the best integer algo for finding strlen. That or Agners... However, I wrote up a quick algo that ran about 30% faster on my machine then either routine. I think it's something simple we all overlooked. Some of you might call this cheating... but...

1) instead of appending a 0 byte to the string append a 0 dword
2) test every 4th byte for 0:

mov eax, lptext

cmp BYTE PTR [eax], 0
lea eax,[eax+4]
jnz @B

3) "search" back for the first 0 byte


I know some people might complain about the extra 3 bytes of 0s but hey, for an algo this small, who cares?


Posted on 2002-04-22 22:12:10 by chorus
Good idea, but all strings are not of our creation.
Posted on 2002-04-22 22:27:02 by bitRAKE
All of mine are :)

Posted on 2002-04-22 22:29:17 by chorus
Ha, ha...

1) instead of appending a 0 byte to the string append a 2 dwords zeros
2) test every 8th byte for 0::)
Posted on 2002-04-23 11:21:39 by buliaNaza
Append cacheline + 1 bytes of zeroes and then test every cacheline + 1 bytes. That way every cacheline**2 bytes we get to skip a cacheline. :tongue:
Posted on 2002-04-23 11:28:33 by bitRAKE
It sounds funny, but it works... If you felt like adding 100 zeros that's fine, too :) I don't know what kind of impact it'll have though. I just thought it was a fun idea. However, as bitRAKE pointed out, it might not be so practical as you don't always have control over how many zeros there are. But in my programs, this is probably what I'll do from now on, tack a word or dword instead of a byte to my strings. It's a simple, fast, and small solution.

Posted on 2002-04-23 11:31:44 by chorus
When creating a string, there's usually a way to get its length without any strlen proc afterwards. And with .data segment strings, just do sizeof of course... fastest way of all :)
Posted on 2002-04-23 11:33:46 by Qweerdy
for strings you have total control of... prefix a length dword. "The
fastest strlen is to not do strlen at all".
Posted on 2002-04-23 13:06:11 by f0dder