• Post Reply Bookmark Topic Watch Topic
  • New Topic

java regx search or string char search  RSS feed

 
Raj Ohadi
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if I simply want to search a character from a long string, theoretically, will the java regex "find" or the "inDexOf" String search do thing faster ??
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Theoretically, both will be even fast.
Practically, you'll implement both methods, and run them a few million times, and see, which is faster.
 
Raj Ohadi
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stefan Wagner:
Theoretically, both will be even fast.
Practically, you'll implement both methods, and run them a few million times, and see, which is faster.


"Theoretically, both will be even fast" doesn't answer my question. My question is if there is fundamental low level implementation difference that may make little difference in the speed.

I don't care what's the practical result. Even i find practically method 1 is faster than 2 by 5%, so what ? I want to know why... And that's why I am interested in knowing if their low level implementation may contribute to it, and that's why I posted it in "Advanced" forum. testing this practically is a junior topic.
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ultimately, both approaches end up looking at every character in the string until the desired character is found. That's as low-level as it gets. If you use a regex, you have the additional overhead of compiling the regex into a Pattern object and creating a Matcher object. Once that's done, the regex approach should be just about as fast as indexOf().
 
Steve Fahlbusch
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raj,

The way to find out is to compare the bytecode - that way you will know.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!