gpt4 book ai didi

c# - 在 for 循环中计算特定事件的有效方法

转载 作者:太空宇宙 更新时间:2023-11-03 23:13:14 24 4
gpt4 key购买 nike

我有一个大约一百万个值的循环。我只想存储每个值的指数以显示它们,例如在直方图中。

目前我是这样做的:

int histogram[51]; //init all with 0
for(int i = 0; i < 1000000; i++)
{
int exponent = getExponent(getValue(i));
//getExponent(double value) gives the exponent(base 10)
//getValue(int i) gives the value for loop i
if(exponent > 25)
exponent = 25;
if(exponent < -25)
exponent = -25;
histogramm[exponent+25]++;
}

是否有更高效、更优雅的方式来做到这一点?
也许不使用数组?

最佳答案

Is there a more efficient and elegant way of doing this?

唯一更优雅的方法是使用 Math.MinMath.Max 但它不会更有效。 histogramm[Math.Max(0,Math.Min(50,exponent+25))]++ 字符更少,但在我看来没有更高的性能或优雅。

Perhaps without using an array?

数组是一组原始值,因此是存储它们的最直接方式。

关于c# - 在 for 循环中计算特定事件的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38232513/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com