. NET Framework developer guide-how to: improve performance

Source: Internet
Author: User

Msdn is still the most comprehensive knowledge base.

Today, I wrote a program under. Net Cf. In order to improve the performance of the program as much as possible, I searched the following document. In fact, Microsoft told us a lot of effective techniques.

Let's learn new things from a warm-up!

How to: improve performance

The following programming practices can save memory and improve the performance of device applications.

Saves memory using Windows Forms and graphics
  • ProvideBeginupdateAndEndupdateMethods controls use these two methods. The controls that provide these two methods include ComboBox, ListBox, listview, toolstripcombobox, and Treeview.

  • When you reposition the control, use the suspendlayout and resumelayout methods.

  • Load other forms in the background, use data to fill the control, and then use the show method.

  • Limit the event processing code to only execute important tasks so that the suspension process can continue.

  • Use the off-screen bitmap. For examples, see How to: Draw background images.

  • Override the onkeydown, onkeypress, and onkeyup methods on the control, instead of adding a key event handler.

Use Data and strings to save memory
  • InForUse an integer variable (int32 or int64) in a loop instead of an object variable.

  • Avoid using the tostring method of enumeration, because it will search for metadata tables, thus affecting performance.

  • Avoid outofmemoryexception errors. If you do not have enough memory for internal use or for new object instances, the Common Language Runtime Library may cause this exception. To avoid this exception, avoid writing large methods that occupy 64 K or more memory.

  • Remove system. sr. dll, which contains the error message string in the exception dialog box. You can use this file when deploying an application to save memory. If yesSystem. sr. dll. NET Compact framework dynamically loads the error string contained in system. sr. dll.

    If the. dll file does not exist on the device, all exceptions contain the message "unable to load resource assembly ". However, adding the system. sr. dll reference to the Microsoft Visual Studio 2005 project during development is always useful, as this way you will see a meaningful exception.

  • The string is unchangeable. Therefore, a new String object is created each time the string is modified. Use stringbuilder to construct strings that need to be modified frequently.

  • If you know the exact format used for datetime serializationDatetimeUse the parseexact method. Otherwise,DatetimeThe analyzer will try to apply multiple regional-specific formats in sequence.

  • Limit the number of opened sqlcecommand objects and release them after completion.

Saves memory when interacting with local code
  • In the Platform call operation, you can directly copy the data type to the local structure. This type has a common representation in the managed and unmanaged memory, suchInt32Or intptr. For value types that can be directly copied to the local structure with more than 32 bits, passing by reference is faster than passing by value. For more information about types that can be directly copied to the local structure, see types that can be directly copied to the local structure in. NET Compact framework.

  • Using inattribute and outattribute attributes for parameters in the function signature can reduce unnecessary sending and processing.

  • Use the method in the marshal class to manuallyIntptrAnd the managed object, such as ptrtostructure, ptrtostringbstr, getobjectfornativevariant, and getobjectforiunknown.

  • Use the prelink and prelinkall methods to trigger JIT compilation that supports local to managed call stubs (stub.

  • If you expect your local COM object to normally return s_false or other non-s_ OK hresult values, set the preservesig fieldTrueAnd match the managed signature with the local signature. This avoids the need for the runtime to convert the hresult value to an exception on the com call.Try/CatchSystem overhead of a block.

  • Use one platform call as much as possible, and use as few calls as possible.

Saves memory in the collection
  • If the set is based on arrays, use the indexer.

  • Once you have the opportunity, specify the size of the set, because dynamic resizing can greatly increase the additional storage.

  • Use a generic set to avoid system overhead caused by packing and unboxing value types. Define your own optimization set to achieve optimal performance.

Saving memory in XML
  • Use xmltextreader and xmltextwriter to replace xmldocument (because it occupies a large amount of memory ).

  • Specify xmlreadersettings and xmlwritersettings to improve performance. If the values ignorewhitespace and ignorecomments apply, they can significantly improve performance.

  • Uses UTF-8, ASCII, and UTF-16 character encoding, which is faster than ANSI and Windows code page encoding.

  • Avoid using an architecture for analysis because it requires more verification work.

  • When filling a dataset form from an XML source, you should map columns to attributes and use the typedDataset.

  • FillDatasetAvoid filling in the following items:

    • Architecture reasoning.

    • Nested tables.

    • MultipleDatetimeColumn. For better performance, use the ticks attribute value.

  • When XML deserialization is used, the following guidelines can be followed to improve performance:

    • Keep element and attribute names as short as possible because each character must be verified.

    • XML based on attribute data is faster than XML based on element data.

    • If applicable, use the system. xml. xmlnodereader. Skip method.

    • If the performance is very important, binary serialization should be considered.

  • Use an xmlserializer instance for each type of XML serialization to reduce the time required for searching metadata.

  • Since serialization of a large number of XML files may exhaust the memory, you should consider using binaryreader and binarywriter to generate a custom binary serialization mechanism.

Saves memory when using Web Services
  • Read and WriteDatasetDiffGram should be used. For more information, see DiffGram.

  • In XML formatDatasetAnd its architecture are saved to the device.

  • Create a simple web service method call during the initial screen, because the first call is slower than the subsequent call.

  • Handle network and data errors with caution.

  • In some cases, you can manuallyDatasetSerialize to an XML string and then execute a web service call to improve performance.

Saves memory in Advanced Programming
  • Asynchronous processing of large operations.

  • Avoid virtual calls .. NET Compact framework virtual invocation is about 30% slower than static or instance invocation. Due to resource constraints,. NET Compact framework does not use vtable. Therefore, methods must be called through conversion classes and interface hierarchies, but this is a very costly operation .. NET Compact framework maintains the cache of resolved virtual calls. Therefore, in most cases, you do not need to re-interpret the call.

  • Use Fields instead of attributes as much as possible.

  • When defining the value type, override the gethashcode and equals methods. If they are not overwritten, the runtime uses the normal version of these methods in the base valuetype class.

  • Use ing with caution. Using ing with a non-instantiated class will affect the performance of the instantiated object in the application.

  • Make sure that the managed resources in the resx file have a fully qualified name and these resources are correct. They must have the correct version and publickeytoken field. Finding the most appropriate alternative type for an unspecified type affects performance.

  • Note that in some cases, it is sufficient to directly read application data from a file, which is more efficient than using ResourceManager.ResourceManagerYou can detect multiple locations in the file system before locating your binary resources to find the most matched ancillary assembly. Use appropriate tools for jobs.

Http://msdn2.microsoft.com/zh-cn/library/1766918e (vs.80). aspx

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.