更快版本的Convert.ChangeType [英] Faster version of Convert.ChangeType

查看:88
本文介绍了更快版本的Convert.ChangeType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我拥有的应用程序中,我经常对 Convert.ChangeType 进行调用,以将值转换为动态加载的类型.

In an application that I have, I am doing quite frequent calls to Convert.ChangeType in order to convert a value to a dynamically loaded type.

但是,在使用ANTS进行性能分析后,我发现此 Convert.ChangeType 似乎要花费大量时间(由于调用频率很高).有谁能比这更快的选择吗?

However, after profiling with ANTS, I've found that this Convert.ChangeType seems to take a significant portion of time (due to being called quite often). Does anyone have a faster alternative to doing this?

这时,我有一个包含目标的类型对象和一个包含值的 string .

At this point I have a type object containing the target, and a string containing the value.

以下是有问题的代码.我当时正在考虑对类型进行切换声明(因为它是类型的有限集合)并调用解析方法,尽管我不确定这样做是否会更快.

The following is the offending code. I was considering doing a switch-statement on type (since it is a limited collection of types) and calling the parse methods, though I'm not sure whether or not that'll be faster.

if(attributeRow["Value"]!=DBNull.Value)
    sample[attr] = attr.AttributeType == typeof(Guid)
                 ? new Guid(attributeRow["Value"].ToString())
                 : (IComparable)Convert.ChangeType(attributeRow["Value"],attr.AttributeType);

推荐答案

除了[code> Convert.ChangeType 函数(而且是显式的)之外,我不知道框架内用于更改类型的任何其他功能显然).

I'm not aware of any other functionality within the framework itself for changing Types other than the Convert.ChangeType function (and explicit casts, obviously).

为此,我认为改善此问题的另一种方法是使用自己的 ChangeType 函数,该函数专门针对您的特定情况(如果可能)进行了优化.

For this, I think the only other way to improve this is to roll your own ChangeType function that is specifically optimized for your particular situation (if possible).

您提到您正在使用数量有限的类型,也许您处理一种类型比处理其他类型更多?如此,您的 ChangeType 函数可以进行优化,以首先尝试执行此特定转换,如果失败则仅尝试其他转换.您提到尝试使用开关样式的代码块,并且可以将相同的方法(首先尝试使用最常用的Type).至于是否更快,将取决于您正在处理的数据(以及您要转换成的类型/从中转换出的类型的频率/可变性),而唯一有效的衡量方法是尝试对其进行分析并在其中进行分析.与 Convert.ChangeType 方法进行比较.

You mention that you're working with a limited number of Types, perhaps you're dealing with one Type more than the others? Is so, your ChangeType function could be optimized to attempt this specific conversion first, and only trying others if failing. You mention trying a switch-style block of code, and this same approach (trying the most frequently used Type first) could be applied to that. As to whether it will be faster would depend upon your data that you're processing (and the frequency/variability of the Types you're converting to/from) and the only real way to measure this is to try it and profile it in comparison with the Convert.ChangeType methodology.

Peter Johnson的博客上有一个有趣的链接,如果您想拥有自己的功能:

One interesting link if you're looking to roll-your-own functionality is on Peter Johnson's blog:

Convert.ChangeType无法处理空值

请务必同时阅读该帖子的所有评论.

Be sure to read all of the comments to the post also.

这篇关于更快版本的Convert.ChangeType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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