先来个测试:

        static void Main(string[] args)
{
Stopwatch stopwatch;
string strStr = "string";
object objStr = strStr;
string strTarget;
object objTarget;
int count = int.MaxValue; stopwatch = Stopwatch.StartNew();
for (int i = ; i < count; i++)
strTarget = strStr;
stopwatch.Stop();
Console.WriteLine("string to string: " + stopwatch.Elapsed); stopwatch = Stopwatch.StartNew();
for (int i = ; i < count; i++)
strTarget = (string)objStr;
stopwatch.Stop();
Console.WriteLine("object to string: " + stopwatch.Elapsed); stopwatch = Stopwatch.StartNew();
for (int i = ; i < count; i++)
objTarget = strStr;
stopwatch.Stop();
Console.WriteLine("string to object: " + stopwatch.Elapsed); stopwatch = Stopwatch.StartNew();
for (int i = ; i < count; i++)
objTarget = objStr;
stopwatch.Stop();
Console.WriteLine("object to object: " + stopwatch.Elapsed);
}

结果:

string to string: ::00.8043824
object to string: ::03.9572322
string to object: ::00.8029497
object to object: ::00.8057540

结论:

.向上转(子转父)与直接添加引用一样,基本没什么消耗
.向下转(父转子),由于父不知道子的某些特性,需要新生成,因此会生成新的对象,非常耗时。

参考文章:

Type casting impact over execution performance in C#

Introduction

Explicit and implicit type casting is a common programming topic for almost any imperative programming language. Most C, C++, or Pascal programmers care about efficiency and speed of their code; but those who use managed programming environments, such as Java, Visual Basic, or C# rely all the optimizing tasks on the compiler and the runtime environment.

This can be a good approach in many cases, but managed languages are becoming more and more popular also for high-performance applications where the knowledge of the language, compiler, and runtime environment can enhance a program's quality and speed.

This article analyzes the most common type casting situations and the compiler behavior in them. We are going to study the MSIL generated code, but not the machine-specific instruction sequences due to the implementation and vendor dependency.

Casting primitive types

Primitive types are those non-composed types which can be handled directly by the (virtual) machine instructions, i.e., intlongfloat, etc... Those types doesn't have inner structure, and are always passed by value if the programmer doesn't specify explicitly other behavior (using the out and ref modifiers). Let's see a simple example about using and casting primitive types:

Hide   Copy Code
int z = 10;
double r = 3.4;
uint n = 20; r = z; // Implicit conversion from int to double (1)
z = (int)r; // Explicit conversion from double to int (2)
n = (uint)z; // Explicit conversion from int to uint (3)

This sample performs some conversions in the set of primitive types, leaving in some cases the casting tasks to the compiler and marking conversions explicitly in some other cases.

OK, time to dive into the MSIL generated code and check the impact of type casts in our code:

Hide   Copy Code
.locals init ([0] int32 z,
[1] float32 r,
[2] unsigned int32 n)
IL_0000: ldc.i4.s 10
IL_0002: stloc.0
IL_0003: ldc.r4 (9A 99 59 40)
IL_0008: stloc.1
IL_0009: ldc.i4.s 20
IL_000b: stloc.2 //(1)
IL_000c: ldloc.0
IL_000d: conv.r4
IL_000e: stloc.1
IL_000f: ldloc.1 //(2)
IL_0010: conv.i4
IL_0011: stloc.0
IL_0012: ldloc.0 //(3)
IL_0013: stloc.2
IL_0014: ret

As we can see, there are several Conv.XY instructions in the code, whose function is to convert the value at the top of the stack to the type designed in the opcode (r4, i4, etc...). From now, we know that the "innocent" explicit and implicit conversions between primitive types generate instructions which can be avoided with a consistent type usage. The same conversions are applied in 64-bit data types, such as doublelong and ulong.

Note that the last type cast doesn't need an explicit "Conv" opcode due to the nature of the involved types: intand uint; these types have a very close storage structure (big endian bit order with a sign bit in the signed type) and conversion sign issues must be controlled by the programmer.

A special kind of primitive type is bool (handled internally as an int), whose conversions to numeric types (and backward) are not allowed in C#, so we will not study them.

Downcasting object references

C# provides two ways for casting object references (note that all types, unless those studied in the previous section, are reference types):

Hide   Copy Code
object myClass = new MyClass();

((MyClass)myClass).DoSome(); //(1)
(myClass as MyClass).DoSome(); //(2)</CODE>

The previous is a good example of downcasting (casting from the top to the bottom of the class hierarchy). The method used to perform the cast appears to be the same, but the generated MSIL sequences are a bit different:

Hide   Copy Code
  .locals init ([0] object myClass)
IL_0000: newobj instance void Sample.MyClass::.ctor()
IL_0005: stloc.0
IL_0006: ldloc.0 //(1)
IL_0007: castclass Sample.MyClass
IL_000c: callvirt instance void Sample.MyClass::DoSome()
IL_0011: ldloc.0 //(2)
IL_0012: isinst Sample.MyClass
IL_0017: callvirt instance void Sample.MyClass::DoSome()
IL_001c: ret

In the first line of code, the compiler emits a "Castclass" opcode, which converts the reference to the type specified between the parenthesis if possible (if not, an InvalidCastException exception is thrown).

In the second case, the as operator is translated as an "IsInst" opcode, which works much faster, because it only checks the reference type but doesn't perform any sort of cast (nor throws any exception).

In performance terms, we prefer the second option, because the "IsInst" speeds up much more the code execution, avoiding type casts and exception throwing. Here is a sample of the speed increment obtained using the "as" operator:

In the other hand, parenthesized casts give a better error control to programmers, avoiding the null-reference errors obtained when invalid typecasts happen using the "as" operator.

Upcasting object references

Let's make the opposite! Now it's time for climbing up into the class hierarchy, and see how slow (or fast) are these sort of casts. The following example creates an object of the type MyDerivedClass and stores its reference in a MyClass type variable:

Hide   Copy Code
MyDerivedClass myDerivedClass = new MyDerivedClass();
MyClass myClass = myDerivedClass;

And the produced code is:

Hide   Copy Code
.locals init ([0] class Sample.MyDerivedClass myDerivedClass,
[1] class Sample.MyClass myClass)
IL_0000: newobj instance void Sample.MyDerivedClass::.ctor()
IL_0005: stloc.0
IL_0006: ldloc.0
IL_0007: stloc.1
IL_0008: ret

As we can see, there are no conversion opcodes, just reference loading and storing. This is good for out efficiency purposes... as expected, upcasting type checks are made at compile time and the runtime costs are as cheap as a simple assign between variables of the same type.

Casting operators

C# language contains a great feature which allows to define implicit and explicit conversion operators. The efficiency of these casting methods depends on the casting method implementation. Anyway, these functions are always static and have only one parameter, so the procedure call overhead is small (no "this" parameter should be passed). Anyway, it seems to be that the Microsoft C# compiler doesn't inline those methods, so arranging parameters and return addresses in the stack may slow your code execution speed.

Putting it all together

Here are some general tips for optimizing your programs based on the results obtained in the previous sections:

  • Numeric type conversions are usually expensive, take them out of the loops and recursive functions and use the same numeric types when possible.
  • Downcasting is a great invention but the type checks involved have a great impact on execution performance, check the object types out of loops and recursive functions, and use the "as" operator into them.
  • Upcasting is cheap!, use it everywhere you need.
  • Build lightweight conversion operators to make custom casts faster.

Tools used

All the tests and disassemblies have been made using the tools included in the .NET Framework SDK. ILDasm can tell you much about your program's performance flaws, so play with it.

C# 类型转换的开销的更多相关文章

  1. SAM4E单片机之旅——23、在AS6(GCC)中使用FPU

    浮点单元(Floating Point Unit,FPU),是用于处理浮点数运算的单元. 为使用FPU,除了需要启用FPU外,还需要对编译器进行设置,以使其针对浮点运算生成特殊的指令.虽然在Atmel ...

  2. Collections你用对了吗?

    .Net有两类基础的集合类型:List和Dictionary.List是基于Index的,Dictionary是基于key的.集合类型一般实现了IEnumberable,ICollection或者Il ...

  3. 第四章-shceme和数据类型优化

    选择数据类型的原则: 1.更小通常更好.因为占用更少磁盘,内存和cpu缓存.但是要确保没有低估,因为进行alter时,是很耗时和头疼的操作.所以当无法确定数据类型的时候,选择不会超过范围的最小类型. ...

  4. C#高级编程第11版 - 第六章 索引

    [1]6.2 运算符 1.&符在C#里是逻辑与运算.管道符号|在C#里则是逻辑或运算.%运算符用来返回除法运算的余数,因此当x=7时,x%5的值将是2. [2]6.2.1 运算符的简写 1.下 ...

  5. C++强制类型转换操作符 dynamic_cast

    dynamic_cast是四个强制类型转换操作符中最特殊的一个,它支持运行时识别指针或引用. >>>>>>>>>>>编译器的RTTI设 ...

  6. 尽量采用as操作符而不是旧式C风格做强制类型转换

    http://www.cnblogs.com/JiangSoney/archive/2009/08/07/1541488.html MSDN: https://msdn.microsoft.com/z ...

  7. 从零开始学C++之从C到C++(二):引用、内联函数inline、四种类型转换运算符

    一.引用 (1).引用是给一个变量起别名 定义引用的一般格式:类型  &引用名 = 变量名: 例如:int a=1; int  &b=a;// b是a的别名,因此a和b是同一个单元 注 ...

  8. C++进阶--显式类型转换(casting)

    //############################################################################ /* * 显式类型转换 * * 类型转换 ...

  9. 从Qt谈到C++(一):关键字explicit与隐式类型转换

    转载:果冻虾仁 提出疑问 当我们新建了一个Qt的widgets应用工程时.会自动生成一个框架,包含了几个文件. 其中有个mainwindow.h的头文件.就是你要操纵的UI主界面了.我们看看其中的一段 ...

随机推荐

  1. 【原】无脑操作:EasyUI Tree实现左键只选择叶子节点、右键浮动菜单实现增删改

    Easyui中的Tree组件使用频率颇高,经常遇到的需求如下: 1.在树形结构上,只有叶子节点才能被选中,其他节点不能被选中: 2.在叶子节点上右键出现浮动菜单实现新增.删除.修改操作: 3.在非叶子 ...

  2. pwnable.kr input解题记录

    pwnable input解题记录 给了源码如下: #include "stdio.h" #include "unistd.h" #include " ...

  3. mysql下载安装及常见问题

    1.下载MySql 官网下载地址:https://dev.mysql.com/downloads/mysql/ 2.安装 如果下载的是zip的,直接解压目录即可,我的解压目录时:C:\mysql\my ...

  4. SQL insert into select 语句

    遇到权限数据变更的需要批量到别的平台, 在175平台添加一个权限需要, 批量到别的现有平台, 以后的建站, 会把sql放到自动建站里面; 权限的 insert into select 表一: `ous ...

  5. [题解]洛谷P2709 小B的询问

    地址 是一道莫队模板题. 分析 设\(\text{vis[i]}\)表示元素\(\text{i}\)出现的次数 当一个元素进入莫队时,它对答案的贡献增加.有\(\delta Ans=(X+1)^2-X ...

  6. js 秒数格式化

    function formatSeconds(value) { var theTime = parseInt(value);// 秒 var theTime1 = 0;// 分 var theTime ...

  7. day09(垃圾回收机制)

    1,复习 文件处理 1.操作文件的三步骤 -- 打开文件:硬盘的空间被操作系统持有 | 文件对象被应用程序持续 -- 操作文件:读写操作 -- 释放文件:释放操作系统对硬盘空间的持有 2.基础的读写 ...

  8. Python中的函数定义方法

    def test(x): "hjkasgd" x += 1 return x def——定义函数的关键字 test——函数名 ()——内可定义形参 ""——文档 ...

  9. 用redis实现分布式锁,秒杀案例(转)

    分布式锁的简单实现代码: 需要的jar包: jedis-2.9.0.jar. commons-pool2-2.4.2.jar import java.util.List; import java.ut ...

  10. 在windows环境利用celery实现简单的任务队列

    测试使用环境: 1.Python==3.6.1 2.MongoDB==3.6.2 3.celery==4.1.1 4.eventlet==0.23.0 Celery分为3个部分 (1)worker部分 ...