String V/s StringBuilder

With the help of my friend we did a small test to understand the behavior of StringBuilder and String in terms

 

  1. Execution Time
  2. Memory

We executed the following piece of code

 

 

class Program
    {
        ulong SessionID = 1000;
        ulong UserID = 292902;
        String GLOBESNAME = “XXX”;
        static void Main(string[] args)
        {
           
            Stopwatch sw = new Stopwatch();
            sw.Start();
            for (int i=0; i < 1000000; i++)
            {
                Program p = new Program();
                p.getXML();
                
            }
            sw.Stop();
            Console.WriteLine(sw.ElapsedMilliseconds.ToString());
            Console.Read();
        }
        private void getXML()
        {
            String xml = @”<INPUTXML>
                    <SESSIONID>” + SessionID.ToString() + @”</SESSIONID>
                    <USERID>” + UserID.ToString() + @”</USERID>
                    <GLOBESNAME>” + GLOBESNAME + @”</GLOBESNAME>
                    <LANGUAGE>ENGLISH</LANGUAGE>
                    <SEARCH>
                        <LOOKINGFORME>VAULT</LOOKINGFORME>
                        <LOOKINGFORMETYPE>ENTITY</LOOKINGFORMETYPE>
                        <LOOKINGFORMEREQUIRED>FALSE</LOOKINGFORMEREQUIRED>
                        <LOOKINGFOR>
                            <ACCESSTONAME>READ</ACCESSTONAME>
                        </LOOKINGFOR>
                    </SEARCH>
                </INPUTXML>”;
        }
    }
The getXML method was changed for StringBuilder.Append and StringBuilderAppenFormat.
StringBuilder.Append
===========================================================
          StringBuilder sb = new StringBuilder(500);
           sb.Append(@”<INPUTXML>
                    <SESSIONID>” + SessionID.ToString() + @”</SESSIONID>
                    <USERID>” + UserID.ToString() + @”</USERID>
                    <GLOBESNAME>” + GLOBESNAME + @”</GLOBESNAME>
                    <LANGUAGE>ENGLISH</LANGUAGE>
                    <SEARCH>
                        <LOOKINGFORME>VAULT</LOOKINGFORME>
                        <LOOKINGFORMETYPE>ENTITY</LOOKINGFORMETYPE>
                        <LOOKINGFORMEREQUIRED>FALSE</LOOKINGFORMEREQUIRED>
                        <LOOKINGFOR>
                            <ACCESSTONAME>READ</ACCESSTONAME>
                        </LOOKINGFOR>
                    </SEARCH>
                </INPUTXML>”);
For StringBuilder.AppendFormat
===========================================================
StringBuilder sb = new StringBuilder(500);
           sb.AppendFormat(@”<INPUTXML>
                    <SESSIONID>{0}</SESSIONID>
                    <USERID>{1}</USERID>
                    <GLOBESNAME>{2}</GLOBESNAME>
                    <LANGUAGE>ENGLISH</LANGUAGE>
                    <SEARCH>
                        <LOOKINGFORME>VAULT</LOOKINGFORME>
                        <LOOKINGFORMETYPE>ENTITY</LOOKINGFORMETYPE>
                        <LOOKINGFORMEREQUIRED>FALSE</LOOKINGFORMEREQUIRED>
                        <LOOKINGFOR>
                            <ACCESSTONAME>READ</ACCESSTONAME>
                        </LOOKINGFOR>
                    </SEARCH>
                </INPUTXML>”,SessionID,UserID,GLOBESNAME);
The reading for execution time was done through Stopwatch class and the reading for memory was taken using ANTS Profiler.
The idea was execute the getXML method 10,000,000 times and determine which out of three  methodology is best for string concatenation. The three method considered was
a. String
b. StringBuilder.Append
c. StringBuilder.AppendFormat
For StringBuilder we considered two cases
a. With inital capacity
b. With Capacity
 
the above was done only for execution time, we ignore this for Memory  load.
The results were SHOCKING compare to our belief. We are still not sure that we are right and would like comment from Microsoft CLR team about the behaviour.
The results for execution time is
The 500 in the above stats means that StringBuilder initial capacity was set to 500.
The readings for memory load taken using ANTS profiler is as follows
The columns are
a. Number of Objects Created.
b. Total Size for entire allocation.
c. Number of time the Garbage Collector was called.
The results are really SHOCKING. It seems concatination using String object is THE MOST EFFICIENT methodology. This is contradictary to what the internet have been saying since the inception of .NET.

ITS A REQUEST TO ALL, TO RUN THE ABOVE CODE AND SHARE YOUR RESULTS. WE ALSO REQUEST THE .NET CLR TEAM TO COMMENT ON THIS.

 

Advertisements

WCF: Hosting Service with multiple end points

In continuation of previous post regarding WCF, here goes another. In this post I will do step step explanation for hosting service using config file (remember this is the beauty of WCF, we don’t need to recompile to add multiple endpoints).

The assumtion is readers have already implemented the service contract. I will use a dummy class named IService for my explanation

  1. Create a instance of ServiceHost. 
    1. ServiceHost servicehost = new ServiceHost(typeof(Examples.ServiceImplementation));
  2. Use the Open to start listening.
    1. servicehost.Open();
  3. Now put the following in the App.Config file

config1.gif

Let me explain each of the element

  • The host element declares the base address for the endpoint. In this case its declaring two base address the http address and another one is the tcp address. The host address is used by the ServiceHost to listen on the respective port and address.

host.gif

  • There are three endpoints

http.gif

Each endpoint basically is collection of address, contract and binding. Here in the above case the address is not specified as we have already sepcified in the base address. The contract is Examples.IService and the binding is HTTP.

  • The second endpoint

tcp.gif

is the TCP endpoint for the same service. Smilar to the previous one this definess the endpoint as collection of Examples.IService as contract and binding as TCP and address is not mentioned and taken from the base address.

  • The last endpoint

mex.gif

is used by the client to generate the meta information about the Service. This  is standard and ideally should be there for each service.I hope the above helps in understanding this beautiful technology.

The author has 10+ years experience with Enterprise Poduct Development. He is also the Co-Founder of PCVITA. He have expertise in Enterprise Architecture, Enterprise Messaging Platform Migration, Email Archiving.

Understanding WCF

Windows Communication Foundation(WCF) that is part of .NET Framework 3.0 is in the line of Microsoft(MS) classic work of abstraction. MS have been innovator in providing abstraction over various technologies from many years. In the same tradition I do believe WCF is another achievement. Undoubtedly they understand the difficulties in any technologies being faced by technology implementors and then they use their experience to nake the technology simple by intoroducing the layer of abstraction.

Let me talk about WCf in particular now

  1. It was always challenge from many years to talk to remote objects.
  2. Many technologies have come and gone or become extinct as dinosors.
  3. To name a few will be CORBA or DCOM.
  4. But there was always problem with the technology itself and its implementation.
  5. Web Service was the most pouplar of all the technology and it still one of the best for such communication.
  6. The problem comes when we want to expose a remote object interface with multiple protocol interface such as HTTP, TCP etc.
  7. The problem with existing technology is that we have create multiple interface for the remote object i.e. each specific to each protocol.
  8. This really become a stumbling block while deciding on the protocol to be used as creating multiple interface results in maintainence overhead.
  9. So if we want a complete interoperable solution we need a web service based interface but if its standalone system the same Web Service interface becomes a performance bottleneck.
  10. WCF really becomes a great help for such scenario. It allows developer to create interface irrespective of the protocol to use.
  11. Developer can then configure the so called end points to decide on which protoocl to used for communication.
  12. The developer will create the interface and implement it. It will then expose the interface using multiple end points i.e. HTTP, TCP, Named Pipes.
  13. The client using the interface then can use the appropriate end point based their need.
  14. WCf as infrastructure takes care all overheads related to messaging etc.