简体   繁体   English

评估Java方法重构性能

[英]Measuring Java method refactoring performance

I was given a weird task. 给我一个奇怪的任务。 I have to refactor certain methods in a huge code base (the easy part) and provide performance gain reports. 我必须在庞大的代码库(最简单的部分)中重构某些方法,并提供性能提升报告。 I should focus on speed of execution and memory usage. 我应该关注执行速度和内存使用情况。 They want know the performance improvement by method! 他们想知道方法的性能改进!

So I have a method like this: 所以我有一个这样的方法:

public void processHugeFile(File f) {
   long start = java.lang.System.currentTimeMillis();

   // lots of hashmaps, lots of arrays, weird logic,...

   long end = java.lang.System.currentTimeMillis();
   logger.log("performance comparison - exec time: " + (end - start));
}

Then I have to refactor it: 然后我必须重构它:

public void processHugeFile(File f) {
   long start = java.lang.System.currentTimeMillis();

   // just lists, some primitives, simple logic,...

   long end = java.lang.System.currentTimeMillis();
   logger.log("performance comparison - exec time: " + (end - start));
}

In the end I just have to process the logs. 最后,我只需要处理日志。

But what about memory usage ? 但是内存使用情况呢? I have tried getRuntime().totalMemory() - getRuntime().freeMemory() and the getHeapMemoryUsage().getUsed() but they don't seem to work. 我已经尝试过getRuntime().totalMemory() - getRuntime().freeMemory()getHeapMemoryUsage().getUsed()但它们似乎没有用。 Also, JVM Profilers focus on objects not on methods and I am speaking of a fairly large code base. 另外,JVM Profilers专注于对象而不是方法,我说的是相当大的代码库。

Can someone provide me some hints? 有人可以给我一些提示吗?

Thank you very much. 非常感谢你。

Broadly, refactoring is not a means to increase performance, but to improve readability and maintainability of a code base. 广义而言,重构并不是提高性能的一种手段,而是提高代码库的可读性和可维护性的一种手段。 (Which may then help you make optimizations and architectural changes with more confidence and ease.) I assume you're aware of this, and you mean that you're trying to clean up some slow code in the process. (然后,这可能会帮助您更加自信和轻松地进行优化和体系结构更改。)我假设您已经意识到这一点,并且您是在尝试清除过程中的一些慢代码。

This is really a tool for a profiler, not for hand-instrumentation. 这实际上是用于探查器的工具,而不是用于手动乐器的工具。 You will never get precise measurements this way. 这样您将永远无法获得精确的测量结果。 For example, System.currentTimeMillis() calls add their own overhead and for short-lived methods could take longer than the method itself. 例如, System.currentTimeMillis()调用增加了其自身的开销,而寿命短的方法可能比方法本身花费更长的时间。 Runtime can only help you get a crude picture of memory usage. Runtime只能帮助您大致了解内存使用情况。

I don't agree that profilers can't help. 我不同意分析器无法提供帮助。 JProfiler for instance will happily graph heap size over time, include generation sizes. 例如,JProfiler将愉快地绘制随时间变化的堆大小,包括生成大小。 It will break down memory usage by allocation site, object type. 它将按分配站点,对象类型细分内存使用情况。 It will show you performance bottlenecks by inclusive/exclusive time. 它将按包含/排除时间显示性能瓶颈。 And all of this without touching your code. 所有这些都无需触碰您的代码。

You really, really want to use a profiler like JProfiler, not hand-coded stuff. 您真的很想使用像JProfiler这样的分析器,而不是手工编码的东西。

You could use a profiler but also of use would be enabling Garbage Collection logging and then analysing the GC logs after you program has run. 您可以使用探查器,但也可以启用垃圾收集日志记录,然后在程序运行后分析 GC日志。

This will tell you how much memory was being used AND how much time was being spent doing Garbage Collection which is more useful than just knowing how much memory was used - for example if you notice any excessive GC or stop the world collections that affect throughput & latency requirements. 这将告诉您正在使用多少内存以及花费了多少时间来进行垃圾回收,这比仅知道已使用了多少内存更为有用-例如,如果您注意到任何过多的GC或停止了影响吞吐量的世界回收,延迟要求。

Use profiler!!! 使用探查器!!! Results may be distorted if you are doing it this way. 如果这样做,结果可能会失真。

If you are using for example Netbeans, it has built-in profiler . 例如,如果您使用的是Netbeans,则它具有内置的profiler

I'm puzzled by this requirement. 我对此要求感到困惑。 I agree with Sean that refactoring is done for design reasons rather than performance reasons. 我同意肖恩的观点,重构是出于设计而不是性能的原因。 There is no a priori reason to expect any performance increase in general, and if there is enough doubt about the benefits maybe it shouldn't be done at all. 没有先验的理由去期待任何一般的性能提升,如果有足够的疑问的好处也许不应该被执行。

And maybe you should do an algorithmic analysis instead: this is easy enough most of the time, which gives you an a analytic result in Big-O terms: if there isn't a clearly visible advantage in Big-O then there probably isn't much of a performance advantage at all. 也许您应该进行算法分析:大多数时候这很容易,就Big-O而言,这为您提供了分析结果:如果Big-O没有明显的优势,那么可能就没有了。根本没有任何性能优势。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM