views:

166

answers:

8

I'd like to gather metrics on specific routines of my code to see where I can best optimize. Let's take a simple example and say that I have a "Class" database with multiple "Students." Let's say the current code calls the database for every student instead of grabbing them all at once in a batch. I'd like to see how long each trip to the database takes for every student row.

This is in C#, but I think it applies everywhere. Usually when I get curious as to a specific routine's performance, I'll create a DateTime object before it runs, run the routine, and then create another DateTime object after the call and take the milliseconds difference between the two to see how long it runs. Usually I just output this in the page's trace...so it's a bit lo-fi. Any best practices for this? I thought about being able to put the web app into some "diagnostic" mode and doing verbose logging/event log writing with whatever I'm after, but I wanted to see if the stackoverflow hive mind has a better idea.

A: 

I use this method and I think it's very accurate.

Kirill Titov
+2  A: 

I use this method on occasion and find it to be fairly accurate. The problem is that in large applications with a fairly hefty amount of debugging logs, it can be a pain to search through the logs for this information. So I use external tools (I program in Java primarily, and use JProbe) which allow me to see average and total times for my methods, how much time is spent exclusively by a particular method (as opposed to the cumulative time spent by the method and any method it calls), as well as memory and resource allocations.

These tools can assist you in measuring the performance of entire applications, and if you are doing a significant amount of development in an area where performance is important, you may want to research the tools available and learn how to use one.

Elie
When I develop a program in Eclipse I just print the time (in ms) to console before and after execution the needed function.
Kirill Titov
Which I do as well when I'm testing limited portions of the code. But it doesn't tell you how the time is being spent. If the function calls other functions, it can be useful to know which of those calls is the expensive one.
Elie
+3  A: 

For database queries, you have a two small problems. Cache: data cache and statement cache.

If you run the query once, the statement is parsed, prepared, bound and executed. Data is fetched from files into cache.

When you execute the query a second time, the cache is used, and performance is often much, much better.

Which is the "real" performance number? First one or second one? Some folks say "worst case" is the real number, and we have to optimize that. Others say "typical case" and run the query twice, ignoring the first one. Others says "average" and run in 30 times, averaging them all. Other say "typical average", run the 31 times and average the last 30.

I suggest that the "last 30 of 31" is the most meaningful DB performance number. Don't sweat the things you can't control (parse, prepare, bind) times. Sweat the stuff you can control -- data structures, I/O loading, indexes, etc.

S.Lott
+1  A: 

There are some Profilers available but, frankly, I think your approach is better. The profiler approach is overkill. Maybe the use of profilers is worth the trouble if you absolutely have no clue where the bottleneck is. I would rather spend a little time analyzing the problem up front and putting a few strategic print statements than figure out how to instrument your app for profiling then pour over gargantuan reports where every executable line of code is timed.

Glenn
Profilers don't have to output gargantuan reports. At least one I know of produces an excellent sorted graph of your most time-consuming methods. Analysis easy - look at the top method on the list...
Bill Michell
If only it was that easy. Then you have to see what that method calls, and do on.
Glenn
+1  A: 

If you're working with .NET, then I'd recommend checking out the Stopwatch class. The times you get back from that are going to be much more accurate than an equivalent sample using DateTime.

I'd also recommend checking out ANTS Profiler for scenarios in which performance is exceptionally important.

Gabriel Isenberg
A: 

I think you have a good approach. I recommend that you produce "machine friendly" records in the log file(s) so that you can parse them more easily. Something like CSV or other-delimited records that are consistently structured.

Patrick Cuff
+1  A: 

It is worth considering investing in a good commercial profiler, particularly if you ever expect to have to do this a second time.

The one I use, JProfiler, works in the Java world and can attach to an already-running application, so no special instrumentation is required (at least with the more recent JVMs).

It very rapidly builds a sorted list of hotspots in your code, showing which methods your code is spending most of its time inside. It filters pretty intelligently by default, and allows you to tune the filtering further if required, meaning that you can ignore the detail of third party libraries, while picking out those of your methods which are taking all the time.

In addition, you get lots of other useful reports on what your code is doing. It paid for the cost of the licence in the time I saved the first time I used it; I didn't have to add in lots of logging statements and construct a mechanism to anayse the output: the developers of the profiler had already done all of that for me.

I'm not associated with ej-technologies in any way other than being a very happy customer.

Bill Michell
+1  A: 

Some times approach you take will give you a best look at you application performance. One things I can recommend is to use System.Diagnostics.Stopwatch instead of DateTime , DateTime is accurate only up to 16 ms where Stopwatch is accurate up to the cpu tick.

But I recommend to complement it with custom performance counters for production and running the app under profiler during development.

MichaelT