Click here to Skip to main content
12,634,422 members (25,937 online)
Click here to Skip to main content

Tagged as

Stats

24.8K views
173 downloads
12 bookmarked
Posted

Code Contract Performance Analysis

, 15 Apr 2011 CPOL
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)

Share

About the Author

Sean Michael Murphy
Product Manager
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.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161208.2 | Last Updated 15 Apr 2011
Article Copyright 2011 by Sean Michael Murphy
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid