优化 Java 代码
我该如何优化这段代码?我做了IPFilter,我需要优化它。
package com.ipfilter;
import java.util.HashMap;
import java.util.Map;
/**
* IPFilter
*
* Loads given IP addresses to memory, so you can easily check if ip addres has been blocked
*/
public class IPFilter {
private Map<Integer, IPFilter> filter = new HashMap<Integer, IPFilter>();
/**
* Convert String ip address to Integer array and then calls add ip method
* @param ip
* @return
*/
public void addIP(String ip)
{
int[] numbers = convert(ip);
addIP(numbers, 0);
}
/**
* Convert String ip address to Integer array
* @param ip
* @return
*/
private int[] convert(String ip) {
String[] strings = ip.split("\\.");
int[] numbers = new int[strings.length];
for(int i = 0; i < strings.length; i++)
{
numbers[i] = Integer.parseInt(strings[i]);
}
return numbers;
}
/**
* Add ip address to memory
* @param ip
* @param level
*/
private void addIP(int[] ip, int level) {
if(level < ip.length)
{
if (filter.containsKey(ip[level])) {
filter.get(ip[level]).addIP(ip, level + 1);
} else {
filter.put(ip[level], new IPFilter());
filter.get(ip[level]).addIP(ip, level + 1);
}
}
}
/**
* Checks if ip address is in filter
* @param ip
* @return
*/
public boolean isBlocked(String ip)
{
return isBlocked(filter, convert(ip), 0);
}
/**
* Check if ip address is blocked
* @param list
* @param ip
* @param level
* @return
*/
private boolean isBlocked(Map<Integer, IPFilter> list, int[] ip, int level)
{
if(list.containsKey(ip[level]))
{
if(level < ip.length - 1)
{
return isBlocked(list.get(ip[level]).getList(), ip, level + 1);
}
else
{
return true;
}
}
else
{
return false;
}
}
/**
* Getter for list
* @return
*/
protected Map<Integer, IPFilter> getList() {
return filter;
}
}
How can I optimize this code ? I made IPFilter and I need to optimize it.
package com.ipfilter;
import java.util.HashMap;
import java.util.Map;
/**
* IPFilter
*
* Loads given IP addresses to memory, so you can easily check if ip addres has been blocked
*/
public class IPFilter {
private Map<Integer, IPFilter> filter = new HashMap<Integer, IPFilter>();
/**
* Convert String ip address to Integer array and then calls add ip method
* @param ip
* @return
*/
public void addIP(String ip)
{
int[] numbers = convert(ip);
addIP(numbers, 0);
}
/**
* Convert String ip address to Integer array
* @param ip
* @return
*/
private int[] convert(String ip) {
String[] strings = ip.split("\\.");
int[] numbers = new int[strings.length];
for(int i = 0; i < strings.length; i++)
{
numbers[i] = Integer.parseInt(strings[i]);
}
return numbers;
}
/**
* Add ip address to memory
* @param ip
* @param level
*/
private void addIP(int[] ip, int level) {
if(level < ip.length)
{
if (filter.containsKey(ip[level])) {
filter.get(ip[level]).addIP(ip, level + 1);
} else {
filter.put(ip[level], new IPFilter());
filter.get(ip[level]).addIP(ip, level + 1);
}
}
}
/**
* Checks if ip address is in filter
* @param ip
* @return
*/
public boolean isBlocked(String ip)
{
return isBlocked(filter, convert(ip), 0);
}
/**
* Check if ip address is blocked
* @param list
* @param ip
* @param level
* @return
*/
private boolean isBlocked(Map<Integer, IPFilter> list, int[] ip, int level)
{
if(list.containsKey(ip[level]))
{
if(level < ip.length - 1)
{
return isBlocked(list.get(ip[level]).getList(), ip, level + 1);
}
else
{
return true;
}
}
else
{
return false;
}
}
/**
* Getter for list
* @return
*/
protected Map<Integer, IPFilter> getList() {
return filter;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过一些典型的用例对其进行分析,并使用该数据找出性能瓶颈所在。 然后,优化该代码。
如果不真正知道性能问题出在哪里,您可能会花费大量时间和精力来节省微秒。
Profile it through some typical use cases and use that data to find out where the performance bottlenecks are. THEN, optimize that code.
Without actually knowing where the performance issue is, you could spend a lot of time and effort saving microseconds.
我不确定你到底想优化什么。但是,您有几个
containsKey
后跟get
。一种可能的优化是使用get
并与null
进行比较。例如,而不是:执行以下操作:
但我认为我可以给您的最佳提示是:使用分析器。如果您使用的是 Eclipse,请检查 TPTP。
I'm not sure about what exactly are you trying to optimize. However, you have a couple of
containsKey
followed byget
. A possible optimization is to useget
and compare tonull
. For example, instead of:Do the following:
But I think the best tip I can give you is: use a profiler. If you are using Eclipse, check TPTP.
并非没有原因,但 IP 地址通常是一个 4 字节整数。为什么不对此进行编码,然后让:
作为您的比较。
对于 IPv6,您可以使用 long。
话又说回来,我可能会做的是使用 java.net.Inet4Address 并将它们存储在一个 Set 中。
既然您已经在使用地图,为什么不尝试一种简单的方法呢? Inet4Address.equals() 的半智能实现将进行整数比较,而不是字符串比较。
当然,如果你想做通配符,这个方法就会失败......:-/
Not for nothing, but an IP address is a 4 byte integer as it's usually implemented. Why not encode to that, and just let:
be your comparison.
And for IPv6, you could use use a long.
Then again, what I'd probably do is use java.net.Inet4Address and store them in a Set.
Since you're already using a Map, why not try out a simplistic approach? A halfway smart implementation of Inet4Address.equals() would do an integer comparison, and not a string comparison.
Of course this method breaks down if you want to do wildcarding... :-/