代码合同使用导致性能下降 [英] Performance degradation with code contracts usage

查看:63
本文介绍了代码合同使用导致性能下降的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在MSDN杂志中找到了Microsoft提供的示例代码,用于解释.Net Framework 4中的CodeContracts。我发现性能下降了2ms举个简单的例子。这将如何影响大规模应用程序的性能?。

I have profiled the sample code given by Microsoft in the MSDN magazine for the explanation of CodeContracts in .Net Framework 4. I find the performance dip by 2ms for the simple example. How will this affect performance in a large scale application?.

个人档案结果: 

Profile Results: 

没有codecontracts

without codecontracts

 

应用程序/主方法调用/ Sum方法调用时间/除法方法调用时间

Application / Main method call / Sum method call time / divide method call time

 

164ms / 35ms / 0ms / 0ms

164ms / 35ms / 0ms / 0ms

 

with codecontracts

with codecontracts

 

应用程序/主方法调用/ Sum方法调用时间/除法方法调用时间

Application / Main method call / Sum method call time / divide method call time

 

166ms / 37ms / 2ms / 0ms 

166ms / 37ms / 2ms / 0ms 

 

任何回复?...

推荐答案

如果您可以发布文章链接,对我们来说会更容易知道代码是什么样的。 你为读者做的越方便,他们就越有可能花一点时间来回答你的问题。

If you can post a link to the article, it would be easier for us to know what the code looks like.  The more convenient you make it for your readers, the more likely they will take a minute to answer your question.

但无论应用程序做什么,它都是一个典型的演示应用程序,因为它运行在200毫秒以内。我一直在大规模的严肃应用程序中使用代码契约,包括运行超过10小时的大型数据转换应用程序。合同的
成本是"通常"的。与我正在做的相比,微不足道。但在我的上下文中,我正在调用数据库和Web服务。这些操作比简单合同的成本要大得多,例如"Contract.Requires(!String.IsNullOrEmpty(schemaName));"
显示在第一个代码示例中。这类断言非常快。

But whatever the application is doing, it is a typical demo application because it runs in under 200 ms. I use code contracts in large scale serious applications all the time, including a large data conversion application that runs over 10 hours. And the cost of contracts, is "usually" trivial compared to what I am doing. But in my context, I am doing calls to databases and web services. These operations are much more substantial than the cost of simple contracts like "Contract.Requires(!String.IsNullOrEmpty(schemaName));" shown in the first code example. These kind of assertions are extremly quick.


    private static void TryToCreateTable(string schemaName, string tableName)
    {
      Contract.Requires(!String.IsNullOrEmpty(schemaName));
      Contract.Requires(!String.IsNullOrEmpty(tableName));
      if (TableExists(tableName, schemaName))
      {
        Console.WriteLine("Table {0}.{1} already exists.", schemaName, tableName);
      }
      else
      {
        CreateTable(tableName, schemaName);
      }
    }


这篇关于代码合同使用导致性能下降的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆