• 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

Questions on Speed for String Parsing/Comparisons

 
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many times I find I have a need to parse a string, looking for another string value inside, or compare strings. I know that String actions eat alot of time/resources, so I'm looking for a way to speed this up. One idea I had was to convert all my Strings to byte[] objects, but I have no idea if this is faster, nor if it's the quickest way to do this. Does anyone know the best way?
For example, let's say I have a String that comes in from an email that is this:
"Hey Robert,\r\nWhat's been going on?\r\n"
I want to find all the cases of "\r\n" or, if the program that sent the message used "\n" instead of "\r\n", then i want to find all those. What's the fastest way to do this?
Thanks in advance!
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you can expect the String methods to already be maximally optimized, so there really is no reason to do something like a conversion to byte[] or the like (String *is* working on char[] internally).
In some cases, intelligent use of regular expressions might be more effective than indexOf() and the like, but it's hard to say in general.
Anyway, I would suggest using a profiler first to get more info on where your performance bottlenecks are - you might be surprised...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic