我只是写了一篇关于如何达到这一点的描述,但是认为发布代码并保留它很容易:)

据我所知,test3()的性能应与test1()相同-唯一的区别是捕获异常的位置(在test1()的调用方法内,在test3()的被调用方法内)

为什么test3()通常需要在test1()和test2()之间的某个时间才能完成?

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class Test {

    public static void main(String[] args) {
        warmup();
        test1(2500000); // Exception caught inside the loop
        test2(2500000); // Exception caught outside the loop
        test3(2500000); // Exception caught "inside" the loop, but in the URLEncoder.encode() method
    }

    private static void warmup() {
        // Let URLEncoder do whatever startup it needs before we hit it
        String encoding = System.getProperty("file.encoding");
        try {
            URLEncoder.encode("ignore", encoding);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static void test1(int count) {
        String encoding = System.getProperty("file.encoding");
        long start = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            try {
                URLEncoder.encode("test 1 " + i, encoding);
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("Performed " + count + " encodings trying to catch each in " + (end - start) + "ms");
    }

    private static void test2(int count) {
        String encoding = System.getProperty("file.encoding");
        long start = System.currentTimeMillis();
        try {
            for (int i = 0; i < count; i++) {
                URLEncoder.encode("test 2" + i, encoding);
            }
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("Performed " + count + " encodings trying to catch all in " + (end - start) + "ms");
    }

    private static void test3(int count) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            URLEncoder.encode("test 3 " + i);
        }
        long end = System.currentTimeMillis();
        System.out.println("Performed " + count + " encodings with a deprecated method in " + (end - start) + "ms");
    }

}


运行它会给我(在Windows XP上为JDK 1.6.0_13)输出:

Performed 2500000 encodings trying to catch each in 4906ms
Performed 2500000 encodings trying to catch all in 2454ms
Performed 2500000 encodings with a deprecated method in 2953ms


因此,响应非常接近(我们正在谈论的东西太微不足道了,这是不相关的),但是我很好奇!

后来...

人们建议JVM优化妨碍了人们的发展-我同意。因此,我将每个测试细分为它自己的类/ main方法,并分别进行了测试。结果如下:

1 - Performed 2500000 encodings trying to catch each in 5016ms
1 - Performed 5000000 encodings trying to catch each in 7547ms
1 - Performed 5000000 encodings trying to catch each in 7515ms
1 - Performed 5000000 encodings trying to catch each in 7531ms

2 - Performed 2500000 encodings trying to catch all in 4719ms
2 - Performed 5000000 encodings trying to catch all in 7250ms
2 - Performed 5000000 encodings trying to catch all in 7203ms
2 - Performed 5000000 encodings trying to catch all in 7250ms

3 - Performed 2500000 encodings with a deprecated method in 5297ms
3 - Performed 5000000 encodings with a deprecated method in 8015ms
3 - Performed 5000000 encodings with a deprecated method in 8063ms
3 - Performed 5000000 encodings with a deprecated method in 8219ms


有趣的观察:


在自己的JVM中,减少了捕获每个调用与捕获循环外的所有内容之间的差距(我认为由于一次执行的所有其他迭代,优化不会在“一次测试全部完成”的情况下就花了很多精力)
现在,我这一边的try / catch与URLEncoder.encode()内部的try / catch之间的差距要小得多(在5000000次迭代中间隔了半秒),但是仍然始终在那里。

最佳答案

按照您发布的顺序运行它:


执行了2500000种编码,试图
在34208ms内捕捉到每个
执行了2500000种编码,试图捕获所有
在31708毫秒内执行2500000编码
在30738ms中弃用的方法


冲销订单:


执行2500000个编码
32598ms中已弃用的方法已执行
2500000种编码试图捕捉所有
在31239毫秒内执行2500000编码
试图在31208ms内捕捉到每个


因此,我实际上并不认为您在看自己在看的东西(当然,test1的速度不会比test3慢66%,这是基准测试所建议的)

关于java - Java异常循环和弃用(或者是URLEncoding吗?),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/905126/

10-12 06:04