Compile time and runtime processing of a code in java - java

I have a piece of Java code and I compiled and ran it. I got an output and I made some changes to it, before compiling and running again.
Is there any difference between the time it takes during the first compilation compared to the second compilation. Similarly are there changes between the first runtime to second runtime? Is there any way to find that difference in processing time?

There can be certain difference according to the changes you have made.
It depends on what your program did and what it does now, i think you can understand that.
To check the time, you can do this by creating a thread that can act like a timer just after the program execution, and stop that thread after all your processes have been done and simply display to see the time.

Firstly, I'm unsure why this is important to you. Perhaps by providing some more context you will get a more detailed answer.
Comparing compilation time can be achieved using Operating System tools. For example, on Linux try using time.
Complete execution time of your two Java programs can be achieved in the same fashion. However, if you are looking more closely at whether your code changes have improved your execution performance, I would suggest you Google "benchmarking in Java" to find a wealth of information on the correct methods to benchmark code.

If you use Eclipse you can configure Project -> Build Automatically to rebuild the project after every change.
So once you want to run it would take minimal time.

Related

First time execution performance of Java

I've been developing a Reporting Engine (RE is to generate PDF-reports) in C++ on Linux. If a PDF-report being generated must contain some charts, I need to build them while building the report. ChartBuilder is written in Java (with JFreeChart of Java-TeeChart - it does not matter anyway). 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). 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)! More specifically, it takes about 1.5 seconds to build the first chart. If there are several charts to be created, the rest of charts are built during about (~0.05, ~0.1) seconds. That is 30 times faster than the first one! 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).
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.
In addition I'd like to describe the way it works: Somebody invokes C++RE::CreateReport with all needed parameters. This function, if it's needed, creates a JVM and makes requests to it via JNI. When a report is created, the JVM is destroyed.
Thanks in advance!
Just-in-time compilation. Keep your JVM alive as a service to avoid paying JIT compilation cost multiple times.
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.
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:
visualvm (http://visualvm.java.net)
JVM monitor (http://jvmmonitor.org)
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. With performance monitoring, multiple test runs are very important to allow for slow and fast individual runs for one reason or another (e.g. 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.

Monitor distribution of execution time of a java program

I have a very long program with hundreds of methods. In my attempt to enhance the performance of the program, I want to monitor the time taken in each method or for loop to see where should I start to optimize the code.
is there any eclipse plugin or software tool to do that? I mean to calculate time taken in each method without adding tons of lines of code?
Visualvm has an excellent profiler.
I usually run my code in a hot loop and attach after the code warms up, but setting a breakpoint early in your code and then attaching works well also. Think you'll really like visualvm, I use it to track down the performance bottlenecks in code at least once a week.
jvisualvm it is in jdk and jre AFAIR

Fastest way to run Java jar file from Python?

Here's my issue. I have an existing .jar file that I must use in my program. The program, however, is written in Python.
Since my program is taking a long time to run (a named entity tagger on a large development corpus) I profiled it using cProfiler and lined profiled it using line_profiler. It seems that 92% of the time is spent on this task.
I am currently using the following code:
import subprocess as sub
sub.call(["java", "-jar", "-Xmx512m", "MyFile.jar",
featuresFileName, numIterations, featureCutOff])
I read somewhere about subprocess vs Popen and other bits and pieces, but couldn't find a good solution that does not require subprocess or os calls (of course, there may not be any).
I'd really appreciate some advice on the fastest way to run a .jar file from within a Python script. Note, however, that I cannot modify the Java code nor do I have access to speak to the developer of that code.
Alternatively, and I don't know if this will help or if I'm simply grasping at straws here, but perhaps there is a way to keep the process called in sub.call() above in the background, somehow keeping the JVM running (?) so that I can simply invoke the jar file. Maybe that can help reduce startup costs? BTW I am a total Java newbie (mostly C++,C#,Python experience) so my question could make no sense whatsoever - I apologize in advance...
You could try porting your Python to Jython, and then run it all natively in the same JVM (that may or may not work). That way you have effectively zero start up time, and the JVM has enough time to leverage its JIT over time to ideally give you better performance overall.
That indicates that most of the time is spent in this process. It may not be the startup time which is the problem. It may be what it does once it has started.
The only way around this I can think of is to run the process in the background, multiple time concurrently if that is an option. (concurrently rather than running one after another)
Try with "-client" option. It should reduce JVM startup time.
By analysing the manifest file of the jar file you can find out the class name of the jar file which is used. So then you could in principle write your own small java daemon which is listening for new arguments to arrive and calls the main() function of the appropriate class. But it is really worth the effort only if startup costs are the issue.

how to get the last executed method in code that doesn't terminate?

I'm modifying a rather big codebase and I'm in the process of debugging. My code doesn't terminate, so I don't get a stacktrace. I tried to isolate the trouble code with breakpoints, but unfortunately it runs through sections that are executed all the time (transaction manager), so I'm clicking to death. Furtermore, i get the impression that the code breaks only under certain conditions, but runs fine most of the time.
Is there a way in Intellij to see the last method that was / the current method that is executed?
thanks
You can use BTrace, a tracing tool, to print out the name of every method entered in your program. There is a sample script that comes with BTrace, called AllMethods, which does just that.
All of you have to do is start your java process and then run the BTrace script against the JVM's PID. It will print out every method entered. You can restrict it to certain packages if you like. For more information about using BTrace check out this tutorial.
Use a profiler. It will give you a breakdown of which methods are executing, how much time your program spends in each method etc etc. It should be quite easy to find out from there where your program is spending its time.
JProfiler is an example. I think it has a trial / demo version which is fully functional.

Running c++ from Java problem

I need to compile and run a c++ program from java. I am using
Process a = Runtime.getRuntime().exec ("g++ -g function.cpp -o function");
Process b = Runtime.getRuntime().exec ("./function");
the problem is that the output I get from the c++ program is not correct but If I compile and run it myself in the command line it works just fine. The problem is Java and i dont know why.
Thanks a lot
Al,
There is one definite and one probable problem that I see here. The definite problem is that Runtime.exec() does not wait for the process to complete. So you will need to add
a.waitFor();
before calling b.
The possible issue is that depending on how you are invoking this application, the current working directory may not be where you think it is. So function.cpp may not exist.
Are you waiting for process A to finish before running process B?
"The output... is not correct" doesn't help anyone to diagnose your issue. You should definitely give the output you expected, and the output you saw from Java. Assuming that your program is small, you should post the source code of that too (since this is about the compilation process after all).
By the way, what happens when you navigate to the working direction of the Java program, find the function executable it generating and invoke that yourself from the command line? Is the output correct now? The answer to this will let you know whether the problem is in the compilation step or the execution step.
If it's execution, I would hazard a guess at things like the environment (envvars, PATH, etc.) but without more information it's hard to tell.
Also, as with all question that involve Processes, take a look at these common pitfalls. It looks like you're making at least one of them (the common one of not consuming output) which might lead to your program working on trivial C++ code but deadlocking on a larger codebase.
You're also not checking the output (either the return value or the stdout/stderr streams) of the compilation step at all, so you have no idea whether the compilation was successful - and if not, what (useful) error messages you got from the compiler.

Categories