.NET装箱/拆箱VS铸造性能 [英] .NET boxing / unboxing vs casting performance

查看:119
本文介绍了.NET装箱/拆箱VS铸造性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解两个解决方案都从性能的角度首选。
为例,我有两段代码:

I'm trying to understand which of two solutions are preferred from a performance perspective. For example, I have two pieces of code:

1)拳击/拆箱

int val = 5;
Session["key"] = val; 
int val2 = (int)Session["key"];



2)铸造(IntObj具有存储INT)

2) Casting (IntObj has int Value property to store int)

IntObj val = new IntObj(5);  
Session["key"] = val;
int val2 = ((IntObj )Session["key"]).Value;



什么是这些实例之间的内存管理的区别?
有没有执行这样的操作更快的方法

What is the memory management difference between these examples? Is there a faster way to perform such operations?

注意: 会话只是例如,它可以是任何词典<字符串对象>

NOTE: Session is just for example, it can be any Dictionary<string, object>

推荐答案

它看起来像你真正这里做的是手工用拳拳击内置比较。内置的拳击经过高度优化 - 所以我不希望在这里看到一个巨大的差异,但我们可以检查。重要的是,请注意,这两个具有相同内存的影响:一种含每 INT 盒装/包裹有一个 INT 字段,堆对象

It looks like what you are really doing here is comparing manual boxing with inbuilt boxing. The inbuilt boxing has been highly optimised - so I wouldn't expect to see a huge difference here, but we can check. Importantly, note that both have the same memory impact: one heap object containing one int field, per int boxed/wrapped.

下面显示了相当-多少次相同的两个接触;我会说,因此,只框它的直接/内置方式

The following shows pretty-much identical times for the two approached; I would say, therefore, just box it the direct / inbuilt way.

请注意:在发布模式下运行,没有一个调试器(最好在命令行)。需要注意的第一个电话是有预JIT一切

Note: run it in release mode, without a debugger (ideally at the command line). Note the first call is there to pre-JIT everything.

using System;
using System.Diagnostics;
public sealed class IntObj
{
    public readonly int Value;
    public IntObj(int value)
    {
        Value = value;
    }
}
static class Program
{
    static void Main()
    {
        Run(1, 0, false);
        Run(100000, 500, true);
        Console.ReadKey();
    }
    static void Run(int length, int repeat, bool report)
    {
        var data = new object[length];

        int chk = 0;
        var watch = Stopwatch.StartNew();
        for (int j = 0; j < repeat; j++)
        {
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = i;
                chk += i;
            }
        }
        watch.Stop();
        if(report) Console.WriteLine("Box: {0}ms (chk: {1})", watch.ElapsedMilliseconds, chk);
        chk = 0;
        watch = Stopwatch.StartNew();
        for (int j = 0; j < repeat; j++)
        {
            for (int i = 0; i < data.Length; i++)
            {
                chk += (int) data[i];
            }
        }
        watch.Stop();
        if (report) Console.WriteLine("Unbox: {0}ms (chk: {1})", watch.ElapsedMilliseconds, chk);

        chk = 0;
        watch = Stopwatch.StartNew();
        for (int j = 0; j < repeat; j++)
        {
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = new IntObj(i);
                chk += i;
            }
        }
        watch.Stop();
        if (report) Console.WriteLine("Wrap: {0}ms (chk: {1})", watch.ElapsedMilliseconds, chk);
        chk = 0;
        watch = Stopwatch.StartNew();
        for (int j = 0; j < repeat; j++)
        {
            for (int i = 0; i < data.Length; i++)
            {
                chk += ((IntObj)data[i]).Value;
            }
        }
        watch.Stop();
        if (report) Console.WriteLine("Unwrap: {0}ms (chk: {1})", watch.ElapsedMilliseconds, chk);
    }


}

这篇关于.NET装箱/拆箱VS铸造性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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