Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

java regx search or string char search

 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ??
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Raj,

The way to find out is to compare the bytecode - that way you will know.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic