优化 Java 代码

发布于 2024-08-04 10:58:10 字数 2910 浏览 2 评论 0原文

我该如何优化这段代码?我做了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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

飘然心甜 2024-08-11 10:58:10

通过一些典型的用例对其进行分析,并使用该数据找出性能瓶颈所在。 然后,优化该代码。

如果不真正知道性能问题出在哪里,您可能会花费大量时间和精力来节省微秒。

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.

童话 2024-08-11 10:58:10

我不确定你到底想优化什么。但是,您有几个 containsKey 后跟 get。一种可能的优化是使用 get 并与 null 进行比较。例如,而不是:

 if (filter.containsKey(ip[level])) {
      filter.get(ip[level])
 }

执行以下操作:

 IPFilter value = filter.get(ip[level]);
 if (value != null) {
      value.addIp(...);
 }

但我认为我可以给您的最佳提示是:使用分析器。如果您使用的是 Eclipse,请检查 TPTP

I'm not sure about what exactly are you trying to optimize. However, you have a couple of containsKey followed by get. A possible optimization is to use get and compare to null. For example, instead of:

 if (filter.containsKey(ip[level])) {
      filter.get(ip[level])
 }

Do the following:

 IPFilter value = filter.get(ip[level]);
 if (value != null) {
      value.addIp(...);
 }

But I think the best tip I can give you is: use a profiler. If you are using Eclipse, check TPTP.

长途伴 2024-08-11 10:58:10

并非没有原因,但 IP 地址通常是一个 4 字节整数。为什么不对此进行编码,然后让:

int ipSrc = convertIpToInt(String ip); 
if ( ipSrc == ipDest ) { 
  /// 
} 

作为您的比较。

对于 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:

int ipSrc = convertIpToInt(String ip); 
if ( ipSrc == ipDest ) { 
  /// 
} 

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... :-/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文