开发者

What is the time taken by java to call a method in another package?

开发者 https://www.devze.com 2023-02-01 13:00 出处:网络
I have an assignment where i need to do feasibility study on two of my approaches and find optimized one.

I have an assignment where i need to do feasibility study on two of my approaches and find optimized one.

There are two packages A and B User input is gathered in A and then sent to B for execution. Now my approaches are 1. Call B methods from package A one by one. 2. Create a common method in B and send all the input as parameters.

Which is feasible and best one, I know 2 is good in terms of code optimization and less number of calls. But, I want to understand what will be the execution time difference in above approaches How much time does java take to call method in another package? Is there any default value like x nano seconds or y milli seconds.

Based on the 开发者_开发问答time taken I can choose the appropiate one.

Thanks


First of all, in terms of performance on the JVM, there is absolutely no difference between calling a method in the same package, and calling a method in another package.

Second of all, the method call overhead is very unlikely to be a performance issue. The overhead per method call is very small, and frequently called methods will often be inlined by the JVM; it may be faster to make separate calls, so as to leave the JVM a better opportunity for optimization.

That said, if there are performance issues in your program, you should profile it and see - questions like this seldom have "one size fits all" answers. Always measure performance on actual data, but don't prematurely optimize each method call.


System.nanoTime()

//method call  

System.nanoTime()

Note: It has nothing to do with performance


I think you are trying to analyze the data flow and execution time. In the first approach, you are getting the user input and calling some other classes in packages B.If your design in such a way that to carry out business validation for the input data then you need to call for every data that the user submitted.If the user input data more, then that much times you need to validate or execute business methods in the package B.In this case, it is directly proportional to input data.

In your second approach, wrap all the business data submitted by the user in a value object (ie in a traditional way) and send that value object to one of the classes in package B and carry out your business functionalities.In this case, you are going to call your classes in package B from Package A only one time. This time measurement you can do it using System.currentTimeMillis().

Hope this will clarify your doubt.


You are basing the method call based on user input.

A users input can take between one billion and 100 billion nano-seconds, a method call takes less than 10 nano-seconds. If your method is small and called often it is likely to be inlined so the method call will disappear.

Whether you call the method once or many times, the overhead is unlikely to be large enough for a user to notice the difference.

0

精彩评论

暂无评论...
验证码 换一张
取 消