简体   繁体   中英

More efficient to have one string, replace and then perform a split or have an array of strings and create a new one changing it?

I am undecided what I should do when it comes to prepare the argument for the ProcessBuilder.command method. The method 1 is definitely faster but I am looking for the best efficiency here.

Method 1:

String test = "ls | grep $name -option";
test = test.replace("$name",textField.getText());
new ProcessBuilder.command(test.split(" "));

Method 2:

String[] test = { "ls", "|", "grep", "$name", "-option" };
String[] newTest = { test[0], test[1], test[2], textField.getText(), test[4] };
new ProcessBuilder.command(newTest);

The second method will be more efficient in overall performance (because of replace method implementation and split method call).

But in this case there will be no significant difference in execution time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM