StopWatch

      为了判分,写了一段简易的代码,后来修改的时候发现原系统中自带这段代码,看着别人写的代码,觉得别人写的比自己的简洁多了,想着怎么人家就能把代码写的那么简洁,那么效率高.想到曹建新提过,什么测试代码的运行时间来看代码的效率,我也想看看我的代码和别人的代码到底差多少.

         上网查找Stopwatch,用这样的一段代码就能够测试代码的效率.

 

[csharp]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. System.Diagnostics.Stopwatch stopwatch = new Stopwatch();  
  2. stopwatch.Start(); //  开始监视代码运行时间  
  3. //  you code ....  
  4. stopwatch.Stop(); //  停止监视  
  5. TimeSpan timespan = stopwatch.Elapsed; //  获取当前实例测量得出的总时间  
  6. double hours = timespan.TotalHours; // 总小时  
  7. double minutes = timespan.TotalMinutes;  // 总分钟 ...  

             我的代码是这样的:

 

 

[csharp]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();  
  2. stopwatch.Start(); //  开始监视代码运行时间  
  3.   
  4. string strCorAnsMy = "chemistry#living#everything#outside#working out#junior#the way#taught#experiments#single"//正确答案  
  5. string strStuAnsMy = "living#everything#outside#working out#junior#the way#taught#experiments#single";          //学生答案  
  6. string strIsOrdMy = "有序";//是否有序,测试时需要修改这里  
  7.   
  8. double dlTotalMy = 0;//总分  
  9.   
  10. string[] strArrCorAnsMy = strCorAnsMy.Split('#');  //拆分正确答案  
  11. string[] strArrStuAnsMy = strStuAnsMy.Split('#');  //拆分学生答案  
  12.   
  13. for (int i = 0; i < strArrCorAnsMy.Length; i++)  
  14. {  
  15.     strArrCorAnsMy[i] = strArrCorAnsMy[i].Trim();   //去正确答案的空格  
  16. }  
  17.   
  18. for (int i = 0; i < strArrStuAnsMy.Length; i++) //去学生答案空格  
  19. {  
  20.     strArrStuAnsMy[i] = strArrStuAnsMy[i].Trim();  
  21. }  
  22.   
  23. if (strIsOrdMy == "无序")  
  24. {  
  25.     //由于无序,所以对于正确答案和学生答案进行for循环,比较  
  26.     for (int i = 0; i < strArrCorAnsMy.Length; i++)  
  27.     {  
  28.         for (int j = 0; j < strArrStuAnsMy.Length; j++)  
  29.         {  
  30.             //如果学生答案等于正确答案,分数+1,并跳出  
  31.             if (strArrCorAnsMy[i].Equals(strArrStuAnsMy[j]))  
  32.             {  
  33.                 dlTotalMy += 1; //每题1分,共9分  
  34.                 break;  
  35.             }  
  36.         }  
  37.     }  
  38. }  
  39. else  //有序是这样  
  40. {  
  41.     //循环学生的成绩  
  42.     for (int i = 0; i < strArrStuAnsMy.Length; i++)  
  43.     {  
  44.         //如果正确答案和学生答案一一对应,则正确  
  45.         if (strArrCorAnsMy[i].Equals(strArrStuAnsMy[i]))  
  46.         {  
  47.             dlTotalMy += 1; //每题1分,得0分  
  48.         }  
  49.     }  
  50. }  
  51. stopwatch.Stop(); //  停止监视          
  52. TimeSpan timespan = stopwatch.Elapsed; //  获取当前实例测量得出的总时间  
  53. doule hours = timespan.TotalHours; // 总小时 断点设在这里double minutes = timespan.TotalMinutes;  // 总分钟 ...  

 

            别人的代码经过修改,是这样的.

[csharp]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();  
  2. stopwatch.Start(); //  开始监视代码运行时间   
  3. string strCorAns = "chemistry#living#everything#outside#working out#junior#the way#taught#experiments#single"//正确答案  
  4. string strStuAns = "living#everything#outside#working out#junior#the way#taught#experiments#single";          //学生答案  
  5. string strIsOrd = "有序";//是否有序,测试时需要修改这里  
  6.   
  7. double dlTotal = 0;//总分  
  8.   
  9. string[] strArrCorAns = strCorAns.Split('#');      //拆分正确答案  
  10. string[] strArrStuAns = strStuAns.Split('#');//拆分学生答案  
  11.   
  12. if (strIsOrd == "无序")     //无序的话,只要写对了,在哪个空填的都无所谓  
  13. {  
  14.     for (int i = 0; i < strArrCorAns.Length; i++)  //循环正确答案  
  15.     {  
  16.         if (strStuAns.IndexOf(strArrCorAns[i].ToString()) > -1) //比较正确答案在学生的答案是否存在  
  17.         {  
  18.             dlTotal += 1;                         //每题1分,得9分  
  19.         }  
  20.     }  
  21. }  
  22. else   //表示有序,必须要和正确答案的顺序一模一样   
  23. {  
  24.   
  25.     //循环学生答案  
  26.     for (int j = 0; j < strArrStuAns.Length; j++)  
  27.     {  
  28.         //如果学生的答案长度大于0,正确答案的长度也大于0,并且正确的答案长度>=学生的答案长度   
  29.         if (strArrStuAns.Length > 0 && strArrCorAns.Length > 0 && strArrCorAns.Length >= strArrStuAns.Length)  
  30.         {  
  31.             //如果学生答案不为null  
  32.             if (strArrCorAns[j] != null)  
  33.             {  
  34.                 //比较学生答案和正确答案去空格之后是否相同  
  35.                 if (strArrStuAns[j].ToString().Trim() == strArrCorAns[j].ToString().Trim())  
  36.                 {  
  37.                     dlTotal += 1;    //有序,得分为0分  
  38.                 }  
  39.             }  
  40.         }  
  41.     }  
  42. }  
  43. stopwatch.Stop(); //  停止监视  
  44. TimeSpan timespan = stopwatch.Elapsed; //  获取当前实例测量得出的总时间                       
  45. double hours = timespan.TotalHours; // 总小时,断点设在这里  
  46. double minutes = timespan.TotalMinutes;  // 总分钟 ...  

         时间运行情况,如下面图片显示,我的在前面.先是测试有序的情况.

         有序时,我的代码的时间为 0.0000068.


 

         有序时,别人的代码的时间为0.0000068,一样.

         无序时,我的代码的时间为0.0000068.


 

           无序时,别人的代码时间为0.0000441,我的比较快.


         还有其他情况,比如我现在的学生代码,基本和正确答案差不多的顺序,如果我改成,完全不一样的顺序,运行效率是否会有很大区别?

 

stringstrCorAnsMy = "chemistry#living#everything#outside#working out#junior#theway#taught#experiments#single"; //正确答案

string strStuAnsMy ="single#experiments#taught#the way#junior#workingout#outside#everything#living"; //学生答案掉个个,再测试下无序时的运行时间,发现基本没差别.

0.00000068,和上面一样.

  

         0.0000441,和上面一样


 

 

          后来,运行多次发现,timespan不是一定的,这次时间为0.0000441,下次可能就是0.0000498,但是大体上还是差不多的.

         这样看来我的代码运行起来也不是很慢,尤其是无序时.有序时,采用的都是一重循环,我的代码没有做太多的处理,所以可能不安全.无序时,我采用的是,双重循环和equals,而他采用的是一重循环和indexof,照理应该是双重循环慢,所以猜想IndexOf的效率可能比较慢.

        想到老师说的,果然是不怕不知道,就怕不知道.以前不知道要测试代码的效率,现在想起来了,立马就能用,以后可以多多测试测试,了解了解不同代码的运行效率,开发更加优化的代码.

 

还有除了stopwatch,那人的代码使用的时候是有问题的,比如我把正确答案和学生答案改为这样.

 

[csharp]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. string strCorAnsMy = "a";  
  2.           string strStuAnsMy ="an";  //得分为0  

 

         我的代码显示这个得0.但是他的代码运行起来,因为用的是strStuAns.IndexOf(strArrCorAns[i]),所以an.IndexOf(a),>-1,所以

 

[csharp]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. string strCorAns = "a";  
  2.  string strStuAns ="an";  //得分为1  

 

           所以,我的代码还是比较严谨的,尽管用的循环有些腻歪人.最后感慨一下,一段小小的代码,如果不严谨的考虑到各种情况,还是问题多多的,所以测试真的是很有必要的,要不然光看着这段代码,我可看不出什么大问题.

猜你喜欢

转载自sangei.iteye.com/blog/2279112