Git Product home page Git Product logo

benchmarkit's Introduction

Benchmark.It

Simple easy .NET benchmarking for little bits of code. When you just really want to see if one method is actually faster than another.

Install

Run the following command in the Package Manager Console (NuGet).

PM> Install-Package Benchmark.It

Or clone and include BenchmarkIt.csproj directly

Use

Lets say you wanted to see if string.Contains was faster or slower than string.IndexOf. Simply write the following and have it printed out nicely for you to see.

Benchmark.This("string.Contains", () => "abcdef".Contains("ef"))
    .Against.This("string.IndexOf", () => "abcdef".IndexOf("ef"))
    .For(5)
    .Seconds().PrintComparison();
Name    Iterations      Percent
Name                                    Iterations          Percent                       
string.Contains                         23117812            100%                          
string.IndexOf                          10852501            46.9%

Or you wanted to see if a foreach loop was actually faster than a for loop (it is).

int[] values = Enumerable.Range(1, 100000).ToArray();
Benchmark.This("for.Count", () =>
{
    for (int i = 0; i < values.Count(); i++)
    {
        int x = values[i];
    }
})
.Against.This("for.Length", () =>
{
    for (int i = 0; i < values.Length; i++) {
        int x = values[i];
    }
})
.Against.This("foreach", () =>
{
    foreach (int x in values);
})
.For(10000)
.Iterations()
.PrintComparison();
Name                                    Milliseconds        Percent                       
for.Count                               46116               9581%                        
for.Length                              621                 129%                          
foreach                                 481                 100%

And why stop there, you can add as many different methods as you want.

Benchmark.This("empty 1", () => { })
 .Against.This("empty 2", () => { })
 .Against.This("empty 3", () => { })
 .Against.This("empty 4", () => { })
 .Against.This("empty 5", () => { })
 .Against.This("empty 6", () => { })
 .For(1).Minutes().PrintComparison();

You can also just benchmark one method, and on top of that you can specify a number of warmpup loops to perform first.

Benchmark.This("string.Contains", () => "abcdef".Contains("ef"))
    .WithWarmup(1000)
    .For(5).Seconds()
    .PrintComparison();

TODO

  • Improve result print, clean it up etc.
  • Investigate manually unrolling the benchmark loops a bit
  • Speed up time loop
  • Generate graphs?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.