C# 方法优化
我有一个void函数,里面有很多if语句,它们都是必需的,我真的不能删除任何东西。但我觉得可以做得更好。使用一些C# 方法优化,c#,.net,optimization,readability,C#,.net,Optimization,Readability,我有一个void函数,里面有很多if语句,它们都是必需的,我真的不能删除任何东西。但我觉得可以做得更好。使用一些LINQ.Where、类或类似的东西。我希望以尽可能少的字符优化和表达void Smooth: void Smooth(ref int botChips, ref bool botTurn, Label botStatus, int name, int n, int r) { Random rand = new Random(); int rnd
LINQ.Where
、类或类似的东西。我希望以尽可能少的字符优化和表达void Smooth
:
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);
}
}
}
}
}
折叠
方法
private static double RoundN(int sChips, int n) {
double a = Math.Round((sChips / n) / 100d, 0) * 100;
return a;
}
private void Fold(ref bool sTurn, ref bool SFTurn, Label sStatus) {
raising = false;
sStatus.Text = "Fold";
sTurn = false;
SFTurn = true;
}
private void Check(ref bool cTurn, Label cStatus) {
cStatus.Text = "Check";
cTurn = false;
raising = false;
}
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();
}
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;
}
检查
方法
private static double RoundN(int sChips, int n) {
double a = Math.Round((sChips / n) / 100d, 0) * 100;
return a;
}
private void Fold(ref bool sTurn, ref bool SFTurn, Label sStatus) {
raising = false;
sStatus.Text = "Fold";
sTurn = false;
SFTurn = true;
}
private void Check(ref bool cTurn, Label cStatus) {
cStatus.Text = "Check";
cTurn = false;
raising = false;
}
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();
}
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;
}
调用
方法
private static double RoundN(int sChips, int n) {
double a = Math.Round((sChips / n) / 100d, 0) * 100;
return a;
}
private void Fold(ref bool sTurn, ref bool SFTurn, Label sStatus) {
raising = false;
sStatus.Text = "Fold";
sTurn = false;
SFTurn = true;
}
private void Check(ref bool cTurn, Label cStatus) {
cStatus.Text = "Check";
cTurn = false;
raising = false;
}
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();
}
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;
}
提出的
方法
private static double RoundN(int sChips, int n) {
double a = Math.Round((sChips / n) / 100d, 0) * 100;
return a;
}
private void Fold(ref bool sTurn, ref bool SFTurn, Label sStatus) {
raising = false;
sStatus.Text = "Fold";
sTurn = false;
SFTurn = true;
}
private void Check(ref bool cTurn, Label cStatus) {
cStatus.Text = "Check";
cTurn = false;
raising = false;
}
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();
}
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;
}
Smooth
方法可以通过某些方式简化(或者用您的术语:优化?):
if(a | | b)
这样的条件语句的参数中,将首先计算左表达式(即:a
),这称为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();
}
}
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();
}
}
uint轮数=0//读8。
无效平滑(参考整数botChips、参考布尔botTurn、标签botStatus、整数名称、整数n、整数r){
Random rand=新的Random();
int rnd=rand.Next(1,3);
如果(你试过评测吗?顺便说一句,我只会定义一次Random
,然后将这个大方法分成几个小方法(Ctrl+R,M
)我知道这不是你问的,但这是一个非常危险的使用new Random
;如果你在循环中调用Smooth
,它可能使用相同的种子。你应该使rand
成为一个实例变量,并在构造函数中初始化它。谢谢你的回答!不,我没有尝试分析,我会修复new Random
Define optimize。是否要分析输入值并重新构造代码,使其尽可能快地用于最常见的输入,而不管其含义有多神秘?是否希望代码以尽可能少的字符表示?内存缓存未命中率最低?…询问“您可以将此代码缩短多少?”就是在益智网站上问一个代码高尔夫问题。但是较短的代码很少更容易理解、更快、更好的代码。它只是较短。如果你想让代码更优雅、更易懂,那么这与缩短代码非常不同。从消除所有这些引用开始。如果你在计算值,请将值传递回原处而不是修改变量。