C#的结构/类堆栈/堆控制? [英] c# structs/classes stack/heap control?

查看:51
本文介绍了C#的结构/类堆栈/堆控制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此在c ++中,这非常容易.您想在堆上分配任何类/结构,请使用new.如果您希望将其放在堆栈中,请不要使用new.

so in c++ it's very easy. you want whatever class/struct to be allocated on the heap, use new. if you want it on the stack, don't use new.

在C#中,我们始终使用new关键字,并且根据它是结构还是类,将其分配在堆栈或堆上(结构进入堆栈,类分配到堆)-在某些应用程序中更改设计时,可能只有巨大的性能差异,使得只有那些真正进入那里的对象才能进入堆.

in C# we always use the new keyword, and depending on whether it's a struct or a class it's allocated either on the stack or on the heap (structs go to the stack, classes to the heap) - and in some applications there can be a HUGE performance difference when changing the design such that only those objects go to the heap that really belong there.

我想知道的是-是否有一种直接的方法来控制对象的分配位置,而与它是声明为struct还是class无关?我知道值类型(结构)可以装箱进入堆(但是装箱/拆箱会降低性能).有没有办法在堆栈上分配类?

What I wonder is - is there a direct way to control where an object is allocated independant of whether it's declared as struct or class? i know that value types (structs) can be boxed to go to the heap (but boxing/unboxing comes at a performance cost). is there a way to allocate classes on the stack?

此外,是否有任何机制来分配原始内存并使用C ++中的placement之类的新东西?我知道这与被管理的想法相违背-但是如果您可以使用自定义的内存管理,则会对性能产生很大的影响.

Also, is there any mechanism to allocate raw memory and use something like placement new in C++? I know that this breaks with the idea of being managed - but it can make a big performance difference if you can use your custom memory management.

我之所以喜欢C#,是因为它很方便,因为它是垃圾收集器和其他东西-但是有时候,当在应用程序的瓶颈上工作时,最好对实际发生的事情进行更多的控制.

I love C# for it's convenience, for it's garbage collector and other things - but sometimes, when working on the bottleneck of an application, it meight be desirable to have more control over what is actually happening.

欢迎任何提示/提示:)

Any tips/hints welcome :)

效果示例:

struct Foo1
{
    public int i;
    public float f;
    public double d;
}

struct Foo2
{
   public Foo1[] bar;

   public void Init(){
        bar = new Foo1[100];
        for (int i = 0; i < 100; i++)
            bar[i] = new Foo1();
    }
}

class Program
{
    static void Main(string[] args)
    {
        DateTime time = DateTime.Now;
        Foo2[] arr = new Foo2[1000000];
        for (int i = 0; i < 1000000; i++)
        {
            arr[i] = new Foo2();
            arr[i].Init();
        }

        Console.WriteLine((DateTime.Now - time).TotalMilliseconds);
    }
}

这需要在我的机器上执行1.8秒(请注意,实际上只有分配正在进行中-没有参数传递)

This takes 1.8 seconds on my machine to execute (note that there is actually only allocation going on - no parameter passing)

如果Foo1从结构更改为类,则执行需要8.9秒!慢五倍

if Foo1 is changed from struct to class, execution takes 8.9 seconds! that's five times slower

推荐答案

在通常情况下,确实总是在堆上分配对象,但是C#确实允许您降低指针级别以进行大量互操作或非常高的操作性能关键代码.

While in the general case it's true that objects are always allocated on the heap, C# does let you drop down to the pointer level for heavy interop or for very high performance critical code.

不安全块中,可以使用 stackalloc 可以在堆栈上分配对象并使用它们作为指针.

In unsafe blocks, you can use stackalloc to allocate objects on the stack and use them as pointers.

举他们的例子:

// cs_keyword_stackalloc.cs
// compile with: /unsafe
using System; 

class Test
{
   public static unsafe void Main() 
   {
      int* fib = stackalloc int[100];
      int* p = fib;
      *p++ = *p++ = 1;
      for (int i=2; i<100; ++i, ++p)
         *p = p[-1] + p[-2];
      for (int i=0; i<10; ++i)
         Console.WriteLine (fib[i]);
   }
}

但是请注意,您不需要将整个方法声明为不安全的,只需对其使用unsafe {...}块即可.

Note however that you don't need to declare an entire method unsafe, you can just use an unsafe {...} block for it.

这篇关于C#的结构/类堆栈/堆控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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