Thinking in mind, improving software efficiency - Talking about program algorithm

zhaozj2021-02-16  82

How to improve operation efficiency about VC #

Everyone knows that .NET allows us to develop more simple, especially for the development of large software, which use GC (garbage recovery) mechanism like Java. With garbage collection, you can lose it in C or C In the painful pointer, you can not need to pay attention to the memory, it is not released, you can say that the programmer is alleviated. I don't say that the GC is not good. GC also has its own shortcomings. If the memory that is not necessary is not released in advance, more or less waste of memory, I don't want to say that GC in C # is more successful, But from my own experience, the efficiency of C # is very disappointing. The same function is achieved by C than C #, I don't use C # to develop too big programs. I don't know how big it is. According to the analysis of my program, the language with garbage recovery is 3-20 times the C memory, how can I improve the operational efficiency of the process of C # development, how can I save space?

Ask more, the algorithm is the core of the program, we can't change the language itself, only from our algorithm to find a way! The same can be achieved with different ideas. The so-called bar road is in Rome. You know that maybe we are missing. We can think of the event as a programmer. A happy family! Below I said an example, about the use of VC #, can use different methods for the same problem

The problems we face now are as follows (assuming) to make a similar resource browser with TreeView

Speaking of the algorithm is very simple: it is traversing the file in the computer and adds to the TreeView contact!

One of the simplest (core) code is as follows:

private void button1_Click (object sender, System.EventArgs e) {try {DirectoryInfo ainfo = new DirectoryInfo ( "C: //"); TreeNode nan = new TreeNode (ainfo.FullName); this.treeView1.Nodes.Add (nan) AddNode (nan);} catch (system.io.directoryNotFoundException Ednfa) {MessageBox.show (Ednfa.Message);}}

private void addNode (TreeNode tn) {DirectoryInfo di = new DirectoryInfo (tn.Text); DirectoryInfo [] aTemp = di.GetDirectories (); if (aTemp.Length> 0) foreach (DirectoryInfo d in aTemp) {TreeNode node = new TREENODE (D.Fullname); TN.NODES.ADD (Node); addnode (node);} fileinfo [] sfiles = di.getfiles (); foreach (fileInfo f in sfiles) {Treenode Node = new Treenode (f.Fullname ); Tn.nodes.add (node);}} The above code shows that the full file of the C drive is TreeView, you can analyze the order of the execution:

Button1's Click is initiated, then performs the following operation, then call the AddNode (Treenode TN) method! Look carefully, actually use recursive, first to add "c: //" to the root point, then access the first folder of the C drive, join to the tree, then access the first folder under the C drive A folder, add to the number, recursive, when you get to the end, then return to add the file, the image is to do a S-shaped cycle! I can't use the picture to explain the specific situation, maybe it is more difficult to understand, I think as long as I think it is, I can think about it, I will always remember, the programmer is the profession of thinking! Also innovate!

There is no doubt that the above algorithm is correct, but how many memory is used until the last contact is added to TreeView, which is undoubtedly used in many spaces! More depressed is if we want to operate software, you must wait for the software that has already finished the last part, responding to us next to operation, the above method will cause our software to have a better way?

The problem will appear us to use the routine ideas, it is a little change!

We think carefully, in TreeView, we want to see (here, see that people can see him in the tree) All contacts require us to start with the mouse, when we don't expand contacts (no deployment Is it necessary to add to the tree)? The above algorithm is a one-time initialization of all the contacts, as long as we do not initialize!

Look at the following method:

private void Form1_Load (object sender, System.EventArgs e) {this.treeView1 .Nodes .Clear (); DirectoryInfo my = new DirectoryInfo ( "c: //"); TreeNode nan = new TreeNode (@ my.FullName); this .treeView1.nodes.add (nan); addmytreeView (nan);

}

private void addMyTreeView (TreeNode mNode) {try {DirectoryInfo di = new DirectoryInfo (@ mNode.Text); DirectoryInfo [] aTemp = di.GetDirectories (); for (int i = 0; i

Private void TreeView1_aftelect (Object Sender, System.Windows E) {E.Node.Nodes.clear (); this.addmyTreeView (E.NODE);}

private void treeView1_BeforeExpand (object sender, System.Windows.Forms.TreeViewCancelEventArgs e) {e.Node.Nodes.Clear (); this.addMyTreeView (e.Node);} The above is the use of our full control of all methods treeView The above ideas are as follows: Add the "C: //" contact in the form LOAD, then call Private Void AddmyTreeView (Treenode Mnode), but use two cycles in this, and add a file relative to the C drive. Two layers, (C disk file we can see a directory of primary level) Two more convenient than the first, the second type did not increase what technical difficulty, just changed a way of thinking!

You may say that my first way is to waste space. Of course, I just make a contrast demonstration, I believe you can also optimize in my second China France!

Software development is not simple to copy and paste, more important to think, do not underestimate the resource problem of the applet, maybe the unnecessary space time is ware!

Write this today!

转载请注明原文地址:https://www.9cbs.com/read-21837.html

New Post(0)