Java – three questions about doing a lot of computing

This is just a series of questions about doing a lot of calculations Either I can't find the answer online, or I need clarification

>Passing (floating point number, floating point number, floating point number) as a method parameter vs (float []) is faster, where the array has three items? > Is the method returning float [] faster than setting the contents of the float [] passed to the method as a parameter? > Is it faster to replace method calls with actual calculations, i.e. is a = sum (B, c) slower than a = B, C? Suppose sum (x, y) {return x, y}

Edit:

Thanks for all your answers, guys! Before I close this post, if anyone knows, I have a faster question:

>If I'm using a class to repeatedly calculate the same statistics over and over again (and then throw them away), would it be better to create instance variables to act as containers to avoid continuous reassignment and deallocation?

Solution

That depends If you already have a series of floating convenience, it should make no difference If you are constructing an array every time, it will take some time to allocate the array, and it may take some time to construct the array

Does it matter? If you execute it in a tight loop that has been executed millions of times in a row, and execute it many times in the life cycle of the application, it will certainly do so

If you need to construct a float array for the return value each time, it will certainly not be faster than setting the value in a pre-existing array Just because both options involve setting values, one of them has the additional task of creating a new array But creating a new array can be very, very fast

Nevertheless, if you benchmark your application millions of times quickly and continuously, it may save you some time

It's almost impossible to say The built-in hotspot code optimizer is very good at finding these things and optimizing them for you

If you benchmark this, try to make the sum method private, which will make it easier for hotspot to determine that it can be inlined (although it will find it itself if you don't have any overridden implementation of the sum method)

One thing about benchmarking is:

It may help your application to use the current version of VM you are using (and your current code base) If you decide to upgrade to a new version of VM, you may find that the performance characteristics have changed and you may need to optimize again

So only it is really important to your application, otherwise it may be a waste of energy

It's best to focus on your algorithm and its temporal and spatial complexity first; Any gain will increase forever

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>