在編寫完成一個程序後,你們都比較關心程序的性能如何,想把程序優化得更好。不少時候憑我的直覺來優化程序是件很是不靠普的事情,即便你是一個優秀的開人員也很難準確地判斷程序中那些出現問題。VS2010提供了性能分析工具就能輕鬆地幫咱們解決這一事情。web
class Program
{
static List<Expression> mExpressions = new List<Expression>();
static Random mRan = new Random();
static void Main(string[] args)
{
try
{
string dbpath = @"Data Source=d:\\northwind.db;Pooling=true;FailIfMissing=false;";
DBContext.SetConnectionDriver<SqliteDriver>(ConnectionType.Context1);
DBContext.SetConnectionString(ConnectionType.Context1, dbpath);
mExpressions.Add(Order.shipCountry == "Switzerland");
mExpressions.Add(Order.shipRegion == "RJ");
mExpressions.Add(Order.customerID.In(Customer.customerID, Customer.country == "UK"));
mExpressions.Add(Order.customerID.In(Customer.customerID, Customer.country == "Germany"));
mExpressions.Add(Order.orderDate > "1997-8-5");
mExpressions.Add(Order.orderDate < "1997-12-1");
mExpressions.Add(Order.orderDate > "1997-5-1" & Order.orderDate<"1997-11-5");
System.Threading.Thread thread;
for (int i = 0; i < 10; i++)
{
thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Test
));
thread.Start();
}
}
catch (Exception e_)
{
Console.WriteLine(e_.Message);
}
}
static void Test(object obj)
{
while (true)
{
Expression exp = mExpressions[mRan.Next(mExpressions.Count - 1)];
Console.WriteLine(exp.Count<Order>());
System.Threading.Thread.Sleep(mRan.Next(50, 5000));
}
}
}
到了這裏發現原來是connection.Open方法佔用了大部分資源,這個時候就想到這個測試程序跑這麼久爲何鏈接打開這麼損耗資源,是否是鏈接池沒有開啓致使每次操做都進行數據庫鏈接操做呢?數據庫
其實VS2010給咱們提供的分析工具真得很輕鬆就可讓咱們瞭解到程序代碼情況,從而優化程序的代碼。若是有這煩腦的朋友不防試下:)dom