C # performance problems caused by no pointer

Source: Internet
Author: User

 

The question is okay.

The structure type can allocate space on the stack, while the reference type can only allocate pointers.

Through arrays, the structure type can be allocated with a continuous space and a pointer.

Arrays can only be used with reference types to allocate consecutive pointers and scattered spaces. This is a potential issue of performance.

Therefore, it is necessary to serialize the object and focus more on performance. It is necessary to combine arrays with structure types.

No pointer, which can be accessed by subscript, that is, the elements of the enumerated array, need to return the array itself (pointer) and its current subscript (Natural Number) two things, which is not convenient, but it can basically meet the requirements.

The problem is that many data structure APIs do not return arrays and subscripts, but return values themselves. If the value is of the reference type, it can also be used as a pointer. If the value is of the value type, it cannot enumerate elements.

Maybe we don't need to enumerate the elements. We only need to change the elements. The element of the extracted value type is to clone this setting, which is also a potential point of performance problems.

What should I do if I want to return a pointer instead of a value? Returns the object that can access it and the method parameters that can be accessed.

For example, a binary tree, a Node object holds a value type, and two pointers point to the left and right nodes. Generally, you cannot modify the return value of the data structure api. The only way is to return the node.

For example, if an array and a node is a value, the return value cannot modify the value of the node. Instead, an array and subscript of the node must be returned.

We cannot change the class libraries that come with. net, but to implement a high-performance data structure, we need to consider providing reference access interfaces to avoid meaningless replication.

C # the built-in IEnumerable and IEnumerator are similar to the above concepts, but the values of the current enumeration returned cannot be modified. Second, it is a tool for Traversing purposes, starting from 0, not from the items you need to use. For example, if you want to return 3rd items of the array, but the enumerated type will return an enumerator whose subscript starts from-1.

The ideal design should be similar:

Class Tor <Node, T> {

Node node;

Int I;

Pubulic T Value {get {return node [I];} set {node [I] = value }}

Public bool move () {if (node. Length> I) {I ++; return ture;} else return flase ;};

Public Tor (Node n, int index) {node = n; I = index );

}

 

Static void main (){

Int [] m = {0, 1, 2, 3, 4 };

Tor <int [], int> t = new Tor (m, 2 );

T. Value = 5;

While (t. move) t. Value = 0;

List <int> list = new List <int> (Enumerable. Rangle (0, 4 ));

Tor <List <int>, int> t = new Tor (list, 2 );

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.