c# – 接受float []和double []数组的方法

c# – 接受float []和double []数组的方法,第1张

概述我有一个方法接受一个数组(浮点数或双精度数),开始和结束索引,然后对startIndex到endIndex范围内的索引进行一些元素 *** 作. 基本上它看起来像这样: public void Update(float[] arr, int startIndex, int endIndex){ if (condition1) { //Do some array manipulati 我有一个方法接受一个数组(浮点数或双精度数),开始和结束索引,然后对startIndex到endindex范围内的索引进行一些元素 *** 作.

基本上它看起来像这样:

public voID Update(float[] arr,int startIndex,int endindex){   if (condition1)   {     //Do some array manipulation   }   else if (condition2)   {     //Do some array manipulation   }   else if (condition3)   {       if (subcondition1)       {         //Do some array manipulation       }   }}

方法比这长,并涉及将一些元素设置为0或1,或者规范化数组.
问题是我需要在那里传递float []和double []数组,并且不希望有一个重复的代码接受double [].

性能也很关键,因此我不想创建一个新的double []数组,向其投射浮点数组,执行计算,然后通过强制转换为浮点数来更新原始数组.

是否有任何解决方案可以避免重复的代码,但也尽可能快?

解决方法 你有几个选择.它们都不符合您的要求,但根据您需要的 *** 作类型,您可能会接近.

第一种是使用泛型类型受限制的泛型方法,但您可以执行的唯一 *** 作是有限的:

public voID Update<T>(T[] arr,int endindex) : IComarable{   if (condition1)   {     //Do some array manipulation   }   else if (condition2)   {     //Do some array manipulation   }   else if (condition3)   {       if (subcondition1)       {         //Do some array manipulation       }   }}

并且该函数中的条件和数组 *** 作将仅限于使用以下形式的表达式:

if (arr[Index].Compareto(arr[OtherIndex])>0)arr[Index] = arr[OtherIndex];

这足以执行查找最小值或最大值或对数组中的项进行排序等 *** 作.它不能做加/减/等,所以这不能说,找到平均值.您可以通过为所需的任何其他方法创建自己的重载委托来弥补这一点:

public voID Update<T>(T[] arr,int endindex,Func<T,T> Add) : IComarable{   //...   arr[Index] = Add(arr[OtherIndex] + arr[Thirdindex]);}

对于你实际使用的每个 *** 作,你需要另一个参数,我不知道它将如何执行(最后一部分将成为这里的一个主题:我没有对此进行基准测试,但性能似乎对于这个问题).

想到的另一个选择是动态类型:

public voID Update(dynamic[] arr,int endindex){     //...logic here}

这应该可行,但对于一些被称为反复的事情,你声称我不知道它会对性能产生什么影响.

您可以将此选项与另一个答案(现已删除)结合使用,以回馈某些类型的安全性:

public voID Update(float[] arr,int endindex){    InternalUpdate(arr,startIndex,endindex);}public voID Update(double[] arr,endindex);}public voID InternalUpdate(dynamic[] arr,int endindex){     //...logic here}

另一个想法是将所有花车投射到双打:

public voID Update(float[] arr,int endindex){    Update( Array.ConvertAll(arr,x => (double)x),endindex);}public voID Update(double[] arr,int endindex){   //...logic here}

同样,这将重新分配数组,因此,如果这会导致性能问题,我们将不得不寻找其他地方.

如果(并且仅当)所有其他都失败,并且分析器显示这是代码的关键性能部分,则可以重载该方法并实现逻辑两次.从代码维护的角度来看,这并不理想,但如果性能问题得到充分证实和记录,那么复制意大利面的问题就是值得的.我添加了一个示例注释,以指示您可能希望如何记录此内容:

/******************   WARNING: Profiler tests conducted on 12/29/2014 showed that this is a critical            performance section of the code,and that separate double/float            implementations of this method produced a XX% speed increase.            If you need to change anything in here,be sure to change BOTH SETS,and be sure to profile both before and after,to be sure you            don't introduce a new performance bottleneck. */public voID Update(float[] arr,int endindex){    //...logic here}public voID Update(double[] arr,int endindex){    //...logic here}

这里要探讨的最后一个项目是,C#包含一个通用的ArraySegment<T>类型,您可能会发现它对此有用.

总结

以上是内存溢出为你收集整理的c# – 接受float []和double []数组的方法全部内容,希望文章能够帮你解决c# – 接受float []和double []数组的方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1248712.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存