测量创建和销毁简单对象的时间 [英] Measuring the time to create and destroy a simple object

查看:32
本文介绍了测量创建和销毁简单对象的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 Effective Java 2nd Edition Item 7:避免终结器

From Effective Java 2nd Edition Item 7: Avoid Finalizers

哦,还有一件事:使用终结器会导致严重的性能损失.在我的机器上,创建和销毁一个简单对象的时间大约为 5.6 ns.添加终结器将时间增加到 2,400 ns.在换句话说,使用终结器创建和销毁对象大约要慢 430 倍."

"Oh, and one more thing: there is a severe performance penalty for using finalizers. On my machine, the time to create and destroy a simple object is about 5.6 ns. Adding a finalizer increases the time to 2,400 ns. In other words, it is about 430 times slower to create and destroy objects with finalizers."

如何测量创建和销毁对象的时间?你只是做:

How can one measure the time to create and destroy an object? Do you just do:

long start = System.nanoTime();
SimpleObject simpleObj = new SimpleObject();
simpleObj.finalize();
long end = System.nanoTime();
long time = end - start;

推荐答案

那只是测量执行 finalize 方法的时间.最终确定的大部分成本都将用于 GC 必须执行的特殊处理.

That only measures the time to execute the finalize method. The vast majority of the cost of finalization will be in the special handling that the GC has to perform.

这篇关于测量创建和销毁简单对象的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆