How to filter strings in Java performance Optimization
What this article shares with you is about how to filter strings in Java performance optimization. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
A simple requirement for ★
First describe the requirements:
Given a String object, filter out characters other than numbers (characters'0' to'9'). The time cost is required to be as small as possible. The prototype of the filter function is as follows:
String filter (String str)
In response to the above requirements, I wrote 5 different filtering functions. For ease of description, the function names are defined as filter1 to filter5. Among them, filter1 has the worst performance and filter5 has the best performance. Before looking at the following content, you first think to yourself, if you were to implement the function, what would it look like? You'd better write down the function you want so that it can be compared with the example I gave.
★ Code-- 5 ways to implement ◇ Test Code step by step
To make it easier to test performance, prepare a pile of test code as follows:
Class Test {public static void main (String [] args) {if (args.length! = 1) {return;} String str = ""; long nBegin = System.currentTimeMillis (); for (int iTuno; I)