Three questions about doing a lot of computing

This is just a series of questions regarding many computations. Either I could not find the answers online, or I still need an explanation.

  • Is it faster to pass (float, float, float) as parameters of the vs (float[]) method, where the array has three members?

  • Is it faster for a method to return float[] vs, setting the contents of float[] , which is passed to the method as an argument?

  • Is it faster to replace method calls with actual calculations, i.e. instead of A=sum(B,C) less than A=B+C ? assuming sum(x,y){return x+y}

EDIT:

Thanks for all the answers guys! Before closing this thread, I have another quick question if anyone knows:

  1. If I use a class to recalculate the same statistics over and over (and then discard them), would it be better to create instance variables to act like containers to avoid continuous re-distribution and de-distribution?
+6
source share
2 answers

Is it faster to pass (float, float, float) as parameters of the vs (float[]) method, where the array has three members?

It depends. If you already have an array of floats, this should not make any difference. If you create an array each time, it will take some time for assignments to the array and, possibly, some time for building the array.

Does it matter? If you do this in a tight loop that runs several million times in a row, and many times during the life of your application, this can certainly be done.

Is it faster for a method to return float [] against setting the contents of float [], which is passed to the method as an argument?

If you need to build a float array each time for your return value, then this, of course, will not be faster than setting values ​​in a pre-existing array. Just because both options include setting values, and one of them has the additional task of creating a new array. But creating a new array can be really, really fast.

However, if you do this many millions of times in your application quickly, compare it, this can save you some time.

Is it faster to replace method calls with actual calculations, i.e. instead of A=sum(B,C) less than A=B+C ? provided sum(x,y){return x+y}

It is almost impossible to say. HotSpot's built-in code optimizer is pretty good at things like this and optimizes this for yours.

If you are comparing this, try making the sum method private , which makes it easier for HotSpot to decide that it can be inlined (although it will also detect it by itself if you don't have overridden implementations of the sum method)


The only thing about benchmarking:

This can help your application right now, with the current version of the virtual machine that you are using (and the current code base). If you decide to upgrade to a new version of the virtual machine, you may find that the performance characteristics are changing and you may need to optimize again.

So do it only if it is really important for your application, otherwise it may be wasted.

It is better to first focus on your algorithm, as well as its spatial and temporal complexities; any gain is forever.

+6
source

1) Is it faster to pass (float, float, float) as parameters of the vs (float []) method, where the array has three members?

1.) Depends. If individual floats are not contiguous in memory, then float [] can help.

2) Is it faster for the method to return float [] against the setting, the contents of float [], which is passed to the method as an argument?

2.) Depends if float [] already exists anyway. If you create a new float [] and pass it, or create a new float [] and return it, the cost will be the same. But if in any case you can somehow use the existing float [], it will be faster and create fewer distributions.

3) Is it faster to replace method calls with actual calculations, that is, instead of A = sum (B, C), any slower than A = B + C? provided sum (x, y) {return x + y}

3.) I'm not sure, I'm more a C # programmer. I know that with the basic CLR (common runtime) used by the Xbox 360 when starting C #, manual calculations were much cheaper than using overloaded methods. I'm not sure Java has similar problems on any platform.

+2
source

Source: https://habr.com/ru/post/987099/


All Articles