performance - Java ReplaceAll vs For loop with Replace in it -
i have string holds data , need remove of special characters , tokenise data.
which of following 2 methods should preferred better performance:
string data = "random data (for performance) waiting reply?" data=data.replaceall("?", ""); data=data.replaceall(".", ""); data=data.replaceall(",", ""); data=data.replaceall("(", ""); data=data.replaceall(")", ""); string[] tokens = data.split("\\s+"); for(int j = 0; j < tokens.length; j++){ //logic on tokens }
or
string data = "random data (for performance) waiting reply?" string[] tokens = data.split("\\s+"); for(int j = 0; j < tokens.length; j++){ tokens[j]=tokens[j].replace("?", ""); tokens[j]=tokens[j].replace(".", ""); tokens[j]=tokens[j].replace(",", ""); tokens[j]=tokens[j].replace("(", ""); tokens[j]=tokens[j].replace(")", ""); //logic on each token }
or there other approach can increase performance.
statistics on same appreciated
for loop provided above used performing other logics on each token.
meant replace function imposed on whole content faster or replace on each token in loop(for loop anyhow used other operations) faster? i.e. replace once , perform other operations or replace step step each token , perform required operation.
thanks in advance
just replace
enough without loops.
replaceall
uses regexp engine under hood has more performance overhead.
there seems common misunderstanding of "all" suffix.
see difference between string replace() , replaceall().
update
found similar question one:
Comments
Post a Comment