timing functions

M

mp

I was thinking of looking at how long certain functions took in a program to
see if there were areas to optimize.
so i considered creating two objects
FunctionTimer, and ProgramTimer
then in each sub/function in the program do something like...
private void SomeFunction()
{
FunctionTimer ft = New FunctionTimer("SomeFunction");
.....code...
ft.TimeOut();

//then somehow add it to a list of all the function times for this program
run
GlobalProgramTimer.AddFunctionTimer(ft);

}
my thoughts were FunctionTimer could use a stopwatch to time the process and
store the name of function and time of running
then the programTimer could store/increment the various functions' times in
maybe a sortedDictionary or somesuch???
any thoughts/comments on either the idea or the implementation...
or is there something built in to c# that already does this?
tanks
mark
 
M

mp

Peter Duniho said:
I was thinking of looking at how long certain functions took in a program
to
see if there were areas to optimize.
[...]
my thoughts were FunctionTimer could use a stopwatch to time the process
and
store the name of function and time of running
then the programTimer could store/increment the various functions' times
in
maybe a sortedDictionary or somesuch???
any thoughts/comments on either the idea or the implementation...
or is there something built in to c# that already does this?
[...]
IMHO, for simple quick-and-dirty profiling, it suffices to instrument a
single method at a time, using a plain Stopwatch to determine which
section of code within that method (often just a single statement calling
another method) is taking the most time. Plain Debug.WriteLine()
statements can be used to emit the information as the program executes.
[...]
Pete

but if for the sake of learning, i want to encapsulate the stopwatch in an
object that stores the name of function and cumulative times...
what kind of pattern would be recommended? I'm wondering how to minimally
couple the timing class with the actual 'worker' class which is doing some
work and whose methods want to be timed.
my current setup has a form(wpf) as the 'main' program, and it uses a
'worker' class to do something.
so the main program (form) may want to time some of it's own methods
(filling listboxes or any other time consuming operation) as well as timing
some internal methods of the 'worker' class.
my first thought is to create an overview timer object (which stores
multiple functions/times) in the main(form) object.
then either i have to

A. pass that object as an argument into any methods (either in the form or
the 'worker' class) which i want to time
or
B. somehow make the scope of that object so the internal methods of the
'worker' class can see the form's timer object
or
C. give the 'worker' class a property of a timer object reference and set it
in the class constructor or a property set
or
D. ?

any thoughts?
thanks
mark
 
M

mp

Peter Duniho said:
but if for the sake of learning, i want to encapsulate the stopwatch in
an
object that stores the name of function and cumulative times...
[... ]
As for how to go about option B, I would think that a static class would
work fine. You can just pass strings to the timing methods (basically,
start and stop) that uniquely identify the method(s) you're timing. The
string then could be used in a dictionary to retrieve and maintain the
timing data for that method.

ah! static must be what i was missing...i had tried something very close to
what you show, except I wasn't getting the Profile class to be visible to
the "MyMethod" class.
I'll try adding static and see if that works.
[... ]
Or something like that. Note: the above code is completely uncompiled,
untested, etc. It's just for illustration purposes; you'll need to
elaborate quite a bit, at the very least adding features to report the
statistics that were gathered, and perhaps to do some minimal error
checking, like making sure the Stop() method isn't called if the Start()
method hasn't been called first.

Pete

thanks much, appreciate the pointers.
mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top