优化方法 [英] Method optimization

查看:122
本文介绍了优化方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了很多,如果它和所有的报表都需要我真的不能删除任何的void函数。但我觉得,它可以做的更好。使用一些 LINQ.Where ,类或类似这样的东西。我想优化,并表示无效平滑中的字符尽可能最少的:

 无效平滑(REF INT botChips,裁判布尔botTurn,标签botStatus,诠释名称,INT N,INT R){
随机兰特=新的随机();
INT RND = rand.Next(1,3);
如果(发== 0 ||轮== 1)
{
如果(调用< = 0)
{
检查(REF botTurn,botStatus) ;
}
,否则
{
如果(调用> = RoundN(botChips,N))
{
调用(REF botChips,裁判botTurn,botStatus );
}
,否则
{
如果(botChips> =通话* 2)
{
升高* = 2;
募集(REF botChips,裁判botTurn,botStatus);
}
,否则
{
调用(REF botChips,裁判botTurn,botStatus);
}
}
}
}
如果(发== 2 ||回合== 3)
{
如果(调用< ; = 0)
{
如果(RND == 1)
{
升高= RoundN(botChips,R);
募集(REF botChips,裁判botTurn,botStatus);
}
,否则如果(RND = 1&安培;!&安培;发== 2)
{
检查(REF botTurn,botStatus);
}
}
,否则
{
如果(调用> = RoundN(botChips,R))
{
如果(botChips> ;调用)
{
调用(REF botChips,裁判botTurn,botStatus);
}
如果(botChips< =调用)
{
募集= FALSE;
botTurn = FALSE;
botChips = 0;
botStatus.Text =呼叫+呼叫;
tbPot.Text =(int.Parse(tbPot.Text)+电话)的ToString();
}
}
,否则
{
如果(提高&下; =(RoundN(botChips,R))/ 2)
{
提高= RoundN(botChips,R);
募集(REF botChips,裁判botTurn,botStatus);
}
,否则
{
升高* = 2;
募集(REF botChips,裁判botTurn,botStatus);
}
}
}
}
}

RoundN

 私有静态双RoundN(INT sChips,INT N){
双A = Math.Round((sChips / N)/ 100D,0)* 100;
返回;
}



折叠

 私人无效折(REF布尔sTurn,裁判布尔SFTurn,标签sStatus){
募集= FALSE;
sStatus.Text =折;
sTurn = FALSE;
SFTurn = TRUE;
}



检查

 私人无效检查(REF布尔C打开,标签cStatus){
cStatus.Text = 检查;
C打开= FALSE;
募集= FALSE;
}



呼叫

 私人无效呼叫(REF INT sChips,裁判布尔sTurn,标签sStatus){
募集= FALSE;
sTurn = FALSE;
sChips - =呼叫;
sStatus.Text =呼叫+呼叫;
tbPot.Text =(int.Parse(tbPot.Text)+电话)的ToString();
}



募集

 私人无效提高(REF INT sChips,裁判布尔sTurn,标签sStatus){
sChips - = Convert.ToInt32(提高);
sStatus.Text =提升+提高;
tbPot.Text =(int.Parse(tbPot.Text)+ Convert.ToInt32(提高))的ToString()。
调用= Convert.ToInt32(提高);
募集= TRUE;
sTurn = FALSE;
}


解决方案

平滑方法可以的简化的(或在您的任期:优化的?)在某些方面:




  1. 要删除条件(的if-else )嵌套块,考虑使用的早期收益的条件,是在两个间简单或没有进一步的延续。这样一来,您就可以取出难读嵌套块。

  2. 要避免重复模块,具有相同的动作块应该被认为是组合在一起的,而不是分离。

  3. 如果想扭转的条件可以帮助简化代码

  4. 漏洞,你知道语言评价任何有益的行为。例如,的C#,在条件语句类似的说法如果(A || b)情况下,在离开表达(即: A )将首先评估 - 这被称为短路求

  5. 只要有可能,没有显著不失可读性,可以考虑使用的三元运算符来替换的if-else 块。

  6. 声明,您将使用多次的变量不改变值唯一的一次

  7. 当心重叠(翻了一番/复制)的条件!

  8. 使用的正确数据类型的将会的帮助!



有关你的情况下,简单的代码可以是这样的

  UINT轮= 0; //读取8 
无效平滑(REF INT botChips,裁判布尔botTurn,标签botStatus,诠释的名字,诠释N,INT R){
随机兰特=新的随机();
INT RND = rand.Next(1,3);
如果(发< = 1){//读取8
如果(调用< = 0){
检查(REF botTurn,botStatus); //因为你的检查没有改变回合,这是合法的
的回报; //读取1.提前返回
} //超出此调用> 0
如果(调用> = RoundN(botChips,N)|| botChips<拨打* 2){//读取2,3,4,和7
调用(REF botChips ,楼盘botTurn,botStatus);
的回报; //读取1
} //超越这两者条件
提高* = 2则相反;
募集(REF botChips,裁判botTurn,botStatus);
}

如果(发== 2 ||回合== 3){
如果(调用< = 0){
如果(RND == 1 ){//调用< = 0时,RND == 1,类似于呼叫<块; rNBChips,可能潜在进一步简化
升高= RoundN(botChips,R);
募集(REF botChips,裁判botTurn,botStatus);
}否则如果(发== 2)//读取7. RND绝对不会是1,不需要进一步的检查
检查(REF botTurn,botStatus);
的回报; // 1。这是有效的,因为你不希望继续
}
双rNBChips = RoundN(botChips,R); //读取6.这样就避免了多个呼叫。它既更短,更快的
如果(调用< rNBChips){//读取3
升高=提高< = rNBChips / 2? rNBChips:提高* 2; //读取募集5,
(REF botChips,裁判botTurn,botStatus);
的回报; //读取1
}
如果(botChips>调用){
调用(REF botChips,裁判botTurn,botStatus);
的回报; //读取1
}
募集= FALSE;
botTurn = FALSE;
botChips = 0;
botStatus.Text =呼叫+呼叫;
tbPot.Text =(int.Parse(tbPot.Text)+电话)的ToString();
}
}

如果没有它甚至看起来有很多更紧凑的意见,像这样

  UINT轮= 0; 
无效平滑(REF INT botChips,裁判布尔botTurn,标签botStatus,诠释名称,INT N,INT R){
随机兰特=新的随机();
INT RND = rand.Next(1,3);
如果(发< = 1){
如果(调用< = 0){
检查(REF botTurn,botStatus);
的回报;
}
如果(调用> = RoundN(botChips,N)|| botChips<拨打* 2){
调用(REF botChips,裁判botTurn,botStatus);
的回报;
}
升高* = 2;
募集(REF botChips,裁判botTurn,botStatus);
}

如果(发== 2 ||回合== 3){
如果(调用< = 0){
如果(RND == 1 ){
升高= RoundN(botChips,R);
募集(REF botChips,裁判botTurn,botStatus);
}否则如果(发== 2)
检查(REF botTurn,botStatus);
的回报;
}
双rNBChips = RoundN(botChips,R);
如果(调用< rNBChips){
升高=提高< = rNBChips / 2? rNBChips:提高* 2;
募集(REF botChips,裁判botTurn,botStatus);
的回报;
}
如果(botChips>调用){
调用(REF botChips,裁判botTurn,botStatus);
的回报;
}
募集= FALSE;
botTurn = FALSE;
botChips = 0;
botStatus.Text =呼叫+呼叫;
tbPot.Text =(int.Parse(tbPot.Text)+电话)的ToString();
}
}


I have a void function that has a lot of if statements in it and all of them are required I really can't remove anything. But I feel like that it could be done better. Using some LINQ.Where, classes or something like this. I want to optimize and express void Smooth in the fewest characters possible :

    void Smooth(ref int botChips, ref bool botTurn, Label botStatus, int name, int n, int r) {
        Random rand = new Random();
        int rnd = rand.Next(1, 3);
        if (rounds == 0 || rounds == 1)
        {
            if (call <= 0)
            {
                Check(ref botTurn, botStatus);
            }
            else
            {
                if (call >= RoundN(botChips, n))
                {
                    Call(ref botChips, ref botTurn, botStatus);
                }
                else
                {
                    if (botChips >= call * 2)
                    {
                        Raise *= 2;
                        Raised(ref botChips, ref botTurn, botStatus);
                    }
                    else
                    {
                        Call(ref botChips, ref botTurn, botStatus);
                    }
                }
            }
        }
        if (rounds == 2 || rounds == 3)
        {
            if (call <= 0)
            {
                if (rnd == 1)
                {
                    Raise = RoundN(botChips, r);
                    Raised(ref botChips, ref botTurn, botStatus);
                }
                else if (rnd!=1 && rounds==2)
                {
                    Check(ref botTurn, botStatus);
                }
            }
            else
            {
                if (call >= RoundN(botChips, r))
                {
                    if (botChips > call)
                    {
                        Call(ref botChips, ref botTurn, botStatus);
                    }
                    if (botChips <= call)
                    {
                        raising = false;
                        botTurn = false;
                        botChips = 0;
                        botStatus.Text = "Call " + call;
                        tbPot.Text = (int.Parse(tbPot.Text) + call).ToString();
                    }
                }
                else
                {
                    if (Raise <= (RoundN(botChips, r)) / 2)
                    {
                        Raise = RoundN(botChips, r);
                        Raised(ref botChips, ref botTurn, botStatus);
                    }
                    else
                    {
                        Raise *= 2;
                        Raised(ref botChips, ref botTurn, botStatus);
                    }
                }
            }
        }
    }

RoundN method

    private static double RoundN(int sChips, int n) {
        double a = Math.Round((sChips / n) / 100d, 0) * 100;
        return a;
    }

Fold method

    private void Fold(ref bool sTurn, ref bool SFTurn, Label sStatus) {
        raising = false;
        sStatus.Text = "Fold";
        sTurn = false;
        SFTurn = true;
    }

Check method

    private void Check(ref bool cTurn, Label cStatus) {
        cStatus.Text = "Check";
        cTurn = false;
        raising = false;
    }

Call method

    private void Call(ref int sChips, ref bool sTurn, Label sStatus) {
        raising = false;
        sTurn = false;
        sChips -= call;
        sStatus.Text = "Call " + call;
        tbPot.Text = (int.Parse(tbPot.Text) + call).ToString();
    }

Raised method

    private void Raised(ref int sChips, ref bool sTurn, Label sStatus) {
        sChips -= Convert.ToInt32(Raise);
        sStatus.Text = "Raise " + Raise;
        tbPot.Text = (int.Parse(tbPot.Text) + Convert.ToInt32(Raise)).ToString();
        call = Convert.ToInt32(Raise);
        raising = true;
        sTurn = false;
    }

解决方案

The Smooth method can be simplified (or in your term: optimized?) in some ways:

  1. To remove the conditional (if-else) nested blocks, consider the use of early return for conditions which are simpler among the two or has no further continuation. This way, you may remove "difficult-to-read" nested blocks.
  2. To avoid "duplicate" blocks, blocks with identical actions should be considered to be grouped together rather than being separated.
  3. Think if reversing the condition can help to simplify your code
  4. Exploit whatever beneficial behaviors that you know about the language evaluation. For instance, for C#, in the argument of the conditional statement like if (a || b) case, the left expression (that is: a) will be evaluated first - this is known as Short Circuit Evaluation.
  5. Whenever possible, and without significantly losing the readability, consider of using Ternary operator to replace if-else block.
  6. Declare variable that you will be using multiple times without changing the value only once
  7. Watch out for overlapping (doubled/duplicated) conditions!
  8. Use correct data type will help!

For your case, the simplified code can be something like this

uint rounds = 0; //read 8.
void Smooth(ref int botChips, ref bool botTurn, Label botStatus, int name, int n, int r) {
    Random rand = new Random();
    int rnd = rand.Next(1, 3);
    if (rounds <= 1) { //read 8.
        if (call <= 0) {
            Check(ref botTurn, botStatus); //since your Check doesn't change rounds, this is legal                  
            return; //read 1. early return                  
        } //beyond this call > 0
        if (call >= RoundN(botChips, n) || botChips < call * 2) { //read 2., 3., 4., and 7.
            Call(ref botChips, ref botTurn, botStatus);
            return; //read 1.
        } //beyond this is the opposite of both conditions
        Raise *= 2;
        Raised(ref botChips, ref botTurn, botStatus);
    }

    if (rounds == 2 || rounds == 3) {
        if (call <= 0) {
            if (rnd == 1) { //call <= 0, rnd == 1, similar to the block on call < rNBChips, may potentially be further simplified
                Raise = RoundN(botChips, r);
                Raised(ref botChips, ref botTurn, botStatus);
            } else if (rounds == 2) //read 7. rnd is definitely not 1, no need for further check
                Check(ref botTurn, botStatus);
            return; //read 1. this is valid since you don't want to continue
        }
        double rNBChips = RoundN(botChips, r); //read 6. this way you avoid multiple calls. It both shorter and faster
        if (call < rNBChips) { //read 3.
            Raise = Raise <= rNBChips / 2 ? rNBChips : Raise * 2; //read 5.
            Raised(ref botChips, ref botTurn, botStatus);
            return; // read 1.
        }
        if (botChips > call) {
            Call(ref botChips, ref botTurn, botStatus);
            return; //read 1.
        }
        raising = false;
        botTurn = false;
        botChips = 0;
        botStatus.Text = "Call " + call;
        tbPot.Text = (int.Parse(tbPot.Text) + call).ToString();
    }
}

Without the comments it even looks a lot more compact, like this

uint rounds = 0;
void Smooth(ref int botChips, ref bool botTurn, Label botStatus, int name, int n, int r) {
    Random rand = new Random();
    int rnd = rand.Next(1, 3);
    if (rounds <= 1) {
        if (call <= 0) {
            Check(ref botTurn, botStatus);              
            return; 
        }
        if (call >= RoundN(botChips, n) || botChips < call * 2) {
            Call(ref botChips, ref botTurn, botStatus);
            return;
        }
        Raise *= 2;
        Raised(ref botChips, ref botTurn, botStatus);
    }

    if (rounds == 2 || rounds == 3) {
        if (call <= 0) {
            if (rnd == 1) {
                Raise = RoundN(botChips, r);
                Raised(ref botChips, ref botTurn, botStatus);
            } else if (rounds == 2)
                Check(ref botTurn, botStatus);
            return;
        }
        double rNBChips = RoundN(botChips, r);
        if (call < rNBChips) {
            Raise = Raise <= rNBChips / 2 ? rNBChips : Raise * 2;
            Raised(ref botChips, ref botTurn, botStatus);
            return;
        }
        if (botChips > call) {
            Call(ref botChips, ref botTurn, botStatus);
            return;
        }
        raising = false;
        botTurn = false;
        botChips = 0;
        botStatus.Text = "Call " + call;
        tbPot.Text = (int.Parse(tbPot.Text) + call).ToString();
    }
}

这篇关于优化方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆