简体   繁体   English

Java的首次执行性能

[英]First time execution performance of Java

I've been developing a Reporting Engine (RE is to generate PDF-reports) in C++ on Linux. 我一直在Linux上用C ++开发一个报告引擎(RE是为了生成PDF报告)。 If a PDF-report being generated must contain some charts, I need to build them while building the report. 如果生成的PDF报告必须包含一些图表,则在构建报告时需要构建它们。 ChartBuilder is written in Java (with JFreeChart of Java-TeeChart - it does not matter anyway). ChartBuilder用Java编写(带有Java-TeeChart的JFreeChart-没关系)。 Well, while RE is building a report, it invokes some ChartBuilder-API functions via JNI to build a chart (or several charts) step by step (ChartBuilder is packed into .jar-file). 好的,RE在构建报表时,会通过JNI调用一些ChartBuilder-API函数来逐步构建一个图表(或多个图表)(ChartBuilder被打包到.jar文件中)。 The problem is that it takes a lot of time to build the first chart (that is, to execute every ChartBuilder-API function for the first time during the process lifetime)! 问题在于,构建第一个图表需要花费大量时间(也就是说,在流程生命周期中第一次执行每个ChartBuilder-API函数)! More specifically, it takes about 1.5 seconds to build the first chart. 更具体地说,构建第一张图表大约需要1.5秒。 If there are several charts to be created, the rest of charts are built during about (~0.05, ~0.1) seconds. 如果要创建多个图表,则其余图表将在大约(〜0.05,〜0.1)秒内生成。 That is 30 times faster than the first one! 那比第一个快30倍! It's worth to note, that this first chart is the same with the rest of them (except for data). 值得注意的是,第一个图表与其余图表相同(数据除外)。 The problem seems to be fundamental for Java (and I'm not very expirienced in this platform). 这个问题似乎是Java的根本问题(我对这个平台不是很熟悉)。 Below is the picture that illustrates described problem: 下面是说明所描述问题的图片:

在此处输入图片说明

I wonder if there is a way to hasten the first execution. 我想知道是否有一种方法可以加快第一次执行的速度。 It would be great to understand how to avoid the overhead on the first execution at all because now it hampers the whole performance of RE. 非常了解如何避免第一次执行时的开销,因为现在它会妨碍RE的整体性能。

In addition I'd like to describe the way it works: Somebody invokes C++RE::CreateReport with all needed parameters. 另外,我想描述它的工作方式:有人用所有需要的参数调用C ++ RE :: CreateReport。 This function, if it's needed, creates a JVM and makes requests to it via JNI. 如果需要,此函数将创建一个JVM并通过JNI对其进行请求。 When a report is created, the JVM is destroyed. 创建报告后,JVM被销毁。

Thanks in advance! 提前致谢!

Just-in-time compilation . 即时编译 Keep your JVM alive as a service to avoid paying JIT compilation cost multiple times. 使您的JVM作为服务保持活动状态,以避免多次支付JIT编译成本。

I this it is likely a combination of things as people have pointed out in the comments and other answer - JVM startup, class loader, the fact that Java 'interprets' your code when it is running it etc. 我认为这很可能是人们在评论和其他答案中指出的事情的结合-JVM启动,类加载器,Java在运行代码时“解释”您的代码等事实。

Most fall into the category of 'first time start up' overhead - hence the higher performance in subsequent runs. 大多数属于“首次启动”开销类别,因此在后续运行中具有更高的性能。

I would personally be inclined to agree with Thomas (in the comments to your question) that the highest overhead is possibly the class loader. 我个人倾向于同意托马斯(在对您的问题的评论中),最高的开销可能是类加载器。

There are tools you can use to profile the Java JVM to get a feel for what is taking the most time within the JVM itself - such as: 您可以使用一些工具来概要分析Java JVM,以了解JVM自身花费的时间最多的时间,例如:

You have to be careful using these tools to interpret the results with some thought - you may want to measure first runs and subsequent runs separately, and you also may want to add your own system timings into your C++ code that wraps the JNI calls to get a better picture of the end to end timings. 您必须谨慎使用这些工具来思考结果,您可能需要三思而行-您可能希望分别测量首次运行和后续运行,并且还可能希望将自己的系统计时添加到包装JNI调用的C ++代码中,以获得更好的端到端时序图。 With performance monitoring, multiple test runs are very important to allow for slow and fast individual runs for one reason or another (eg other load on the computer - even on a non shared laptop). 借助性能监控,多次测试运行非常重要,因为一个或另一个原因(例如,计算机上的其他负载-甚至是非共享笔记本电脑上的负载),允许进行缓慢和快速的单独运行。

As LeffeBrune mentions if you can have the chart builder running as a service already, it will likely speed up the first run, although you will probably need to experiment to see how much difference it makes if it has not actually been running on a processor for a while, for example. 正如LeffeBrune提到的,如果您可以让图表构建器已经作为服务运行,它可能会加快第一次运行的速度,尽管您可能需要试验一下,看看如果它实际上未在处理器上运行会产生多大的不同。例如一段时间。

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

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