c#赋值的执行顺序


主要代码

有下面一段代码,

/*@Magma 2014/05/22 
 *This code is testing the performance of boxing
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Service
{
    public class Boxing
    {
        /// <summary>
        /// Test the performance of boxing 
        /// </summary>
        /// <param name="cycleTimes">the times of cycles</param>
        /// <returns></returns>
        public void PerformanceTest(int cycleTimes) {
            //Normal operation
            DateTime begin = DateTime.Now;
            for (int i = 0; i < cycleTimes; i++)
            {
                int ita = 63;
                //int itb = ita;
            }
            Console.WriteLine("the time of Normal:" + (DateTime.Now - begin).Seconds + "s," + (DateTime.Now - begin).Milliseconds + "ms"); 


            //boxing
            begin = DateTime.Now;            
            for (int i = 0; i < cycleTimes; i++)
            {
                object ob = 1;//inboxing
                //int it = (int)ob;//unboxing
            }
            Console.WriteLine("the time of Boxing:" + (DateTime.Now - begin).Seconds+"s,"+(DateTime.Now - begin).Milliseconds+"ms"); 
        }
    }
}


循环4000次时

用如下代码调用:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Service;
using Model;

namespace KindsIntegration
{
    class Program
    {
        static void Main(string[] args)
        {
            Boxing box = new Boxing();
            box.PerformanceTest(4000);
            Console.ReadLine();
        }
    }
}

输出(反复运行多次):


这个结果很难理解,装箱操作的时间竟然比普通赋值时间还要短。

在循环次数小于400000次时一直这样,为了消除执行先后的影响,我改变了Performance函数中装箱与普通操作的先后顺序,结果一样(装箱时间<普通赋值)


循环1000000

输出(反复运行多次):


这个结果比较正常,装箱的时间超过正常赋值


原因:

论坛上有人帮忙回答,结果真是奇怪。原因是下面这一句:

DateTime begin = DateTime.Now;

赋值前,要先计算出等号右边的值,再赋值给左边。在得到 datetime.now后,DateTime begin这是一个新对象,需要创建,时间就是消耗在这上面,改成如下的代码,则会清楚地展示原因:

/*@Magma 2014/05/22 
 *This code is testing the performance of boxing
 */
using System;

namespace Service
{
    public class Boxing
    {
        /// <summary>
        /// Test the performance of boxing 
        /// </summary>
        /// <param name="cycleTimes">the times of cycles</param>
        /// <returns></returns>
        public void PerformanceTest(int cycleTimes) {
            //Normal operation
            DateTime begin = DateTime.Now;
            Console.WriteLine("begin:"+begin.Millisecond);
            begin = DateTime.Now;
            Console.WriteLine("begin:" + begin.Millisecond);
            begin = DateTime.Now;
            Console.WriteLine("begin:" + begin.Millisecond);
            begin = DateTime.Now;
            Console.WriteLine("begin:" + begin.Millisecond);
            begin = DateTime.Now;
            Console.WriteLine("begin:" + begin.Millisecond);
            for (int i = 0; i < cycleTimes; i++)
            {
                int x = 63;
                int y = x;
            }
            Console.WriteLine("the time of Normal:" + (DateTime.Now - begin).Seconds + "s," + (DateTime.Now - begin).Milliseconds + "ms"); 

            //boxing
            begin = DateTime.Now;            
            for (int i = 0; i < cycleTimes; i++)
            {
                int x = 1;
                object ob = x;//inboxing
            }
            Console.WriteLine("the time of Boxing:" + (DateTime.Now - begin).Seconds+"s,"+(DateTime.Now - begin).Milliseconds+"ms"); 
        }
    }
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
《轻松学C#(图解版)》完整扫描版================================================================ 基本信息 作者:谷涛、扶晓、毕国锋 丛书名:轻松学开发 出版社:电子工业出版社 ISBN:978-7-121-20223-0 出版日期:2013年6月 开本:16开 页码:408页 版次:1-1 定价:¥55.00 所属分类:计算机 > 软件与程序设计 > C# ================================================================ 内容简介 本书由浅入深,全面、系统地介绍了C#程序设计。除了详细地讲解C#知识点外,本书还提供了大量的实例,供读者实战演练。本书共分三篇。第一篇是C#概述篇,主要介绍的是Visual Studio 2012的开发环境及搭建。第二篇是面向对象基础篇,主要介绍类、对象、字段、方法、流程控制、数组、继承、属性、运算符重载、接口等C#基本内容。第三篇是应用技术篇,主要介绍的是异常处理、文件和流、委托、事件、Lambda表达式、命名空间、预处理器、程序集、运行时类型标识、反射、特性、泛型、LINQ和数据库开发等。 ================================================================ 图书目录 第一篇 C#概述篇 第1章 C#入门 2 1.1 C#概述 2 1.1.1 C#的发展 2 1.1.2 C#开发的基础 2 1.2 搭建开发环境 3 1.2.1 Visual Studio 2012软硬件配置要求 3 1.2.2 下载Visual Studio 2012 3 1.2.3 安装Visual Studio 2012 4 1.2.4 初始化配置 7 1.3 第一个程序—Hello World 8 1.4 小结 11 1.5 习题 12 第二篇 面向对象基础篇 第2章 类和对象 16 2.1 分析Hello World程序 16 2.2 语法规范 17 2.2.1 标识符 17 2.2.2 关键字 18 2.2.3 注释 19 2.3 定义类 20 2.4 实例化对象 20 2.5 小结 20 2.6 习题 21 第3章 定义类——字段 23 3.1 数据类型 23 3.1.1 简单值类型 23 3.1.2 值的表示——字面量 26 3.1.3 转义序列 27 3.2 定义字段 27 3.2.1 定义字段 28 3.2.2 静态字段和实例字段的访问 28 3.2.3 字段初始化 29 3.2.4 字段的动态赋值——Read()和ReadLine() 31 3.2.5 字段输出 31 3.2.6 格式化输出 32 3.2.7 数据类型转换 39 3.2.8 只读字段 41 3.2.9 访问控制 41 3.3 运算符 43 3.3.1 算术运算符 43 3.3.2 自增、自减运算符 44 3.3.3 复合赋值运算符 45 3.3.4 位运算符 46 3.3.5 sizeof运算符 47 3.3.6 运算符的优先级 47 3.4 小结 49 3.5 习题 49 第4章 定义类——方法 51 4.1 方法的概述 51 4.1.1 定义方法 51 4.1.2 方法的调用 52 4.2 方法体的构成 53 4.2.1 局部变量 53 4.2.2 局部变量与字段同名的解决——this关键字 54 4.2.3 语句 55 4.2.4 全局变量 55 4.3 返回值 56 4.3.1 返回值类型 56 4.3.2 从方法返回 57 4.4 参数 58 4.4.1 参数的分类 58 4.4.2 引用参数(ref参数) 59 4.4.3 输出参数(out参数) 61 4.5 特殊的方法 62 4.5

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值