3 ms·
Just a quick callout that pinning the search to start of line does nothing. It is just another character, after all. Now, getting it to report line numbers wi
by taeric 8y ago
Just a quick callout that pinning the search to start of line does nothing. It is just another character, after all.
Now, getting it to report line numbers will kill it, since you then have to scan every character.
- tyingq 8y agoWhy wouldn't pinning to start of line help for some patterns/data? You do still have to find the next newline, but you don't have to make other, additional comparisons once ^x is false, for that line. Or, roughly, grepping for '^x' instead of 'x' should result in less work if the line does not contain x in the first character. One fewer comparison for each character after the first.
- markrages 8y agonewline is just another character, so grepping '^x' should be no different than grepping 'yx'.
- dllthomas 8y agoYou seem to be thinking about handling a line at a time. In that context, checking for an x at the beginning would be much faster than looking for an x anywhere in the line. However, for anything fast you're going to be handling a large buffer at a time, if not the entire memory-mapped file. In that context, there's no difference between ^x and yx, except that ^x also needs to match at start-of-file (so if anything it should be marginally slower). Finding just x is probably faster than finding yx - longer patterns can be faster when you can skip looking at some fraction of the input (as per TFA) but I expect 2 characters isn't enough to allow that.
- taeric 8y agoJust to elaborate a little on my quick callout. Doing line oriented searches actually requires you to look at every character, if only to find all the line breaks. Instead, treat the file as a collection of characters and just look for patterns. Which may include the line break character. That make sense? Edit: I should also add that you should look for burntsushi's posts. Turns out some of this had become out of date. And largely depends on size of what you are searching.