Thinking with the head skillfully, improving the software running efficiency--talking about the program algorithm

Source: Internet
Author: User
Tags foreach garbage collection
program | algorithm about vc# How to improve running efficiency

We all know that. NET makes it easier for us to develop programs, especially for the development of large software with enterprise, it uses GC (garbage collection) mechanism like Java, with garbage collection can throw away the pain pointer in C or C + +, can not spend mind to pay attention to memory is not already released, Can be said to reduce the burden on programmers.

I'm not saying here about GC is not good, and GC has its own drawbacks, if you do not need to use the memory will not be released in advance, more or less wasted memory, I do not want to say how successful the GC in C #, but from my own development experience, C # is very disappointing, the same function in C + + implementation than C # To achieve more memory savings, I did not use C # to develop too large programs, do not know exactly how much C # on memory requirements, according to my program analysis, there is garbage collection mechanism of the language is C + + 3-20 times the memory, how to improve the C # development of the operating efficiency of the program, how can we save space?

It is well-known that the algorithm is the core of the program, we can not change the language itself unique, only from our algorithm to start to find a way! The same can be achieved with different ideas, the so-called roads with Rome, we all know, maybe we are missing is thinking, we as programmers are doing thinking activities, can be said to think a question is a pleasure!

Let me say one example, with regard to the use of vc#, you can use different methods for the same problem.

The problems we now face are as follows (assuming)
You're going to use the TreeView to do something like a resource browser.

The algorithm is simple: traverse the files in the computer and add 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 code above shows that all the files in the C disk are in the TreeView, and you can analyze the order in which it is executed:




After the Button1 Click is raised, perform the following action, and then call the AddNode (TreeNode tn) Method! Look inside, actually use recursion, the first is to add "c:\\" to root contact, then visit the first folder in C disk, add to the tree, and then access the first folder in the first folder in C disk, add to the number, to the secondary recursion, when to the end, and then return to add the first file, the image is to do the S-shaped cycle! I can not use the picture to illustrate the specific situation, perhaps more difficult to understand, I think as long as they think carefully is to come out, we should always remember that the programmer of this profession is to think of the profession! But also to innovate!

There is no doubt that the above algorithm is correct, but to the end of the use of how much memory, all the points are added to the TreeView, there is no doubt that this use a lot of space! Even more depressing is if we want to operate the software, must wait for the software has done the last part of the matter before responding to our next operation, the above method will lead to our software has a crash phenomenon there is no better way?


The problem arises when we use conventional ideas, and we're almost out of our own changes!

We think carefully, in the TreeView, we want to see (here refers to the eye can be seen in the tree) all the contacts need we use the mouse to expand, when we do not expand the contact (before the expansion of the contact is 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 on a one-time!

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<atemp.length; i++)
{
TreeNode my2=new TreeNode (@aTemp [i]. FullName);
Mnode.nodes. ADD (MY2);
Addmytreeview (MY2); This is a bad method!
DirectoryInfo di2=new DirectoryInfo (@my2. Text);
Directoryinfo[] Atemp2=di2. GetDirectories ();
for (int j=0;j<atemp2.length; j + +)
{
TreeNode my3=new TreeNode (@aTemp2 [j]. FullName);
My2. Nodes. ADD (MY3);
}


}
}
catch (Exception ex)
{

}

}

private void treeView1_AfterSelect (object sender, System.Windows.Forms.TreeViewEventArgs 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 idea that we have fully utilized all the methods of the TreeView control as follows:
Add a "c:\\" point to the form load, and then invoke the private void Addmytreeview (TreeNode mnode), but it uses a two-layer loop, adding a two-layer file relative to the C-disk. (C-Disk file We can think of as a directory at the first level), when the afterselect occurs this time call Addmytreeview (TreeNode mnode), when the BeforeExpand clear contact carefully compare the algorithm, We can see that the second is more convenient than the first, the second also did not increase what technical difficulty, just changed the way of thinking!



You may say my first method is intentionally wasted space, of course I just to do a contrast demo, I believe you can also in my second method of more optimization!


Software development is not a simple copy and paste, more to think about, do not despise the resources of small programs, perhaps in the large program can save unnecessary space time waste!

It's written here today!


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.