How to write a better strlen function?

后端 未结 7 1917
Happy的楠姐
Happy的楠姐 2020-12-30 06:43

I am reading \"Write Great Code Volume 2\" and it shows the following strlen impelementation:

int myStrlen( char *s )
{
    char *start;
    start = s;
    w         


        
7条回答
  •  轮回少年
    2020-12-30 07:27

    From Optimising strlen(), a blogpost by Colm MacCarthaigh:

    Unfortunately in C, we’re doomed to an O(n) implementation, best case, but we’re still not done … we can do something about the very size of n.

    It gives good example in what direction you can work to speed it up. And another quote from it

    Sometimes going really really fast just makes you really really insane.

提交回复
热议问题