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

Code Contract Performance Analysis

Rate me:
Please Sign up or sign in to vote.
4.69/5 (12 votes)
15 Apr 2011CPOL4 min read 45.3K   12  
A look at the runtime cost of using code contracts
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ContractBenchmarker {
    class Program {
        private delegate Int32 testMethod(Int32 x, Int32 y);

        static void Main(string[] args) {
            const Int32 ITERATIONS = 10000;

            foreach (testMethod tm in new testMethod[] { new testMethod(Add), 
                                                         new testMethod(IfCheckedAdd), 
                                                         new testMethod(AssertCheckedAdd),
                                                         new testMethod(ContractCheckedAdd) }) {
                DateTime start = DateTime.Now;

                for (Int32 i = 0; i < ITERATIONS; i++)
                    for (Int32 j = 0; j < ITERATIONS; j++)
                        tm(i, j);

                System.Console.WriteLine(tm.Method.Name + " " + (DateTime.Now - start).TotalMilliseconds.ToString());
            }

            System.Console.ReadLine();
        }

        private static Int32 Add(Int32 x, Int32 y) {
            if (x == y)
                return x * 2;

            return x + y;
        }

        private static Int32 IfCheckedAdd(Int32 x, Int32 y) {
            if (x < 0) throw new InvalidOperationException("X must be greater than 0");

            if (x == y) {
                if (x * 2 < 0) throw new InvalidOperationException("Result must be positive");

                return x * 2;
            }

            if (x + y < 0) throw new InvalidOperationException("Result must be positive");
            return x + y;
        }

        private static Int32 AssertCheckedAdd(Int32 x, Int32 y) {
            System.Diagnostics.Debug.Assert(x >= 0);

            if (x == y) {
                System.Diagnostics.Debug.Assert(x * 2 >= 0);

                return x * 2;
            }

            System.Diagnostics.Debug.Assert(x + y >= 0);
            return x + y;
        }

        private static Int32 ContractCheckedAdd(Int32 x, Int32 y) {
            System.Diagnostics.Contracts.Contract.Requires(x >= 0, "X must be greater than 0");
            System.Diagnostics.Contracts.Contract.Ensures(System.Diagnostics.Contracts.Contract.Result<Int32>() >= 0, "Result must be positive");

            if (x == y)
                return x * 2;

            return x + y;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Technical Lead
Canada Canada
I'm a graduate of the University of Toronto with a degree in zoology. I'm currently a software development manager with a large Canadian financial institution, and a passionate squash player.

I am a proud daddy to Alex and Sarah.

Comments and Discussions