3

I've created a console application where I try 2 different methods of getting values from a sql database (= on the same server). When I add a stopwatch to check which one is the fastest method, I'll sometimes get the first, other times the second. What could be responsible for this? Is it possible to run the test under the same conditions? Should I run this async?

My console-code can be found here: http://pastie.org/7920175

John Saunders
  • 160,644
  • 26
  • 247
  • 397
RubenHerman
  • 1,674
  • 6
  • 23
  • 42
  • 2
    Firstly, the results are meaningless if not under a release build. Secondly if sometimes the first, and sometimes the second, I would hasten to guess that they take very similar amounts of time. – Moo-Juice May 17 '13 at 11:42
  • We need the source code of the methods itself. But why do you need to optimize the code? Is it so slow? Then a thread would be better. @Moo-Juice: I fully agree. – jAC May 17 '13 at 11:43
  • There are likely as many or more factors relating to the execution of the SQL by the database server itself (caching, parameter sniffing and so on) that affect performance than on the client side; of course this depends on what the benchmarked methods actually do - which you have not stated and the time differences - which you also have not stated – Alex K. May 17 '13 at 11:44
  • It is pretty unlikely that you are actually measuring the perf of your code. Not much happens on the .NET side when you query a dbase, it all happens on the dbase server. Which is basically what your test results are telling you. The conclusion you need to draw is that it doesn't matter which method you use. – Hans Passant May 17 '13 at 14:27

4 Answers4

3

I recommend to use ANTS Performance Profile

The performance of a method depends on many factors not just the server .

Anyname Donotcare
  • 11,113
  • 66
  • 219
  • 392
1

StopWatch may not be as accurate as you like.

You could use the Visual Studio 2012 Performance Analysis which is really handy (if you're using vs2012) otherwise you may want to install a Profiler Tool such as YourKit

You said the two methods retrieve data from the SQL database. That means you're not measuring only the code that you written but also 3rd party operaions (such as read from Database) which probably behaves different. This may be one of the causes that the two methods return different timing from one run to another

Dan Dinu
  • 32,492
  • 24
  • 78
  • 114
1

I think you can use this nice tool to check your methods performance.

SpeedTester myTest = new SpeedTester([method to test]);
myTest.RunTest();
Patartics Milán
  • 4,858
  • 4
  • 26
  • 32
0

How often are the methods you're testing called? Run them a thousand times each and see which one's faster over the long haul.

Also, if all your methods are doing is wrapping up some SQL you might not see much of a difference after the first query on the same data anyway. You might consider clearing the query cache before each try.

Community
  • 1
  • 1
Dave
  • 900
  • 6
  • 22