Java indexOf function more efficient than Rabin-Karp? Search Efficiency of Text

后端 未结 7 522
面向向阳花
面向向阳花 2020-12-20 11:47

I posed a question to Stackoverflow a few weeks ago about a creating an efficient algorithm to search for a pattern in a large chunk of text. Right now I am using the St

7条回答
  •  死守一世寂寞
    2020-12-20 12:25

    But this is only natural to happen!
    Your test input first of all is too trivial.

    indexOf returns the index of was searching a small buffer (String's internal char array`) while the Rabin-Karp has to do preprocessing to setup its data to work which takes extra time.

    To see a difference you would have to test in a really large text to find expressions.

    Also please note that when using more sofisticated string search algorithm they can have "expensive" setup/preprocessing to provide the really fast search.
    In your case you just search a was in a sentence. I any case you should always take the input into account

提交回复
热议问题