none
c#代码执行效率低,需要27秒,哪位给提个优化方案 RRS feed

  • 问题

  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Management;
    
    namespace ClassLibrary1
    {
        //=====================================================
        //Copyright (C) 2008-2009 小科
        //All rights reserved
        //CLR版本:            2.0.50727.1433
        //新建项输入的名称: SoftReg
        //机器名称:            MRWXK
        //注册组织名:         
        //命名空间名称:      LimitSoftUseTimes
        //文件名:              SoftReg
        //当前系统时间:      2008-12-16 13:19:10
        //当前登录用户名:   Administrator
        //创建年份:           2008
        //http://www.mingribook.com
        //======================================================
        public class SoftReg
        {
            // 取得设备硬盘的卷标号
            public string GetDiskVolumeSerialNumber()
            {
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"d:\"");
                disk.Get();
                return disk.GetPropertyValue("VolumeSerialNumber").ToString();
            }
    
            //获得CPU的序列号
            public string getCpu()
            {
                string strCpu = null;
                ManagementClass myCpu = new ManagementClass("win32_Processor");
                ManagementObjectCollection myCpuConnection = myCpu.GetInstances();
                foreach (ManagementObject myObject in myCpuConnection)
                {
                    strCpu = myObject.Properties["Processorid"].Value.ToString();
                    break;
                }
                return strCpu;
            }
    
            //生成机器码
            public string getMNum()
            {
                string strNum = getCpu() + GetDiskVolumeSerialNumber();//获得24位Cpu和硬盘序列号
                string strMNum = strNum.Substring(0, 24);//从生成的字符串中取出前24个字符做为机器码
                return strMNum;
            }
    
            public int[] intCode = new int[127];//存储密钥
            public int[] intNumber = new int[25];//存机器码的Ascii值
            public char[] Charcode = new char[25];//存储机器码字
    
            public void setIntCode()//给数组赋值小于10的数
            {
                for (int i = 1; i < intCode.Length; i++)
                {
                    intCode[i] = i % 9;
                }
            }
    
            //生成注册码
            public string getRNum()
            {
                setIntCode();//初始化127位数组
                for (int i = 1; i < Charcode.Length; i++)//把机器码存入数组中
                {
                    Charcode[i] = Convert.ToChar(this.getMNum().Substring(i - 1, 1));
                }
                for (int j = 1; j < intNumber.Length; j++)//把字符的ASCII值存入一个整数组中。
                {
                    intNumber[j] = intCode[Convert.ToInt32(Charcode[j])] + Convert.ToInt32(Charcode[j]);
                }
                string strAsciiName = "";//用于存储注册码
                for (int j = 1; j < intNumber.Length; j++)
                {
                    if (intNumber[j] >= 48 && intNumber[j] <= 57)//判断字符ASCII值是否0-9之间
                    {
                        strAsciiName += Convert.ToChar(intNumber[j]).ToString();
                    }
                    else if (intNumber[j] >= 65 && intNumber[j] <= 90)//判断字符ASCII值是否A-Z之间
                    {
                        strAsciiName += Convert.ToChar(intNumber[j]).ToString();
                    }
                    else if (intNumber[j] >= 97 && intNumber[j] <= 122)//判断字符ASCII值是否a-z之间
                    {
                        strAsciiName += Convert.ToChar(intNumber[j]).ToString();
                    }
                    else//判断字符ASCII值不在以上范围内
                    {
                        if (intNumber[j] > 122)//判断字符ASCII值是否大于z
                        {
                            strAsciiName += Convert.ToChar(intNumber[j] - 10).ToString();
                        }
                        else
                        {
                            strAsciiName += Convert.ToChar(intNumber[j] - 9).ToString();
                        }
                    }
                }
                return strAsciiName;
            }
        }
    }
    主要是 “生成注册码” 这部分效率低

    2015年4月15日 7:48

答案

  • 你好:

    你可以通过Stopwatch累来监视到底是哪个地方耗时比较长,经过我测试我发现下面这行代码差不多耗时1秒,

    Charcode[i] = Convert.ToChar(this.getMNum().Substring(i - 1, 1));

    而这行代码执行的是getMNum方法,getMNum方法执行的又是getCPU和GetDiskVolumeSerialNumber方法,

    string strNum = getCpu() + GetDiskVolumeSerialNumber();//获得24位Cpu和硬盘序列号

    最终测试结果是getCpu方法中的这几行代码是最耗时的代码:

    ManagementObjectCollection myCpuConnection = myCpu.GetInstances();
                foreach (ManagementObject myObject in myCpuConnection)
                {
                    strCpu = myObject.Properties["Processorid"].Value.ToString();
                    break;
                }

    所以优化这几行代码就可以了。我尝试了一下使用WMI查询语句来获取CPU的ID,效率比之前要快很多,参考下面代码(其实可以进一步优化,比如把变量提取出来重复利用):

    //获得CPU的序列号
            public string getCpu()
            {
                string sProcessorID = "";
                string sQuery = "SELECT ProcessorId FROM Win32_Processor";
                ManagementObjectSearcher oManagementObjectSearcher = new ManagementObjectSearcher(sQuery);
                ManagementObjectCollection oCollection = oManagementObjectSearcher.Get();
                foreach (ManagementObject oManagementObject in oCollection)
                {
                    sProcessorID = (string)oManagementObject["ProcessorId"];
                }
    
                return (sProcessorID);
            }



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    2015年4月16日 10:00
    版主