Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to filter strings in Java performance Optimization

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report