最新消息:ww12345678 的部落格重装上线,希望大家继续支持。

System.Diagnostics.StopWatch

网络文摘 Kenny Saelen 1995浏览

I often see code that still uses the WinApi::GetTickCount method to measure an interval of time. And though there is nothing wrong with it, I wanted to show an alternative method : the StopWatch already known in .Net for quite some time.
The sample below show the usage of the stopwatch while I was testing something out with looping all of the tables in the system:

static void FetchTableNames_ModelElementTables(Args _args)
{
    SysModelElement element;
    SysModelElementType elementType;
    System.Collections.ArrayList tableNames = new System.Collections.ArrayList();
    System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
    int64 elapsed;
    ;
 
    stopWatch.Start();
 
    // The SysModelElementType table contains the element types and we need the recId for the next selection
    select firstonly RecId
    from elementType
    where elementType.Name == 'Table';
 
    // With the recId of the table element type, select all of the elements with that type (hence, select all of the tables)
    while select Name
    from element
    where element.ElementType == elementType.RecId
    {
        tableNames.Add(element.Name);
    }
 
    stopWatch.Stop();
 
    // Get the time it took
    elapsed = stopWatch.get_ElapsedMilliseconds();
 
    info(strFmt("Time taken: %1", elapsed));
}