簡體   English   中英

如何在C#中查找編碼執行時間?

[英]how to find the coding execution time in c#?

我正在使用C#,winform和Mysql。 (Visual Studio 2010)。

在我的項目中,所有菜單項都不可見。 在登錄時,其可見的菜單項用戶權限。 為此,我看不到所有菜單欄。 我編寫代碼以顯示菜單欄項目。

但是,這需要太多時間。 再過5分鍾。 所以我有幾個問題。

  1. 我如何找到此編碼執行時間? (下面的我的代碼)....
  2. 有什么特殊工具嗎?
  3. 如何減少有效代碼的時間?
  private void menuActive(ToolStripItemCollection items) { hp.getConnStr(); MySqlConnection connection = new MySqlConnection(hp.myConnStr); MySqlCommand command = connection.CreateCommand(); MySqlDataReader Reader; command.CommandText = "select menu_key from mcs_menu_rights where userid ='"+userId+"'"; connection.Open(); Reader = command.ExecuteReader(); while (Reader.Read()) { foreach (ToolStripMenuItem item in items) { if (item.Name == Reader[0].ToString()) { item.Visible = true; } else { menuActive(item.DropDown.Items); } } } connection.Close(); } 

您可以進行配置,但是我可以直接看到一些明顯的改進:

將回溯功能與數據庫查詢分開。 這樣,您一次查​​詢數據庫。 請注意,它的功能並不完全相同,但是我不確定該表的外觀,因此您必須對其進行測試。

private void menuActive(ToolStripItemCollection items) 
    { 
        hp.getConnStr(); 
        MySqlConnection connection = new MySqlConnection(hp.myConnStr); 
        MySqlCommand command = connection.CreateCommand(); 
        MySqlDataReader Reader; 
        command.CommandText = "select menu_key from mcs_menu_rights where userid ='"+userId+"'"; 
        connection.Open(); 
        Reader = command.ExecuteReader(); 
        while (Reader.Read()) 
        {
            var nameFromDB = Reader[0].ToString();
            setMenuActiveByName(items, nameFromDB);
        }
        connection.Close(); 
    }

    //This is the recursive bit, but doesn't re-enquire the database
    private void setMenuActiveByName(ToolStripItemCollection items, string name) 
    {
            foreach (ToolStripMenuItem item in items) 
            { 
                if (item.Name == name) 
                { 
                    item.Visible = true; 
                } 
                else 
                {
                    setMenuActiveByName(item.DropDown.Items, name); 
                } 
            }
    } 

是的,為每個菜單項創建一個dbase連接將花費一些時間。 您需要避免在foreach循環內調用MenuActive()。 使用一些輔助方法使其更聰明:

    private static bool EnableMenuItem(ToolStripItemCollection items, string name) {
        foreach (ToolStripMenuItem item in items) {
            if (item.Name == name) {
                item.Visible = true;
                return true;
            }
            else if (item.DropDown.Items.Count > 0 {
                if (EnableMenuItem(item.DropDown.Items, name)) return true;
            }
        }
        return false;
    }

並這樣稱呼它:

        ...
        while (Reader.Read()) 
        { 
            EnableMenuItem(items, Reader[0].ToString();
        }

問題1和2

var time = TimeAction(() =>
    {
        //CODE here
    });

private static TimeSpan TimeAction(Action action)
{
    var sw = new Stopwatch();
    sw.Start();
    action.Invoke();
    sw.Stop();
    return sw.Elapsed;
}

問題3

//One loop through all records in database
while (Reader.Read())
{
    //Another loop through all the control
    foreach (ToolStripMenuItem item in items)
    {
        if (item.Name == Reader[0].ToString())
        {
            item.Visible = true;
        }
        else
        {
            menuActive(item.DropDown.Items);
        }
    }
}

假設您在數據庫中有1000條記錄和10個項目,則這需要10000次迭代,每個迭代都包括訪問數據庫以提供新數據的過程。

使用StopWatch類來計時執行時間, http://msdn.microsoft.com/zh-cn/library/system.diagnostics.stopwatch.aspx 在每個步驟之后記錄時間,以確定花費了這么長時間。

您可以使用StopWatch類來測試性能

StopWatch sWatch = new StopWatch();
sWatch.Start();
// Code comes here
...
sWatch.Stop();
// sWatch.Elapsed // Contains the time interval

似乎那里的遞歸不好-您一次又一次在同一個集合上運行該函數。

根據您的VS版本,您也許可以使用內置的性能分析工具來查找時間。

對於問題1和2,可以使用秒表

Stopwatch watch = new Stopwatch();

watch.Start();
//YOUR CODE HERE
watch.Stop();

Console.WriteLine("Elapsed: {0}",watch.Elapsed);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM