Click here to Skip to main content
15,883,883 members
Articles / Programming Languages / C#

Benchmark Code Blocks Easy

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
20 Aug 2012CPOL 7K   5  
Benchmark code blocks easy

Have you been testing code speed like this?

C#
var start = DateTime.Now;
int i;
for(i = 0; i < 1000; i++)
{
    DoSomething();
}
var stop = DateTime.Now;
var total = stop - start;
var timePerIteration = total.Ticks / i;

Or maybe you’ve found the Stopwatch class and been happy with its superior time precision?

Better, I’d say, but I’ve quite had it. Plus I needed to benchmark a local website and needed to test parallel requests (something similar to ab – ApacheBench).

What does a programmer do in such a case? Write his own tools! :P That’s how BenchmarkNET appeared. Using BenchmarkNET, you can write the same thing as the above, only much shorter and with a better timing precision:

C#
var result = Benchmark.Sequentially(() => DoSomething(), 1000);

Neat? That’s not all. The result can easily printed out to a console or inspected with a debugger:

Image 2

Benchmarking an HTTP operation over 10 parallel threads, 100 times for each thread?

C#
var dl2 = Benchmark.Parallel(new BenchmarkParams<WebClient>
(c => c.DownloadString("http://localhost/"), 100), 10, () => new WebClient());

The project has been published as open source (LGPL license) on CodePlex. You can discuss it, file bugs, or even contribute to it.

Have fun and if you test it out, please leave some feedback!

Later edit: It seems some people already dig it. ;)

Image 4
Image 5

Image 6

This article was originally posted at http://blog.andrei.rinea.ro?p=79

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) IBM, Business Analytics
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --