• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

indexOf or Pattern Matcher faster/better?

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am just curious as to which of these would be faster, and/or recommended?



or



There will be a LOT of this going on in the program, on many different patterns.

Thanks
 
author & internet detective
Posts: 41915
910
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brad,
What happened when you tried it?

In general it is good to go with what is clearer (which is the first one) unless it proves to be a performance problem.
 
Brad Walton
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeanne, thanks for the reply. Both work fine to accomplish the task. I haven't timed these examples, so I am not sure which is faster. But timing this alone wouldn't answer my question anyway. My question was more general, because there will be so many different pattern matches that will be going on in rapid succession, continually, in the program. I was inquiring about whether one method is faster to process than the other, in general.

As far as code clarity, they both seem about the same to me. Maybe the pattern seems a little clearer, but that's because I'm more of a regex person (from Perl programming). But I don't really see that as being a factor in my decision in this case.
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This topic came up before -- as I remeber writing a test to compare indexof and regex....

If memory serves, I remember... If the search string is large, like 100000 characters, then indexof wins. And if the search string is small, like 100 characters, then regex wins. (compilation of the regex was not part of the test -- regex was precompiled)

Henry
 
Brad Walton
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry. That's what I was looking for.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic