strstr faster than algorithms?

后端 未结 4 689
一向
一向 2020-12-24 03:42

I have a file that\'s 21056 bytes.

I\'ve written a program in C that reads the entire file into a buffer, and then uses multiple search algorithms to search the file

4条回答
  •  失恋的感觉
    2020-12-24 03:49

    Without seeing your code, it's hard to say exactly. strstr is heavily optimized, and usually written in assembly language. It does things like reading data 4 bytes at a time and comparing them (bit-twiddling if necessary if the alignment isn't right) to minimize memory latency. It can also take advantage of things like SSE to load 16 bytes at a time. If your code is only loading one byte at a time, it's probably getting killed by memory latency.

    Use your debugger and step through the disassembly of strstr -- you'll probably find some interesting things in there.

提交回复
热议问题