小编x4-*_*x4-的帖子

Java效率

我正在使用一些代码来计算计算某些Java代码所需的时间,以了解Java某些功能的效率或低效率.这样做我现在陷入了一些非常奇怪的效果,我无法解释自己.也许你们中的某个人可以帮助我理解它.

public class PerformanceCheck {

 public static void main(String[] args) {
    List<PerformanceCheck> removeList = new LinkedList<PerformanceCheck>();

    int maxTimes = 1000000000;

    for (int i=0;i<10;i++) {
        long time = System.currentTimeMillis();

        for (int times=0;times<maxTimes;times++) {
            // PERFORMANCE CHECK BLOCK START

            if (removeList.size() > 0) {
                testFunc(3);
            }

            // PERFORMANCE CHECK BLOCK END
        }

        long timeNow = System.currentTimeMillis();
        System.out.println("time: " + (timeNow - time));
    }
 }

 private static boolean testFunc(int test) {
    return 5 > test;
 }

}
Run Code Online (Sandbox Code Playgroud)

启动这会导致相对较长的计算时间(请记住removeList为空,因此甚至不调用testFunc):

time: 2328
time: 2223
... …
Run Code Online (Sandbox Code Playgroud)

java performance

12
推荐指数
1
解决办法
1017
查看次数

标签 统计

java ×1

performance ×1