java - Performance issues in Regular Expression -


i having rest service running under heavy load, meaning getting lot of traffic around million read calls per day. rest servcie lookup database basis on userid , retrieve few bunch of columns corresponding userid.

so seeing high performance issues in code currently. suspecting below method 1 of methods should start optimizing first of all.

below method accept attributename , basis on give me match using regular expression.

let's take example- if attrname technology.profile.financial

then below method return me technology.profile. , way work other case well.

private string getattrdomain(string attrname){     pattern r = pattern.compile(commonconstants.valid_domain);     matcher m = r.matcher(attrname.tolowercase());     if (m.find()) {       return m.group(0);     }     return null; } 

in commonconstants class file

string  valid_domain = "(technology|computer|sdc|adj|wdc|pp|stub).(profile|preference|experience|behavioral)"; 

i trying see, whether there might performance issues here or not using regex above? if yes, what's best way rewrite thing again keeping in mind performance issues?

thanks help.

i used caliper test , , results are: if u compile pattern before every method call going fastest way.

you regex method fastest wayto it, but change need make compute pattern upfront, not every time:

 private static pattern p = pattern.compile(valid_domain); 

then in method:

 matcher matcher = pattern.matcher(input); ... 

for ones interested settings used caliper: --warmupmillis 10000 --runmillis 100

 package stackoverflow;   import java.util.regex.matcher;  import java.util.regex.pattern;   import com.google.caliper.param;  import com.google.caliper.runner;  import com.google.caliper.simplebenchmark;  import com.google.common.base.splitter;  import com.google.common.collect.iterables;   public class regexperformance extends simplebenchmark {       private static final string firstpart    = "technology|computer|sdc|adj|wdc|pp|stub";       private static final string secondpart   = "profile|preference|experience|behavioral";       private static final string valid_domain = "(technology|computer|sdc|adj|wdc|pp|stub)\\.(profile|preference|experience|behavioral)";        @param({"technology.profile.financial", "computer.preference.test","sdc.experience.test"})       private string input;        public static void main(string[] args) {            runner.main(regexperformance.class, args);       }        public void timeregexmatch(int reps){           for(int i=0;i<reps;++i){               regexmatch(input);           }       }         public void timeguavamatch(int reps){           for(int i=0;i<reps;++i){               guavamatch(input);           }       }        public void timeregexmatchoutsidemethod(int reps){           for(int i=0;i<reps;++i){               regexmatchoutsidemethod(input);           }       }       public string regexmatch(string input){         pattern p = pattern.compile(valid_domain);         matcher m = p.matcher(input);         if(m.find()) return m.group();         return null;     }      public string regexmatchoutsidemethod(string input){           matcher matcher = pattern.matcher(input);           if(matcher.find()) return matcher.group();           return null;     }      public string guavamatch(string input){         iterable<string> tokens = splitter.on(".").omitemptystrings().split(input);         string firsttoken  = iterables.get(tokens, 0);         string secondtoken = iterables.get(tokens, 1);         if( (firstpart.contains(firsttoken) ) && (secondpart.contains(secondtoken)) ){             return firsttoken+"."+secondtoken;         }         return null;     } } 

and results of test:

             regexmatch technology.profile.financial 2980 ========================              regexmatch     computer.preference.test 2861 =======================             regexmatch           sdc.experience.test 3683 ============================== regexmatchoutsidemethod technology.profile.financial  179 = regexmatchoutsidemethod     computer.preference.test  227 = regexmatchoutsidemethod           sdc.experience.test  987 ========              guavamatch technology.profile.financial  406 ===              guavamatch     computer.preference.test  421 ===             guavamatch           sdc.experience.test  382 === 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -