优化此代码 [英] Optimization for this code

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

问题描述

我该如何优化我的代码?减少行数(声明)?



我尝试过:



Hi, how can I optimize my code? Reduce the number of lines(Statements)?

What I have tried:

private void button1_Click(object sender, EventArgs e)
      {
          string[] seq1 = { "A", "C", "G", "T", "A", "C", "A", "G","T","G" };
          string[] seq2 = { "T", "A", "C", "A", "C", "G","A","A","G","T" };
          string[] res1 = new string[seq1.Length];
          string[] res2 = new string[seq1.Length];
          for (int i = 0; i <= 4; i++)
          {

              res1[i] = seq1[i];
              res2[i] = seq2[i];
              textBox1.Text +=" "+ res1[i];
              textBox2.Text +=" "+ res2[i];
          }
          for(int i = 5; i < seq1.Length; i++)
          {
              if (seq1[i] == "T" )
              {
                  res1[i] = "A";

              }
              else
                  if (seq1[i] == "A" )
              {
                  res1[i] = "T";

              }
              else
                 if (seq1[i] == "C" )
              {
                  res1[i] = "G";

              }
              else
                 if (seq1[i] == "G" )
              {
                  res1[i] = "C";

              }
              textBox1.Text += res1[i]+"   ";

          }
          for (int i = 5; i < seq2.Length; i++)
          {
              if (seq2[i] == "T")
              {
                  res2[i] = "A";

              }
              else
                  if (seq2[i] == "A")
              {
                  res2[i] = "T";

              }
              else
                 if (seq2[i] == "C")
              {
                  res2[i] = "G";

              }
              else
                 if (seq2[i] == "G")
              {
                  res2[i] = "C";

              }

              textBox2.Text += res2[i] + "   ";
          }
      }

推荐答案

看来你的 seq 字符串只包含单个字符。然后不要使用字符串数组而是使用字符数组。这不会减少行数,但会减小程序大小并提高执行速度。



如果 if else $ c>条件切换语句。
It seems that your seq strings contain only single characters. Then don't use string arrays but character arrays. This will not reduce the number of lines but reduces the program size and increases execution speed.

Replace also the if else if conditions by switch statements.


那么问题是什么?

我看到了:

*使用直接访问控件,并更新循环中的Text属性 - 效率低,不可重复使用。

*否方法的名称或单击处理程序给出提示这一切是什么(我猜一些DNA计算?)

*没有评论,或有意义的变量名。

*硬编码常数 - 为什么4或5?

*除了你的控件的directaAccess之外,没有理由让这个方法是非静态的。

*低效的字符串连接 - 使用StringBuilder ..



这个代码需要担心的很多事情,行数?不,那是你遇到的最严重的问题......
So what's the Problem?
I see:
* using direct access to controls, and updating the Text properties in a Loop - inefficient, not reusable.
* No Name for the method or the click-handler to give a hint what this all is about (I'd guess some DNA calculations?)
* No comments, or meaningful variable names.
* hardcoded constants - why 4 or 5?
* no reason for the method to be non-static other than directaAccess of your controls.
* Inefficient string concatinations - use StringBuilder..

So a lot of things to worry about for this Code, number of lines? No that's the smalest problem you have there...


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

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