How to get sub array without cloning

前端 未结 3 1202
借酒劲吻你
借酒劲吻你 2020-12-18 09:19

I know in C# we can always get the sub-array of a given array by using Array.Copy() method. However, this will consume more memory and processing time which is

3条回答
  •  情深已故
    2020-12-18 09:42

    First of all you must consider this as a premature optimization.

    But you may use several ways to reduce memory consumption, if you sure you really need it:

    1) You may use Flyweight pattern https://en.wikipedia.org/wiki/Flyweight_pattern to pool duplicated resources.

    2) You may try to use unsafe directive and manual pointer management.

    3) You may just switch to C for this functionality and just call native code from your C# program.

    From my experience memory consumption for short-lived objects is not a big problem and I'd just write code with flyweight pattern and profile application afterwards.

提交回复
热议问题