Click here to Skip to main content
13,904,127 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


12 bookmarked
Posted 14 Apr 2011
Licenced CPOL

Code Contract Performance Analysis

, 15 Apr 2011
Rate this:
Please Sign up or sign in to vote.
A look at the runtime cost of using code contracts


I found the article about code contracts in this month's MSDN very exciting. I was unaware of this feature of .NET 4.0 before reading the article, and as the manager of a team of developers who maintain a very large and complex application, I am always interested in techniques that improve code quality and correctness. I won't rehash the interesting details about code contracts in this article. Go read the MSDN article first or this CodeProject introduction. The basics are all there. Then go here to the Microsoft DevLabs page with the download you'll need to run the code below.

My first thought when reading the MSDN article was that the benefits must come at some price, and my initial concern was an impact on performance. Contracts are enforced at run-time by inserting custom code at compile time, and whenever some other process is adding code to mine, I worry about hidden performance costs.

I wrote the small app below to get some metrics and assess how expensive contracts are compared to the other techniques that can be used to validate pre and post execution conditions.

Using the Code

The author of the MSDN article used a trivial calculator function to highlight the benefits of contracts. I'll use the same basic function here:

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

   return x + y;

The extra if statement in there is just to reinforce the difficulty of adding post-condition checking everywhere in your code where you have premature exits. I'm going to leave it here for the analysis.


One way to check pre and post conditions is to use explicit if statements to validate your input parameters and output results. All of the pre and post conditions we're adding here are perfectly arbitrary, but will naturally be consistent in all the examples.

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

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

      return x * 2;

   if (x + y < 0) // Same arbitrary post-condition
      throw new InvalidOperationException("Result must be positive");
   return x + y;


Another way is using Debug.Assert():

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;


And the last method to examine is the interesting new one, code contracts:

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

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

   return x + y;

One of the benefits to contracts being that you don't have to worry about where you exit. All of your post checking conditions are centralized at the top of the method.

Running the Different Methods

I timed how long it took to execute each of the methods above 100,000,000 times.

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());

The Setup

I ran the application 5 times to get average times and a feel for the variability of the results. The tests were executed on Windows 7 (32-bit), on a dual core (Intel Core 2 E8400 3.0GHz) CPU with 4Gb of RAM.

I used version 1.4.40314.1 of the code contracts SDK with pre and post contract checking enabled.

I'm not interested in the performance of the different methods of execution when the pre or post conditions are not met, just the overhead of the different validation frameworks.

The Results


Run 1 (ms)1934.42199.62230.83213.6
Run 21950.02184.02215.23244.8
Run 31950.02199.62246.43260.4
Run 41950.02246.42293.23369.6
Run 51934.42184.02246.43244.8
Std Dev8.5425.6329.1860.01
Table 1. Results from running code in Visual Studio 2008 built for debug.
  • The "naked" calculator method took on average 1940.6ms to execute.
  • The method with the if-then-throw pre and post condition checking took 2202.7ms to execute (13.5% slower than the unchecked benchmark Add() method).
  • The method that used Debug.Assert() pre and post condition checking took 2246.4ms, (1.98% slower than if-then-throw and 15.76% slower than the unchecked benchmark Add() method).
  • The method that used contracts took on average 3266.6ms and was 45.42% slower than Debug.Assert() and 68.33% slower than the "naked" calculator method.

When the switch to enable contract code insertion is set to false, the ContractCheckedAdd() method took exactly the same amount of time as the unchecked Add() method, as no code was inserted at compile time. You can verify that by looking at the IL of ContractCheckedAdd() in an assembly built with contracts enabled and contracts disabled. ContractCheckedAdd() looks the same as Add() when contracts are not enabled.


Run 1 (ms)467.8545.8467.8686.2
Run 2467.9545.9467.9701.9
Run 3467.9530.3467.9701.9
Run 4467.9545.9467.9701.9
Run 5467.9545.9467.9686.3
Std Dev07.008.6
Table 2. Results from running code outside of Visual Studio 2008 built for release.


I hope to avoid maintenance headaches on future projects and would be happy to sacrifice some performance for some assurances about correctness, so I expect to use code contracts in a lot of my future development. I thought it would be good to know some of the costs to weigh against the benefits.

Share and enjoy.


  • April 13 2011 - Initial revision
  • April 15 2011 - Added results from running build for Release


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


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...


Comments and Discussions

GeneralMessage Closed Pin
11-Dec-17 22:17
memberthecreative sciences11-Dec-17 22:17 
GeneralMy vote of 3 Pin
GlobX18-Apr-11 19:52
memberGlobX18-Apr-11 19:52 
GeneralMy Vote of 5 Pin
ScruffyDuck16-Apr-11 22:39
memberScruffyDuck16-Apr-11 22:39 
GeneralMy vote of 5 Pin
R. Hoffmann15-Apr-11 9:50
professionalR. Hoffmann15-Apr-11 9:50 
GeneralRequires&lt;T&gt; Pin
kornman0015-Apr-11 7:47
memberkornman0015-Apr-11 7:47 
GeneralRe: Requires<T> Pin
Sean Michael Murphy17-Apr-11 18:24
memberSean Michael Murphy17-Apr-11 18:24 
GeneralOther posters had great responses Pin
Jeremy Hutchinson15-Apr-11 3:31
professionalJeremy Hutchinson15-Apr-11 3:31 
GeneralRe: Other posters had great responses Pin
Sean Michael Murphy15-Apr-11 6:58
memberSean Michael Murphy15-Apr-11 6:58 
GeneralRe: Other posters had great responses Pin
Sean Michael Murphy15-Apr-11 15:18
memberSean Michael Murphy15-Apr-11 15:18 
GeneralRe: Other posters had great responses Pin
Jeremy Hutchinson18-Apr-11 2:29
professionalJeremy Hutchinson18-Apr-11 2:29 
GeneralResults fail Pin
Xetrill15-Apr-11 0:48
memberXetrill15-Apr-11 0:48 
GeneralRe: Results fail Pin
Sean Michael Murphy15-Apr-11 3:15
memberSean Michael Murphy15-Apr-11 3:15 
QuestionWould it improve in release mode? Pin
Werner van Deventer14-Apr-11 19:20
memberWerner van Deventer14-Apr-11 19:20 
AnswerRe: Would it improve in release mode? Pin
Sean Michael Murphy15-Apr-11 3:17
memberSean Michael Murphy15-Apr-11 3:17 
GeneralDifferent Results Pin
Justin Helsley14-Apr-11 18:33
memberJustin Helsley14-Apr-11 18:33 
GeneralRe: Different Results Pin
Sean Michael Murphy15-Apr-11 3:23
memberSean Michael Murphy15-Apr-11 3:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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