friendsterTalk - Friendster Forum

friendsterTalk - Friendster Forum

Welcome guest! Please Login or Register.

#1  2008-11-11 17:58:45

creamownedz
 active server protocol
creamownedz's display avatar
» FriendsterTalker
PsychoCoder
Class-S
Location: Ftalk & MCTF
Registered: 2008-03-22
Posts: 115
Last visit: Yesterday
Reputation: 7

Measure task execution time [C#]

This is a snippet used to measure the execution time of a task or process. It uses the System.Diagnostics.StopWatch to measure the time


Instructions: Pass the method the number of times to execute the task or process, results are returned in a HashTable format showing:

1) Total elapsed time
2) Milliseconds elapsed
3) Timer ticks elapsed


Code:

//Namespace reference
using System;
using System.Diagnostics;

/// <summary>
/// method to measure the execution time of a 
/// task or process.
/// </summary>
/// <param name="num">Number of times to execute the task</param>
/// <returns></returns>
public Hashtable MeasureExecutionTime(int num)
{
    //HashTable instance to hold execution times
    Hashtable time = new Hashtable();
    Stopwatch timer = new Stopwatch();
    try
    {
        //start your StopWatch
        timer.Start();
        for (int i = 1; i < num; i++) 
        { 
            //execute your process or task to be times
        }
        //stop the StopWatch once the processing is complete
        timer.Stop();
        //Add the values to our HashTable
        time.Add("Elapsed: ", timer.Elapsed);
        time.Add("In milliseconds: ", timer.ElapsedMilliseconds);
        time.Add("In timer ticks: ", timer.ElapsedTicks);
    }
    catch (Exception ex)
    {
        time = null;
        MessageBox.Show(ex.Message);
    }
    return time;
}

...Giving of reputation are highly appreciating for me...
Computers don't make errors what they do they do on purpose
...computer is my expertise...
....an addicted coder....
....don't ripp all my tutorials....

Search Friendstertalk

Board footer

FriendsterTalk is not affiliated with Friendster.com
Copyright © 2002–2009 PunBB

[ 8 queries - 0.026 second ]

Pay Per Click Ads by pay per click advertising by Kontera

FriendsterTalk.com x

Welcome to FriendsterTalk! You'll need to login in order to fully use all the features and view all the sections of this site.

Please register if you're not yet a member. =)