How does memchr() work under the hood?

后端 未结 5 1417
迷失自我
迷失自我 2020-12-31 07:59

Background: I\'m trying to create a pure D language implementation of functionality that\'s roughly equivalent to C\'s memchr but uses arrays and indices i

5条回答
  •  暖寄归人
    2020-12-31 08:31

    GNU libc definitely uses the assembly version of memchr() (on any common linux distro). This is why it is so unbelievable fast.

    For example, if we count lines in 11Gb file (like "wc -l" does) it takes around 2.5 seconds with assembly version of memchr() from GNU libc. But if we replace memchr() assembly call with for example memchr() C implementation from FreeBSD - the speed will decrease to like 30 seconds.

    This is equal to replacing memchr() with just a while loop which compares one char after another.

提交回复
热议问题