Fastest Text search method in a large text file

前端 未结 4 1429
南旧
南旧 2021-01-19 04:35

I am doing a text search in a rather big txt file (100k lines, 7mo) Text is not that big but I need a lot of searches. I want to look for a target string and return the line

4条回答
  •  北荒
    北荒 (楼主)
    2021-01-19 05:06

    If you are searching the same text file over and over, consider indexing the file. For example, create a dictionary that maps each word to which lines it's on. This will take a while to create, but will then make searches O(1).

    If you are searching different text files, or can't index the file for some reason, you probably won't get any faster than the KMP algorithm.

    EDIT: The index I described will only work for single word searches, not multi-word searches. If you want to search for multiple words (any string) then you probably won't be able to index it.

提交回复
热议问题