关于做大量计算的三个问题

时间:2021-12-12 13:07:49

This is just a series of questions regarding doing lots of computations. Either I couldn't find the answers online or I still need clarification.

这只是关于进行大量计算的一系列问题。要么我在网上找不到答案,要么我还需要澄清。

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

    传递(float,float,float)作为方法参数vs(float [])是否更快,其中数组有三个项?

  2. Is it faster for a method to return a float[] vs setting the contents of a float[] that is passed to the method as an argument?

    方法返回float []与设置作为参数传递给方法的float []的内容相比更快吗?

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

    用实际计算替换方法调用是否更快,即A = sum(B,C)是否比A = B + C慢?假设sum(x,y){return x + y}

EDIT:

Thanks for all the answers, guys! Before I close this thread I have one more quick question if anyone knows:

谢谢你的所有答案,伙计们!在我关闭这个帖子之前,如果有人知道,我还有一个更快的问题:

  1. If I'm using a class to repeatedly calculate the same statistics over and over again (and then throwing them away), would it be any better to create instance variables to act as containers to avoid continuous re- and de-allocation?
  2. 如果我正在使用一个类一遍又一遍地重复计算相同的统计数据(然后将它们抛弃),那么创建实例变量以充当容器以避免连续的重新分配和解除分配会更好吗?

2 个解决方案

#1


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

传递(float,float,float)作为方法参数vs(float [])是否更快,其中数组有三个项?

That depends. If you already have the array of floats handy, it shouldn't make any difference whatsoever. If you are constructing the array every time, that would take some time for the assignments to the array, and possibly some time for the construction of the array.

那要看。如果你已经有一系列浮动方便,它应该没有任何区别。如果每次都在构造数组,则需要一些时间来分配数组,并且可能需要一些时间来构造数组。

Does it matter? If you do it in a tight loop that is executed a few million times in succession, and that many times during the life of your application, it may certainly do.

有关系吗?如果你在一个连续执行了数百万次的紧密循环中执行它,并且在应用程序的生命周期中多次执行它,它肯定会这样做。

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

方法返回float []与设置作为参数传递给方法的float []的内容相比更快吗?

If you need to construct the array of float every time for your return value, than that is certainly not going to be faster than setting values in a pre-existing array. Simply because both options involve setting values, and one of them has the extra task of creating a new array. But creating a new array may be really, really fast.

如果您需要每次为返回值构造float数组,那么肯定不会比在预先存在的数组中设置值更快。仅仅因为两个选项都涉及设置值,其中一个选项具有创建新数组的额外任务。但是创建一个新阵列可能非常非常快。

Still, if you do it many millions of times in your app in quick succession, benchmark it, it may save you some time.

尽管如此,如果您在应用程序中快速连续执行数百万次,对其进行基准测试,可能会为您节省一些时间。

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

用实际计算替换方法调用是否更快,即A = sum(B,C)是否比A = B + C慢?假设sum(x,y){return x + y}

Almost impossible to say. The builtin HotSpot code optimizer is pretty good at discovering such things and optimizing that for your.

几乎不可能说。内置的HotSpot代码优化器非常擅长发现这些东西并为你的优化。

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

如果您对此进行基准测试,请尝试将sum方法设为私有,这将使HotSpot更容易确定它可以被内联(尽管如果您没有任何被覆盖的sum方法的实现,它也会自己发现它)


The one thing about benchmarking here is:

关于基准测试的一点是:

It may help your application right now, with the current version of the VM that you are using (and your current codebase). If you decide to upgrade to a new version of the VM, you may discover that the performance characteristics change, and you may need to optimize again.

它可能会帮助您的应用程序,使用您正在使用的当前版本的VM(以及您当前的代码库)。如果您决定升级到新版本的VM,您可能会发现性能特征发生变化,您可能需要再次进行优化。

So only do it if it really matters to your application, otherwise it may be wasted effort.

所以只有它对你的应用程序真的很重要,否则可能是浪费精力。

Better to focus on your algorithm and its space and time complexities first; any gains there are gains forever.

最好先关注您的算法及其时空复杂性;任何收益都会永远增加。

#2


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

1)传递(浮点数,浮点数,浮点数)作为方法参数vs(float [])是否更快,其中数组有三个项?

1.) Depends. If the separate floats aren't contiguous in memory then a float[] could help.

1.)取决于。如果单独的浮点数在内存中不连续,则float []可以提供帮助。

2) Is it faster for a method to return a float[] vs setting the contents of a float[] that is passed to the method as an argument?

2)方法返回float [] vs设置作为参数传递给方法的float []的内容是否更快?

2.) Depends on if the float[] already exists in either case. If you're creating a new float[] and passing it in, or creating a new float[] and returning it, the cost is the same. But if in either case you can use an existing float[] somehow, that will be faster and create less allocations.

2.)取决于浮动[]是否已经存在。如果你正在创建一个新的float []并将其传入,或者创建一个新的float []并返回它,那么成本是相同的。但是如果在任何一种情况下都可以以某种方式使用现有的float [],那么速度会更快并且创建的分配更少。

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

3)用实际计算替换方法调用是否更快,即A = sum(B,C)是否比A = B + C慢?假设sum(x,y){return x + y}

3.) I'm not sure, I'm more of a C# programmer. I know that on basic CLR (common language runtime) like used by the Xbox 360 when running C#, manual calculations were far cheaper than using overloaded methods. I'm not sure if Java has similar issues on any platform.

3.)我不确定,我更像是一名C#程序员。我知道在运行C#时,Xbox 360使用的基本CLR(公共语言运行时),手动计算比使用重载方法便宜得多。我不确定Java是否在任何平台上都有类似的问题。

#1


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

传递(float,float,float)作为方法参数vs(float [])是否更快,其中数组有三个项?

That depends. If you already have the array of floats handy, it shouldn't make any difference whatsoever. If you are constructing the array every time, that would take some time for the assignments to the array, and possibly some time for the construction of the array.

那要看。如果你已经有一系列浮动方便,它应该没有任何区别。如果每次都在构造数组,则需要一些时间来分配数组,并且可能需要一些时间来构造数组。

Does it matter? If you do it in a tight loop that is executed a few million times in succession, and that many times during the life of your application, it may certainly do.

有关系吗?如果你在一个连续执行了数百万次的紧密循环中执行它,并且在应用程序的生命周期中多次执行它,它肯定会这样做。

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

方法返回float []与设置作为参数传递给方法的float []的内容相比更快吗?

If you need to construct the array of float every time for your return value, than that is certainly not going to be faster than setting values in a pre-existing array. Simply because both options involve setting values, and one of them has the extra task of creating a new array. But creating a new array may be really, really fast.

如果您需要每次为返回值构造float数组,那么肯定不会比在预先存在的数组中设置值更快。仅仅因为两个选项都涉及设置值,其中一个选项具有创建新数组的额外任务。但是创建一个新阵列可能非常非常快。

Still, if you do it many millions of times in your app in quick succession, benchmark it, it may save you some time.

尽管如此,如果您在应用程序中快速连续执行数百万次,对其进行基准测试,可能会为您节省一些时间。

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

用实际计算替换方法调用是否更快,即A = sum(B,C)是否比A = B + C慢?假设sum(x,y){return x + y}

Almost impossible to say. The builtin HotSpot code optimizer is pretty good at discovering such things and optimizing that for your.

几乎不可能说。内置的HotSpot代码优化器非常擅长发现这些东西并为你的优化。

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

如果您对此进行基准测试,请尝试将sum方法设为私有,这将使HotSpot更容易确定它可以被内联(尽管如果您没有任何被覆盖的sum方法的实现,它也会自己发现它)


The one thing about benchmarking here is:

关于基准测试的一点是:

It may help your application right now, with the current version of the VM that you are using (and your current codebase). If you decide to upgrade to a new version of the VM, you may discover that the performance characteristics change, and you may need to optimize again.

它可能会帮助您的应用程序,使用您正在使用的当前版本的VM(以及您当前的代码库)。如果您决定升级到新版本的VM,您可能会发现性能特征发生变化,您可能需要再次进行优化。

So only do it if it really matters to your application, otherwise it may be wasted effort.

所以只有它对你的应用程序真的很重要,否则可能是浪费精力。

Better to focus on your algorithm and its space and time complexities first; any gains there are gains forever.

最好先关注您的算法及其时空复杂性;任何收益都会永远增加。

#2


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

1)传递(浮点数,浮点数,浮点数)作为方法参数vs(float [])是否更快,其中数组有三个项?

1.) Depends. If the separate floats aren't contiguous in memory then a float[] could help.

1.)取决于。如果单独的浮点数在内存中不连续,则float []可以提供帮助。

2) Is it faster for a method to return a float[] vs setting the contents of a float[] that is passed to the method as an argument?

2)方法返回float [] vs设置作为参数传递给方法的float []的内容是否更快?

2.) Depends on if the float[] already exists in either case. If you're creating a new float[] and passing it in, or creating a new float[] and returning it, the cost is the same. But if in either case you can use an existing float[] somehow, that will be faster and create less allocations.

2.)取决于浮动[]是否已经存在。如果你正在创建一个新的float []并将其传入,或者创建一个新的float []并返回它,那么成本是相同的。但是如果在任何一种情况下都可以以某种方式使用现有的float [],那么速度会更快并且创建的分配更少。

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

3)用实际计算替换方法调用是否更快,即A = sum(B,C)是否比A = B + C慢?假设sum(x,y){return x + y}

3.) I'm not sure, I'm more of a C# programmer. I know that on basic CLR (common language runtime) like used by the Xbox 360 when running C#, manual calculations were far cheaper than using overloaded methods. I'm not sure if Java has similar issues on any platform.

3.)我不确定,我更像是一名C#程序员。我知道在运行C#时,Xbox 360使用的基本CLR(公共语言运行时),手动计算比使用重载方法便宜得多。我不确定Java是否在任何平台上都有类似的问题。