本篇通過(guò)一小段代碼的console程序來(lái)進(jìn)行性能的分析以及改進(jìn)、直到后面的改進(jìn)前、改進(jìn)后性能比較結(jié)果。
先看console代碼(源代碼下載):
代碼如下:
static void Main(string[] args)
{
int i = 10000;
while(i-->0)
{
Core c=new Core();
c.Process(DateTime.Now.ToString());
}
}
public class Core
{
public void Process(string input)
{
//process logic
string result = string.Format("{0}-{1}", DateTime.Now, input);
//log to file
Log(result);
}
public void Log(string message)
{
string fileName = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log.txt");
string msg = "{Now}: {Message}";
msg = msg.Replace("{Now}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
msg = msg.Replace("{Message}", message);
using (StreamWriter sw = System.IO.File.AppendText(fileName))
{
sw.WriteLine(msg);
sw.Flush();
sw.Close();
}
}
}

點(diǎn)擊“完成”按鈕,此時(shí)會(huì)自動(dòng)進(jìn)行分析,直接跑完控制臺(tái)程序,vs2012會(huì)自動(dòng)顯示出分析報(bào)告,如下:
新聞熱點(diǎn)
疑難解答
圖片精選