C# simple方法的优化

C# simple方法的优化,c#,optimization,unity3d,C#,Optimization,Unity3d,在下面给出的方法中,有很多铸造,据我所知,这对于CLR来说是相当舒适的。我想优化它,但我不能更改此方法签名 public uint GetValueBetween (uint beginColor, uint endColor , float percent) { int difference = (int)endColor - (int)beginColor; // can be signed negative integer float fValue = UnityEngi

在下面给出的方法中,有很多铸造,据我所知,这对于CLR来说是相当舒适的。我想优化它,但我不能更改此方法签名

public uint GetValueBetween (uint beginColor, uint endColor , float percent)
{
    int difference = (int)endColor - (int)beginColor; // can be signed negative integer

    float fValue = UnityEngine.Mathf.Abs((float)difference) * percent; // percent must be given in range 0.0 - 1.0, that is why I am not dividing it by 100;

    if (difference < 0)
    {
        return beginColor - (uint)UnityEngine.Mathf.RoundToInt (fValue);
    }

    return (uint)UnityEngine.Mathf.RoundToInt (fValue);
}
public uint GetValueBetween(uint beginColor、uint endColor、float percent)
{
int差=(int)endColor-(int)beginColor;//可以是有符号负整数
float fValue=UnityEngine.Mathf.Abs((float)difference)*percent;//percent必须在0.0-1.0范围内给出,这就是为什么我不把它除以100;
如果(差异<0)
{
返回beginColor-(uint)UnityEngine.Mathf.RoundToInt(fValue);
}
返回(uint)单位发动机.Mathf.RoundToInt(fValue);
}
我对如何删除强制类型几乎没有什么想法,比如不在该方法主体的第一行中从
uint
3次将
Int32
强制类型转换为
uint
3次,但只需检查哪个数字更大,然后将其余代码全部应用到它


无论如何,我想问问你,你的想法是什么。

根据你的要求,我会做以下事情

public uint GetValueBetween(uint beginColor, uint endColor , float percent)
{
    float difference = endColor - beginColor; // can be signed negative integer

    // percent must be given in range 0.0 - 1.0, that is why I am not dividing it by 100;
    float fValue = UnityEngine.Mathf.Abs(difference) * percent;
    var rounded = (uint)UnityEngine.Mathf.RoundToInt(fValue);
    if (difference < 0)
    {
        rounded = beginColor - rounded;
    }

    return rounded;
}
public uint GetValueBetween(uint beginColor、uint endColor、float percent)
{
float difference=endColor-beginColor;//可以是有符号的负整数
//百分比必须在0.0-1.0范围内给出,这就是为什么我不把它除以100;
浮动价值=单位发动机数学绝对值(差值)*百分比;
var rounded=(uint)UnityEngine.Mathf.RoundToInt(fValue);
如果(差异<0)
{
四舍五入=beginColor-四舍五入;
}
返回四舍五入;
}

不一定要更“高效”,但至少更干净一点。

根据您的回答要求,我会做以下工作

public uint GetValueBetween(uint beginColor, uint endColor , float percent)
{
    float difference = endColor - beginColor; // can be signed negative integer

    // percent must be given in range 0.0 - 1.0, that is why I am not dividing it by 100;
    float fValue = UnityEngine.Mathf.Abs(difference) * percent;
    var rounded = (uint)UnityEngine.Mathf.RoundToInt(fValue);
    if (difference < 0)
    {
        rounded = beginColor - rounded;
    }

    return rounded;
}
public uint GetValueBetween(uint beginColor、uint endColor、float percent)
{
float difference=endColor-beginColor;//可以是有符号的负整数
//百分比必须在0.0-1.0范围内给出,这就是为什么我不把它除以100;
浮动价值=单位发动机数学绝对值(差值)*百分比;
var rounded=(uint)UnityEngine.Mathf.RoundToInt(fValue);
如果(差异<0)
{
四舍五入=beginColor-四舍五入;
}
返回四舍五入;
}

不一定要更“高效”,但至少有点干净。

在相同大小的类型(例如uint和int)之间转换通常是零成本的,因为它只是从无符号cpu指令更改为有符号cpu指令

但是,从int到float的转换可能非常昂贵,首先是因为它需要一个非常简单的格式转换(所有浮点值都被标准化为一个数字,所以计算N表示1.xxxxx*2^N),而且还因为浮点寄存器是现代CPU中的向量,因此,您正在将单个值加载到一个寄存器中,该寄存器可以保存其中的8到32个值

由于实际上根本不使用浮点值,并且将Abs应用于有符号值,因此只需将浮点百分比缩放为适当的整数值,就可以很容易地消除大部分开销(这里我使用100,但您可以选择一个更大的值,只需记住在每个语句末尾除以等价的整数,您可以测试乘以2的幂是否比乘以10的幂更快,以防浮点单元费心为此进行优化)

public uint GetValueBetween(uint beginColor、uint endColor、float percent)
{
if(endColor

我将差异拆分为一个单独的变量来说明条件中的关键差异,但如果愿意,您可以将其内联。

在相同大小的类型(例如uint和int)之间进行强制转换通常是零成本的,因为它只是从无符号更改为有符号的cpu指令

但是,从int到float的转换可能非常昂贵,首先是因为它需要一个非常简单的格式转换(所有浮点值都被标准化为一位数字,所以计算N表示1.xxxxx*2^N),另外,由于浮点寄存器是现代CPU中的向量,因此将单个值加载到一个寄存器中,该寄存器可以容纳8到32个值

由于实际上根本不使用浮点值,并且将Abs应用于有符号值,因此只需将浮点百分比缩放为适当的整数值,就可以很容易地消除大部分开销(这里我使用100,但您可以选择一个更大的值,只需记住在每个语句末尾除以等价的整数,您可以测试乘以2的幂是否比乘以10的幂更快,以防浮点单元费心为此进行优化)

public uint GetValueBetween(uint beginColor、uint endColor、float percent)
{
if(endColor

我将差异拆分为单独的变量,以说明条件中的关键差异,但如果愿意,您可以将其内联。

虽然我不相信此方法会成为性能瓶颈,但至少可以大大简化该方法以删除分支:

public uint GetValueBetween(uint beginColor, uint endColor, float percent)
{
    return (uint) UnityEngine.Mathf.RoundToInt( ( 1 - percent ) * beginColor + percent * endColor );
}

虽然我不认为该方法会成为性能瓶颈,但至少可以大大简化该方法以删除分支:

public uint GetValueBetween(uint beginColor, uint endColor, float percent)
{
    return (uint) UnityEngine.Mathf.RoundToInt( ( 1 - percent ) * beginColor + percent * endColor );
}

您的方法与Unity的方法非常相似。这是它的代码

public static float LerpUnclamped (float a, float b, float t)
{
    return a + (b - a) * t;
}
因为它来自团结,我想我