Study some performance problems in simple factory mode. Net binaryformatter causes of slow Object serialization

Source: Internet
Author: User
Tags object serialization

In the past two days, I have studied the design patterns. Now I still don't know which scenarios the various design patterns will be used in. Haha, maybe it hasn't reached that level yet.

The most common model is the factory model.

Come to the simplest Factory:

     Interface  Ifruit {}  Class  Apple: ifruit {}  Class  Orange: ifruit {}  Class  Factory {  Public   Static Ifruit create ( String Name ){  Switch  (Name. tolower ()){  Case   "  Apple  "  :  Return   New  Apple ();  Case   "  Orange  " :  Return   New  Orange ();  Default  :  Return   Null  ;}}} 

Although it looks clear, when you use it, if you come with a new fruit, wouldn't you have to modify the factory method? How many cases have to be added, A lot of trouble.

As a result, reflection is used to create products. The details are as follows:

The other classes are the same, but the factory method is modified.

 Namespace  Simple factory using reflection {  Class Factory {  Public   Static Ifruit makefruit ( String  Name ){  Try  {Type = Type. GetType ( "  Use reflection to implement a simple factory.  " + Name, True  ); Ifruit fruit = Activator. createinstance (type) As Ifruit;  Return  Fruit ;}  Catch  (Exception e) {console. writeline (E. Message );  Return   Null  ;}}}} 

In this way, it will not be done once and for all. No matter how many products you add, I have accepted this factory and do not need to make any changes. Haha ~~

Run the following command first:

After reflection is used, the performance is also greatly different. This is not an order of magnitude. What should I do?

What should I do !!!

 

Of course, cache is used ~~~ Because all the instance types of the class are reference types, you must use deep copy to complete the process.

 

  private const string spacename = "study the performance of reflection and caching _ deep copy. "; Private Static memorycache = new memorycache (" cache "); public static ifruit make (string name) {If (memorycache. contains (name) {// return memorycache [name] As ifruit; return clone (memorycache [name]);} type = type. getType (spacename + name); ifruit fruit = activator. createinstance (type) as ifruit; memorycache. add (name, fruit, datetimeoffset. maxvalue); return fruit;} Private Static ifruit clone (Object t) {using (memorystream stream = new memorystream () {binaryformatter formatter = new binaryformatter (); formatter. serialize (stream, T); stream. position = 0; return formatter. deserialize (Stream) as ifruit; }} 

Modify the factory method, add a cache, and use binaryformatter to perform deep copy. Of course, do not forget to introduce two namespaces:

 
Using system. runtime. serialization. formatters. Binary; using system. runtime. caching;

This is always okay. I'm smiling.

It's a donkey, it's a horse.

Is it too early to smile ~~ When cache is used, the time level goes to another level, which is too scary .~~~~ (>_< )~~~~

In fact, due to the useBinaryformatter to complete deep copy, and binaryformatter is time-consuming during deserialization. For more information, see Article :. net binaryformatter causes slow Object serialization

modifying it is not a sin, so another deep COPY method is used: implement the icloneable interface

 

 
Public interface ifruit: icloneable {string name {Get; Set ;}} class Apple: ifruit {public string name {Get; Set ;} public object clone () {Apple = new Apple (); apple. name = This. name; return Apple ;}}

 

 

The factory method was slightly modified:

 

Private const string spacename = "studying the performance of reflection and caching _ deep copy _."; public static dictionary  <  String  , Ifruit  > Dictionary = new dictionary <  String  , Ifruit  > (); Public static ifruit make (string name) {If (dictionary. containskey (name) {ifruit frifruit; dictionary. trygetvalue (name, out frifruit); Return frifruit. clone () as ifruit;} type = type. getType (spacename + name); ifruit fruit = activator. createinstance (type) as ifruit; dictionary. add (name, fruit); return fruit ;} 

 

Finally, let's try again:

Haha, isn't that nice? Look at this time level ~

The above is my personal understanding of the performance of a simple factory, which may be incorrect. I hope you can correct me.

Source codeDownload: Research reflection and slow storage (deep dive performance .zip

 

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.