.NET Remoting/Serialization way too slow vs C++ binary/tcp

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ajou_king@yahoo.com

    .NET Remoting/Serialization way too slow vs C++ binary/tcp

    I was running some tests on my Win32 1GHZ processor to see how long it
    would take to transmit objects numerous times via TCP/IP using C#
    ..NET Remoting vs the C++ trustworthy method of binary streams. I ran
    the test for 50K, 100K, 500K iterations, where each iteration consists
    of sending an object from a client process to a server process, and the
    server process sends back an ack.
    Here are the results:

    .NET Remoting C++ Binary TCP/IP
    -------------- ------------------
    50,000 Iterations: 128 seconds 3 seconds
    100,000 Iterations: 300 seconds 8 seconds
    500,000 Iterations: 1459 seconds 43 seconds

    In the above tests the .NET remoting overhead was 42.6x, 37.5x, and
    33.9x slower than the c++ version.

    Here is the object that was used:
    ---------------------------------------------------------------
    [Serializable]
    public class Msg
    {
    public int msgType_;
    public int seqNum_;
    public String symbol_;
    public int quoteId_;
    public int responseLevel_;
    public int eqiRole_;
    public float bidPrice_;
    public float offerPrice_;
    public int bidSize_;
    public int offerSize_;
    public float liquidityBidPri ce_;
    public float liquidityOfferP rice_;
    public int liquidityBidSiz e_;
    public int liquidityOfferS ize_;
    public int checkSum_;
    }

    The Server Process:
    ------------------------------------------------------
    using System;
    using Messages;


    namespace tcpServer
    {

    using System;
    using System.Net;
    using System.Net.Sock ets;
    using System.Runtime. Serialization.F ormatters.Binar y;
    using System.IO;


    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Class1
    {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
    int port = 7627;
    int N = 50000;
    int i = 0;

    BinaryFormatter bF = new BinaryFormatter ();

    TcpListener tcpListener = new TcpListener(por t);
    tcpListener.Sta rt();
    Socket soTcp = tcpListener.Acc eptSocket();
    Console.WriteLi ne("SampleClien t is connected through TCP.");
    NetworkStream stream = new NetworkStream(s oTcp,
    FileAccess.Read Write, true);
    BinaryReader bReader = new BinaryReader(st ream);

    DateTime beginTime = new DateTime();

    while (i < N)
    {
    if (i == 0)
    {
    beginTime = System.DateTime .Now;
    }

    ++i;
    Byte[] received = new Byte[1024];
    Messages.Msg msg = new Messages.Msg();
    msg = (Messages.Msg)b F.Deserialize(s tream);
    String returningString = Convert.ToStrin g(99);
    Byte[] returningByte =
    System.Text.Enc oding.ASCII.Get Bytes(returning String.ToCharAr ray());

    //Returning a confirmation back to the client.
    soTcp.Send(retu rningByte, returningByte.L ength, 0);
    }
    DateTime endTime = System.DateTime .Now;
    Console.WriteLi ne(endTime - beginTime);
    }
    }
    }

    The Client Process
    ---------------------------------------------------------------------
    using System;
    using System.Net;
    using System.Net.Sock ets;
    using System.IO;
    using System.Runtime. Serialization.F ormatters.Binar y;
    using Messages;

    namespace tcpClient
    {
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    ///



    class Class1
    {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
    int port = 7627;
    int N = 100000;
    int i = 0;

    TcpClient tcpClient = new TcpClient("mach ine1", port);
    NetworkStream tcpStream = tcpClient.GetSt ream();
    BinaryWriter bWriter = new BinaryWriter(tc pStream);
    BinaryFormatter bF = new BinaryFormatter ();

    while (i < N)
    {
    ++i;

    Messages.Msg m = new Messages.Msg();
    m.msgType_ = 101;
    m.bidPrice_ = 123.32f;
    m.bidSize_ = 100;
    m.eqiRole_ = 1;
    m.liquidityBidP rice_ = 122.12f;
    m.liquidityOffe rPrice_ = 192.32f;
    m.offerPrice_ = 154.25f;
    m.quoteId_ = i;
    m.responseLevel _ = 10;
    m.symbol_ = "IBM";
    bF.Serialize(tc pStream, m);

    // Read back the Ack
    Byte[] received = new Byte[1024];
    tcpStream.Read( received, 0, received.Length );

    }
    }
    }
    }




    The C++ binary streams method was to simply simulate the above but no
    serialization involved - just send the byte stream of the class via
    TCP/IP sockets.

    If .NET Remoting / Serialization is so slow, why would anyone ever use
    it over C++ for performance critical applications that transmit
    hundreds of thousands of messages per day ? Is the tradeoff really
    worth it ? What are people's thoughts ?

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: .NET Remoting/Serialization way too slow vs C++ binary/tcp

    Remoting is different from just shooting the contents of a class over
    the wire. With remoting, you have method calls that are taking place, sinks
    that are manipulating the call before it gets to the call site (on both
    ends, etc, etc). This definitely contributes to the overall time it takes
    to make a call over remoting.

    Also, with serialization, you get that out of the box, with little or no
    modification to your class (with the exception of the Serializable
    attribute). In C++, I've seen some pretty contrived ways of serializing
    information to send over the wire. Serialization really shines in .NET when
    you have large object graphs that need to be serialized.

    Your example class is somewhat simplistic. Granted, it could be all you
    ever send, but in more complex situations, I think that the C++ solution
    will become difficult to implement (I could be very wrong here, granted).

    In the end though, you have to measure what your needs are. If
    processing 500K methods in 1459 seconds (an average of 342 calls a second)
    is not sufficient, then don't use it. However, if you only need to process
    say, 40 operations a second, then this would be more than sufficient, and I
    would take the .NET solution because it would be much easier to implement.

    Hope this helps.

    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    <ajou_king@yaho o.com> wrote in message
    news:1110898272 .233605.189020@ o13g2000cwo.goo glegroups.com.. .[color=blue]
    >I was running some tests on my Win32 1GHZ processor to see how long it
    > would take to transmit objects numerous times via TCP/IP using C#
    > .NET Remoting vs the C++ trustworthy method of binary streams. I ran
    > the test for 50K, 100K, 500K iterations, where each iteration consists
    > of sending an object from a client process to a server process, and the
    > server process sends back an ack.
    > Here are the results:
    >
    > .NET Remoting C++ Binary TCP/IP
    > -------------- ------------------
    > 50,000 Iterations: 128 seconds 3 seconds
    > 100,000 Iterations: 300 seconds 8 seconds
    > 500,000 Iterations: 1459 seconds 43 seconds
    >
    > In the above tests the .NET remoting overhead was 42.6x, 37.5x, and
    > 33.9x slower than the c++ version.
    >
    > Here is the object that was used:
    > ---------------------------------------------------------------
    > [Serializable]
    > public class Msg
    > {
    > public int msgType_;
    > public int seqNum_;
    > public String symbol_;
    > public int quoteId_;
    > public int responseLevel_;
    > public int eqiRole_;
    > public float bidPrice_;
    > public float offerPrice_;
    > public int bidSize_;
    > public int offerSize_;
    > public float liquidityBidPri ce_;
    > public float liquidityOfferP rice_;
    > public int liquidityBidSiz e_;
    > public int liquidityOfferS ize_;
    > public int checkSum_;
    > }
    >
    > The Server Process:
    > ------------------------------------------------------
    > using System;
    > using Messages;
    >
    >
    > namespace tcpServer
    > {
    >
    > using System;
    > using System.Net;
    > using System.Net.Sock ets;
    > using System.Runtime. Serialization.F ormatters.Binar y;
    > using System.IO;
    >
    >
    > /// <summary>
    > /// Summary description for Class1.
    > /// </summary>
    > class Class1
    > {
    > /// <summary>
    > /// The main entry point for the application.
    > /// </summary>
    > [STAThread]
    > static void Main(string[] args)
    > {
    > int port = 7627;
    > int N = 50000;
    > int i = 0;
    >
    > BinaryFormatter bF = new BinaryFormatter ();
    >
    > TcpListener tcpListener = new TcpListener(por t);
    > tcpListener.Sta rt();
    > Socket soTcp = tcpListener.Acc eptSocket();
    > Console.WriteLi ne("SampleClien t is connected through TCP.");
    > NetworkStream stream = new NetworkStream(s oTcp,
    > FileAccess.Read Write, true);
    > BinaryReader bReader = new BinaryReader(st ream);
    >
    > DateTime beginTime = new DateTime();
    >
    > while (i < N)
    > {
    > if (i == 0)
    > {
    > beginTime = System.DateTime .Now;
    > }
    >
    > ++i;
    > Byte[] received = new Byte[1024];
    > Messages.Msg msg = new Messages.Msg();
    > msg = (Messages.Msg)b F.Deserialize(s tream);
    > String returningString = Convert.ToStrin g(99);
    > Byte[] returningByte =
    > System.Text.Enc oding.ASCII.Get Bytes(returning String.ToCharAr ray());
    >
    > //Returning a confirmation back to the client.
    > soTcp.Send(retu rningByte, returningByte.L ength, 0);
    > }
    > DateTime endTime = System.DateTime .Now;
    > Console.WriteLi ne(endTime - beginTime);
    > }
    > }
    > }
    >
    > The Client Process
    > ---------------------------------------------------------------------
    > using System;
    > using System.Net;
    > using System.Net.Sock ets;
    > using System.IO;
    > using System.Runtime. Serialization.F ormatters.Binar y;
    > using Messages;
    >
    > namespace tcpClient
    > {
    > /// <summary>
    > /// Summary description for Class1.
    > /// </summary>
    > ///
    >
    >
    >
    > class Class1
    > {
    > /// <summary>
    > /// The main entry point for the application.
    > /// </summary>
    > [STAThread]
    > static void Main(string[] args)
    > {
    > int port = 7627;
    > int N = 100000;
    > int i = 0;
    >
    > TcpClient tcpClient = new TcpClient("mach ine1", port);
    > NetworkStream tcpStream = tcpClient.GetSt ream();
    > BinaryWriter bWriter = new BinaryWriter(tc pStream);
    > BinaryFormatter bF = new BinaryFormatter ();
    >
    > while (i < N)
    > {
    > ++i;
    >
    > Messages.Msg m = new Messages.Msg();
    > m.msgType_ = 101;
    > m.bidPrice_ = 123.32f;
    > m.bidSize_ = 100;
    > m.eqiRole_ = 1;
    > m.liquidityBidP rice_ = 122.12f;
    > m.liquidityOffe rPrice_ = 192.32f;
    > m.offerPrice_ = 154.25f;
    > m.quoteId_ = i;
    > m.responseLevel _ = 10;
    > m.symbol_ = "IBM";
    > bF.Serialize(tc pStream, m);
    >
    > // Read back the Ack
    > Byte[] received = new Byte[1024];
    > tcpStream.Read( received, 0, received.Length );
    >
    > }
    > }
    > }
    > }
    >
    >
    >
    >
    > The C++ binary streams method was to simply simulate the above but no
    > serialization involved - just send the byte stream of the class via
    > TCP/IP sockets.
    >
    > If .NET Remoting / Serialization is so slow, why would anyone ever use
    > it over C++ for performance critical applications that transmit
    > hundreds of thousands of messages per day ? Is the tradeoff really
    > worth it ? What are people's thoughts ?
    >[/color]


    Comment

    • Dave

      #3
      .NET Remoting/Serialization way too slow vs C++ binary/tcp

      This doesn't sound right. First, a terminology point.
      This isn't remoting b/c you're not using the remoting
      classes (RemotingConfig uration, ChannelServices , etc.).
      But it IS serialization and I would expect it to be doing
      this.

      One item that jumps out at me is the memory allocation.
      Your server is creating memory in every iteration. Of
      course, if you're doing that in native C++, it's a fair
      comparison.

      Could you post the C++ as well?
      [color=blue]
      >-----Original Message-----
      >I was running some tests on my Win32 1GHZ processor to[/color]
      see how long it[color=blue]
      >would take to transmit objects numerous times via[/color]
      TCP/IP using C#[color=blue]
      >..NET Remoting vs the C++ trustworthy method of binary[/color]
      streams. I ran[color=blue]
      >the test for 50K, 100K, 500K iterations, where each[/color]
      iteration consists[color=blue]
      >of sending an object from a client process to a server[/color]
      process, and the[color=blue]
      >server process sends back an ack.
      >Here are the results:
      >
      > .NET Remoting C++[/color]
      Binary TCP/IP[color=blue]
      > -------------- -------[/color]
      -----------[color=blue]
      >50,000 Iterations: 128 seconds 3[/color]
      seconds[color=blue]
      >100,000 Iterations: 300 seconds 8[/color]
      seconds[color=blue]
      >500,000 Iterations: 1459 seconds 43[/color]
      seconds[color=blue]
      >
      >In the above tests the .NET remoting overhead was 42.6x,[/color]
      37.5x, and[color=blue]
      >33.9x slower than the c++ version.
      >
      >Here is the object that was used:
      >----------------------------------------------------------[/color]
      -----[color=blue]
      > [Serializable]
      > public class Msg
      > {
      > public int msgType_;
      > public int seqNum_;
      > public String symbol_;
      > public int quoteId_;
      > public int responseLevel_;
      > public int eqiRole_;
      > public float bidPrice_;
      > public float offerPrice_;
      > public int bidSize_;
      > public int offerSize_;
      > public float liquidityBidPri ce_;
      > public float liquidityOfferP rice_;
      > public int liquidityBidSiz e_;
      > public int liquidityOfferS ize_;
      > public int checkSum_;
      >}
      >
      >The Server Process:
      >------------------------------------------------------
      >using System;
      >using Messages;
      >
      >
      >namespace tcpServer
      >{
      >
      > using System;
      > using System.Net;
      > using System.Net.Sock ets;
      > using[/color]
      System.Runtime. Serialization.F ormatters.Binar y;[color=blue]
      > using System.IO;
      >
      >
      > /// <summary>
      > /// Summary description for Class1.
      > /// </summary>
      > class Class1
      > {
      > /// <summary>
      > /// The main entry point for the[/color]
      application.[color=blue]
      > /// </summary>
      > [STAThread]
      > static void Main(string[] args)
      > {
      > int port = 7627;
      > int N = 50000;
      > int i = 0;
      >
      > BinaryFormatter bF = new[/color]
      BinaryFormatter ();[color=blue]
      >
      > TcpListener tcpListener = new[/color]
      TcpListener(por t);[color=blue]
      > tcpListener.Sta rt();
      > Socket soTcp =[/color]
      tcpListener.Acc eptSocket();[color=blue]
      > Console.WriteLi ne("SampleClien t is[/color]
      connected through TCP.");[color=blue]
      > NetworkStream stream = new[/color]
      NetworkStream(s oTcp,[color=blue]
      >FileAccess.Rea dWrite, true);
      > BinaryReader bReader = new[/color]
      BinaryReader(st ream);[color=blue]
      >
      > DateTime beginTime = new DateTime[/color]
      ();[color=blue]
      >
      > while (i < N)
      > {
      > if (i == 0)
      > {
      > beginTime =[/color]
      System.DateTime .Now;[color=blue]
      > }
      >
      > ++i;
      > Byte[] received = new Byte[/color]
      [1024];[color=blue]
      > Messages.Msg msg = new[/color]
      Messages.Msg();[color=blue]
      > msg = (Messages.Msg)[/color]
      bF.Deserialize( stream);[color=blue]
      > String returningString =[/color]
      Convert.ToStrin g(99);[color=blue]
      > Byte[] returningByte =
      >System.Text.En coding.ASCII.Ge tBytes[/color]
      (returningStrin g.ToCharArray() );[color=blue]
      >
      > //Returning a[/color]
      confirmation back to the client.[color=blue]
      > soTcp.Send(retu rningByte,[/color]
      returningByte.L ength, 0);[color=blue]
      > }
      > DateTime endTime =[/color]
      System.DateTime .Now;[color=blue]
      > Console.WriteLi ne(endTime -[/color]
      beginTime);[color=blue]
      > }
      > }
      >}
      >
      >The Client Process
      >----------------------------------------------------------[/color]
      -----------[color=blue]
      >using System;
      >using System.Net;
      >using System.Net.Sock ets;
      >using System.IO;
      >using System.Runtime. Serialization.F ormatters.Binar y;
      >using Messages;
      >
      >namespace tcpClient
      >{
      > /// <summary>
      > /// Summary description for Class1.
      > /// </summary>
      > ///
      >
      >
      >
      > class Class1
      > {
      > /// <summary>
      > /// The main entry point for the[/color]
      application.[color=blue]
      > /// </summary>
      > [STAThread]
      > static void Main(string[] args)
      > {
      > int port = 7627;
      > int N = 100000;
      > int i = 0;
      >
      > TcpClient tcpClient = new TcpClient[/color]
      ("machine1", port);[color=blue]
      > NetworkStream tcpStream =[/color]
      tcpClient.GetSt ream();[color=blue]
      > BinaryWriter bWriter = new[/color]
      BinaryWriter(tc pStream);[color=blue]
      > BinaryFormatter bF = new[/color]
      BinaryFormatter ();[color=blue]
      >
      > while (i < N)
      > {
      > ++i;
      >
      > Messages.Msg m =[/color]
      new Messages.Msg();[color=blue]
      > m.msgType_ = 101;
      > m.bidPrice_ =[/color]
      123.32f;[color=blue]
      > m.bidSize_ = 100;
      > m.eqiRole_ = 1;
      >[/color]
      m.liquidityBidP rice_ = 122.12f;[color=blue]
      >[/color]
      m.liquidityOffe rPrice_ = 192.32f;[color=blue]
      > m.offerPrice_ =[/color]
      154.25f;[color=blue]
      > m.quoteId_ = i;
      > m.responseLevel _ =[/color]
      10;[color=blue]
      > m.symbol_ = "IBM";
      > bF.Serialize[/color]
      (tcpStream, m);[color=blue]
      >
      > // Read back the[/color]
      Ack[color=blue]
      > Byte[] received =[/color]
      new Byte[1024];[color=blue]
      > tcpStream.Read[/color]
      (received, 0, received.Length );[color=blue]
      >
      > }
      > }
      > }
      >}
      >
      >
      >
      >
      >The C++ binary streams method was to simply simulate the[/color]
      above but no[color=blue]
      >serializatio n involved - just send the byte stream of the[/color]
      class via[color=blue]
      >TCP/IP sockets.
      >
      >If .NET Remoting / Serialization is so slow, why would[/color]
      anyone ever use[color=blue]
      >it over C++ for performance critical applications that[/color]
      transmit[color=blue]
      >hundreds of thousands of messages per day ? Is the[/color]
      tradeoff really[color=blue]
      >worth it ? What are people's thoughts ?
      >
      >.
      >[/color]

      Comment

      • Pete Davis

        #4
        Re: .NET Remoting/Serialization way too slow vs C++ binary/tcp

        > If .NET Remoting / Serialization is so slow, why would anyone ever use[color=blue]
        > it over C++ for performance critical applications that transmit
        > hundreds of thousands of messages per day ? Is the tradeoff really
        > worth it ? What are people's thoughts ?
        >[/color]

        If you're only doing hundreds of thousands per day, then the fact that it
        takes 5 minutes to handle 100,000 iterations means that you could handle
        nearly 29 million iterations every 24 hours.

        But why not just do the same thing in C# that you're doing in C++ if
        performance is critical? That's the tradeoff. If you want performance, open
        a socket, pack the data in a byte array and send it out. The serialization
        stuff in .NET is necessarily slow because it requires reflection. I can't
        really speak for the remoting stuff as I haven't had a need to use it. If
        performance is an issue, serialization isn't your friend.

        Pete


        Comment

        • Dave P.

          #5
          Re: .NET Remoting/Serialization way too slow vs C++ binary/tcp

          Also, serialization provides for hardware independence. Presumably, with
          C++, you're not doing anything to protect against one machine using
          big-endian and another using little-endian. With .NET serialization, that's
          taken care of for you.

          I didn't say it before, and I may be in the minority in saying it, but I
          don't think this is an unreasonable volume for transmission. The two
          methods should not be this far apart. If they are, then I agree that it's
          unacceptable. Remoting has many benefits, but ease-of-use isn't one of
          them, IMO. (if you didn't care about performance, you'd be using SOAP,
          because that's ridiculously easy).

          I'm really interested to see how this turns out. If you can post the C++
          code, it will be really helpful for a comparative analysis.

          Dave


          "Pete Davis" <pdavis68@NOSPA M.hotmail.com> wrote in message
          news:BMmdnfILsb zunqrfRVn-qg@giganews.com ...[color=blue][color=green]
          > > If .NET Remoting / Serialization is so slow, why would anyone ever use
          > > it over C++ for performance critical applications that transmit
          > > hundreds of thousands of messages per day ? Is the tradeoff really
          > > worth it ? What are people's thoughts ?
          > >[/color]
          >
          > If you're only doing hundreds of thousands per day, then the fact that it
          > takes 5 minutes to handle 100,000 iterations means that you could handle
          > nearly 29 million iterations every 24 hours.
          >
          > But why not just do the same thing in C# that you're doing in C++ if
          > performance is critical? That's the tradeoff. If you want performance,[/color]
          open[color=blue]
          > a socket, pack the data in a byte array and send it out. The serialization
          > stuff in .NET is necessarily slow because it requires reflection. I can't
          > really speak for the remoting stuff as I haven't had a need to use it. If
          > performance is an issue, serialization isn't your friend.
          >
          > Pete
          >
          >[/color]


          Comment

          • Dave P.

            #6
            Re: .NET Remoting/Serialization way too slow vs C++ binary/tcp

            Another thought:
            Try running this through the JetBrains and CLR profilers to see where the
            CPU cycles and memory allocations look like.

            Dave

            <ajou_king@yaho o.com> wrote in message
            news:1110898272 .233605.189020@ o13g2000cwo.goo glegroups.com.. .[color=blue]
            > I was running some tests on my Win32 1GHZ processor to see how long it
            > would take to transmit objects numerous times via TCP/IP using C#
            > .NET Remoting vs the C++ trustworthy method of binary streams. I ran
            > the test for 50K, 100K, 500K iterations, where each iteration consists
            > of sending an object from a client process to a server process, and the
            > server process sends back an ack.
            > Here are the results:
            >
            > .NET Remoting C++ Binary TCP/IP
            > -------------- ------------------
            > 50,000 Iterations: 128 seconds 3 seconds
            > 100,000 Iterations: 300 seconds 8 seconds
            > 500,000 Iterations: 1459 seconds 43 seconds
            >
            > In the above tests the .NET remoting overhead was 42.6x, 37.5x, and
            > 33.9x slower than the c++ version.
            >
            > Here is the object that was used:
            > ---------------------------------------------------------------
            > [Serializable]
            > public class Msg
            > {
            > public int msgType_;
            > public int seqNum_;
            > public String symbol_;
            > public int quoteId_;
            > public int responseLevel_;
            > public int eqiRole_;
            > public float bidPrice_;
            > public float offerPrice_;
            > public int bidSize_;
            > public int offerSize_;
            > public float liquidityBidPri ce_;
            > public float liquidityOfferP rice_;
            > public int liquidityBidSiz e_;
            > public int liquidityOfferS ize_;
            > public int checkSum_;
            > }
            >
            > The Server Process:
            > ------------------------------------------------------
            > using System;
            > using Messages;
            >
            >
            > namespace tcpServer
            > {
            >
            > using System;
            > using System.Net;
            > using System.Net.Sock ets;
            > using System.Runtime. Serialization.F ormatters.Binar y;
            > using System.IO;
            >
            >
            > /// <summary>
            > /// Summary description for Class1.
            > /// </summary>
            > class Class1
            > {
            > /// <summary>
            > /// The main entry point for the application.
            > /// </summary>
            > [STAThread]
            > static void Main(string[] args)
            > {
            > int port = 7627;
            > int N = 50000;
            > int i = 0;
            >
            > BinaryFormatter bF = new BinaryFormatter ();
            >
            > TcpListener tcpListener = new TcpListener(por t);
            > tcpListener.Sta rt();
            > Socket soTcp = tcpListener.Acc eptSocket();
            > Console.WriteLi ne("SampleClien t is connected through TCP.");
            > NetworkStream stream = new NetworkStream(s oTcp,
            > FileAccess.Read Write, true);
            > BinaryReader bReader = new BinaryReader(st ream);
            >
            > DateTime beginTime = new DateTime();
            >
            > while (i < N)
            > {
            > if (i == 0)
            > {
            > beginTime = System.DateTime .Now;
            > }
            >
            > ++i;
            > Byte[] received = new Byte[1024];
            > Messages.Msg msg = new Messages.Msg();
            > msg = (Messages.Msg)b F.Deserialize(s tream);
            > String returningString = Convert.ToStrin g(99);
            > Byte[] returningByte =
            > System.Text.Enc oding.ASCII.Get Bytes(returning String.ToCharAr ray());
            >
            > //Returning a confirmation back to the client.
            > soTcp.Send(retu rningByte, returningByte.L ength, 0);
            > }
            > DateTime endTime = System.DateTime .Now;
            > Console.WriteLi ne(endTime - beginTime);
            > }
            > }
            > }
            >
            > The Client Process
            > ---------------------------------------------------------------------
            > using System;
            > using System.Net;
            > using System.Net.Sock ets;
            > using System.IO;
            > using System.Runtime. Serialization.F ormatters.Binar y;
            > using Messages;
            >
            > namespace tcpClient
            > {
            > /// <summary>
            > /// Summary description for Class1.
            > /// </summary>
            > ///
            >
            >
            >
            > class Class1
            > {
            > /// <summary>
            > /// The main entry point for the application.
            > /// </summary>
            > [STAThread]
            > static void Main(string[] args)
            > {
            > int port = 7627;
            > int N = 100000;
            > int i = 0;
            >
            > TcpClient tcpClient = new TcpClient("mach ine1", port);
            > NetworkStream tcpStream = tcpClient.GetSt ream();
            > BinaryWriter bWriter = new BinaryWriter(tc pStream);
            > BinaryFormatter bF = new BinaryFormatter ();
            >
            > while (i < N)
            > {
            > ++i;
            >
            > Messages.Msg m = new Messages.Msg();
            > m.msgType_ = 101;
            > m.bidPrice_ = 123.32f;
            > m.bidSize_ = 100;
            > m.eqiRole_ = 1;
            > m.liquidityBidP rice_ = 122.12f;
            > m.liquidityOffe rPrice_ = 192.32f;
            > m.offerPrice_ = 154.25f;
            > m.quoteId_ = i;
            > m.responseLevel _ = 10;
            > m.symbol_ = "IBM";
            > bF.Serialize(tc pStream, m);
            >
            > // Read back the Ack
            > Byte[] received = new Byte[1024];
            > tcpStream.Read( received, 0, received.Length );
            >
            > }
            > }
            > }
            > }
            >
            >
            >
            >
            > The C++ binary streams method was to simply simulate the above but no
            > serialization involved - just send the byte stream of the class via
            > TCP/IP sockets.
            >
            > If .NET Remoting / Serialization is so slow, why would anyone ever use
            > it over C++ for performance critical applications that transmit
            > hundreds of thousands of messages per day ? Is the tradeoff really
            > worth it ? What are people's thoughts ?
            >[/color]


            Comment

            • ajou_king@yahoo.com

              #7
              Re: .NET Remoting/Serialization way too slow vs C++ binary/tcp

              Here is the C++ code - It uses the Ace Toolkit for the Network layer
              socket encapsulatation .

              Client
              ----------------------------------------------------------------------------
              /////////////////////////////////////////////////////////////////////////////////
              // BINARY TEST C++
              //////////////////////////////////////////////////////////////////////////////////
              int
              binaryStructTes t()
              {
              struct MsgStruct
              {
              int msgType_;
              int seqNum_;
              char symbol_[5];
              int quoteId_;
              int responseLevel_;
              int eqiRole_;
              float bidPrice_;
              float offerPrice_;
              int bidSize_;
              int offerSize_;
              float liquidityBidPri ce_;
              float liquidityOfferP rice_;
              int liquidityBidSiz e_;
              int liquidityOfferS ize_;
              char text_[512];
              int checkSum_;

              MsgStruct()
              {
              memset(this, NULL, sizeof(MsgStruc t));
              msgType_ = 92;
              seqNum_ = 0;
              strcpy(symbol_, "IBM");
              bidPrice_ = 100.12; offerPrice_ = 101.21;
              }
              };


              ACE_SOCK_Stream clientStream;
              ACE_INET_Addr remoteAddr(remo tePort, "machine1") ;
              ACE_SOCK_Connec tor connector;

              if (connector.conn ect(clientStrea m, remoteAddr) == -1)
              {
              cout << "Failed to connect" << endl;
              exit(-1);
              }

              for (int i=0; i<N; ++i)
              {
              MsgStruct msg;
              msg.msgType_ = 101;
              msg.bidPrice_ = 123.32;
              msg.bidSize_ = 100;
              msg.eqiRole_ = 1;
              msg.liquidityBi dPrice_ = 122.12;
              msg.liquidityOf ferPrice_ = 192.32;
              msg.offerPrice_ = 154.25;
              msg.quoteId_ = i;
              msg.responseLev el_ = 10;
              strcpy(msg.symb ol_, "IBM");


              int n=0;
              if ((n = clientStream.se nd(&msg, sizeof(msg))) == -1)
              {
              cout << "Failed to send " << endl;
              exit(-1);
              }

              // wait for response now
              int response;
              clientStream.re cv(&response, sizeof(response ));

              }

              clientStream.cl ose();

              return 0;
              }



              Server
              ----------------------------------------------------------------------------------------------------------
              /////////////////////////////////////////////////////////////////////////////////////
              // BINARY TEST
              ///////////////////////////////////////////////////////////////////////////////////////
              int binaryStructTes t()
              {
              struct MsgStruct
              {
              int msgType_;
              int seqNum_;
              char symbol_[5];
              int quoteId_;
              int responseLevel_;
              int eqiRole_;
              float bidPrice_;
              float offerPrice_;
              int bidSize_;
              int offerSize_;
              float liquidityBidPri ce_;
              float liquidityOfferP rice_;
              int liquidityBidSiz e_;
              int liquidityOfferS ize_;
              char text_[512];
              int checkSum_;
              };

              ACE_INET_Addr serverAddr(5666 ), clientAddr;
              ACE_SOCK_Accept or peerAcceptor(se rverAddr);

              if (peerAcceptor.g et_local_addr(s erverAddr) == -1)
              {
              cout << "Failed in get_local_addr( )" << endl;
              exit(-1);
              }

              ACE_SOCK_Stream newStream;
              if (peerAcceptor.a ccept(newStream , &clientAddr) == -1)
              {
              cout << "Failed in accept()" << endl;
              }

              time_t t;
              int numMsgs = 0;
              while (true)
              {
              MsgStruct msg;
              int bytes = newStream.recv_ n(&msg, sizeof(msg));
              ++numMsgs;
              if (bytes == 0)
              break;

              // send back response
              int response = msg.seqNum_;
              newStream.send( &response, sizeof(response ));

              if (numMsgs == 1)
              t = time(NULL);
              else if (numMsgs >= N)
              {
              break;
              }
              }


              time_t t2 = time(NULL);
              cout << "Msgs received: " << numMsgs << endl;
              cout << "TIME: " << t2 - t << endl;
              return 0;
              }




              So the code as you can see is straight forward . And my belief is that
              adding to the size of the class object (more fields/attributes) will
              furthermore add extra overhead to the .NET serialization process via
              reflection - making the difference even more so greater.

              Comment

              • Dave P.

                #8
                Re: .NET Remoting/Serialization way too slow vs C++ binary/tcp

                I see a big difference in memory allocation. For both client and server,
                you declare the MsgStruct inside the loop, but in C++ that causes one
                allocation (during the first iteration). You are repeatedly using the same
                memory on the stack instead of allocating memory from the heap for each
                iteration. In the .NET code, you're creating a new object every iteration,
                which has to allocate memory from the heap. The C++ is reusing the same
                memory over and over, but the .NET code has to allocate (and later clean up)
                memory for each iteration.

                I suggest performing a new/delete every iteration in the C++ code for the
                sake of symmetry. Alternatively, you could change the .NET code to reuse
                the same object every time.

                Dave

                <ajou_king@yaho o.com> wrote in message
                news:1110903944 .117518.49350@o 13g2000cwo.goog legroups.com...[color=blue]
                > Here is the C++ code - It uses the Ace Toolkit for the Network layer
                > socket encapsulatation .
                >
                > Client
                > --------------------------------------------------------------------------[/color]
                --[color=blue]
                >[/color]
                ////////////////////////////////////////////////////////////////////////////
                /////[color=blue]
                > // BINARY TEST C++
                >[/color]
                ////////////////////////////////////////////////////////////////////////////
                //////[color=blue]
                > int
                > binaryStructTes t()
                > {
                > struct MsgStruct
                > {
                > int msgType_;
                > int seqNum_;
                > char symbol_[5];
                > int quoteId_;
                > int responseLevel_;
                > int eqiRole_;
                > float bidPrice_;
                > float offerPrice_;
                > int bidSize_;
                > int offerSize_;
                > float liquidityBidPri ce_;
                > float liquidityOfferP rice_;
                > int liquidityBidSiz e_;
                > int liquidityOfferS ize_;
                > char text_[512];
                > int checkSum_;
                >
                > MsgStruct()
                > {
                > memset(this, NULL, sizeof(MsgStruc t));
                > msgType_ = 92;
                > seqNum_ = 0;
                > strcpy(symbol_, "IBM");
                > bidPrice_ = 100.12; offerPrice_ = 101.21;
                > }
                > };
                >
                >
                > ACE_SOCK_Stream clientStream;
                > ACE_INET_Addr remoteAddr(remo tePort, "machine1") ;
                > ACE_SOCK_Connec tor connector;
                >
                > if (connector.conn ect(clientStrea m, remoteAddr) == -1)
                > {
                > cout << "Failed to connect" << endl;
                > exit(-1);
                > }
                >
                > for (int i=0; i<N; ++i)
                > {
                > MsgStruct msg;
                > msg.msgType_ = 101;
                > msg.bidPrice_ = 123.32;
                > msg.bidSize_ = 100;
                > msg.eqiRole_ = 1;
                > msg.liquidityBi dPrice_ = 122.12;
                > msg.liquidityOf ferPrice_ = 192.32;
                > msg.offerPrice_ = 154.25;
                > msg.quoteId_ = i;
                > msg.responseLev el_ = 10;
                > strcpy(msg.symb ol_, "IBM");
                >
                >
                > int n=0;
                > if ((n = clientStream.se nd(&msg, sizeof(msg))) == -1)
                > {
                > cout << "Failed to send " << endl;
                > exit(-1);
                > }
                >
                > // wait for response now
                > int response;
                > clientStream.re cv(&response, sizeof(response ));
                >
                > }
                >
                > clientStream.cl ose();
                >
                > return 0;
                > }
                >
                >
                >
                > Server
                > --------------------------------------------------------------------------[/color]
                --------------------------------[color=blue]
                >[/color]
                ////////////////////////////////////////////////////////////////////////////
                /////////[color=blue]
                > // BINARY TEST
                >[/color]
                ////////////////////////////////////////////////////////////////////////////
                ///////////[color=blue]
                > int binaryStructTes t()
                > {
                > struct MsgStruct
                > {
                > int msgType_;
                > int seqNum_;
                > char symbol_[5];
                > int quoteId_;
                > int responseLevel_;
                > int eqiRole_;
                > float bidPrice_;
                > float offerPrice_;
                > int bidSize_;
                > int offerSize_;
                > float liquidityBidPri ce_;
                > float liquidityOfferP rice_;
                > int liquidityBidSiz e_;
                > int liquidityOfferS ize_;
                > char text_[512];
                > int checkSum_;
                > };
                >
                > ACE_INET_Addr serverAddr(5666 ), clientAddr;
                > ACE_SOCK_Accept or peerAcceptor(se rverAddr);
                >
                > if (peerAcceptor.g et_local_addr(s erverAddr) == -1)
                > {
                > cout << "Failed in get_local_addr( )" << endl;
                > exit(-1);
                > }
                >
                > ACE_SOCK_Stream newStream;
                > if (peerAcceptor.a ccept(newStream , &clientAddr) == -1)
                > {
                > cout << "Failed in accept()" << endl;
                > }
                >
                > time_t t;
                > int numMsgs = 0;
                > while (true)
                > {
                > MsgStruct msg;
                > int bytes = newStream.recv_ n(&msg, sizeof(msg));
                > ++numMsgs;
                > if (bytes == 0)
                > break;
                >
                > // send back response
                > int response = msg.seqNum_;
                > newStream.send( &response, sizeof(response ));
                >
                > if (numMsgs == 1)
                > t = time(NULL);
                > else if (numMsgs >= N)
                > {
                > break;
                > }
                > }
                >
                >
                > time_t t2 = time(NULL);
                > cout << "Msgs received: " << numMsgs << endl;
                > cout << "TIME: " << t2 - t << endl;
                > return 0;
                > }
                >
                >
                >
                >
                > So the code as you can see is straight forward . And my belief is that
                > adding to the size of the class object (more fields/attributes) will
                > furthermore add extra overhead to the .NET serialization process via
                > reflection - making the difference even more so greater.
                >[/color]


                Comment

                • ajou_king@yahoo.com

                  #9
                  Re: .NET Remoting/Serialization way too slow vs C++ binary/tcp

                  In C++, that MsgStruct object as you said is allocated on the stack,
                  BUT it happens for every iteration. If it were allocated as a static
                  variable, then it would only be allocated once. But in this case it is
                  being allocated/dellocated on the stack (not the same memory location).


                  Just to go along with your idea, though - I changed the allocation
                  inside the loop to be on the heap via explicit "new MsgStruct()" and
                  "delete msg" inside the loop, to see how much extra penalty would be
                  incurred in the C++ version. And the result is very minimal :
                  For 500,000 iterations it took 45 seconds now (43 seconds before), but
                  still way better than the 1459 seconds for the .NET version.

                  Perhaps there is a way to optimize the .NET version via the
                  compiler.... Otherwise, the penalty of an interpreted environment,
                  reflection overhead for serialization, and overhead of garbage
                  collection is just too much, and cannot be recommended for real-time
                  systems.

                  Comment

                  • ajou_king@yahoo.com

                    #10
                    Re: .NET Remoting/Serialization way too slow vs C++ binary/tcp

                    Another interesting test. I added Java Serialization into the mix,
                    basically I ported the C# code to Java - same logic, but the results
                    are much in better, in favor of Java.

                    The Java Serialization results:
                    For 50,000 Iterations: 19 seconds
                    For 100,000 Iterations: 46 seconds
                    For 500,000 Iterations: 250 seconds

                    That averages to about 6x slower than binary C++ version, but __MUCH__
                    better than the .NET version which averaged about 35x slower.

                    Why is .NET's serialization/processing so much worse than Java's ?


                    Here is the Java Code:
                    Server
                    -------------------------------------------------------------------------------------------
                    class tcpServer
                    {
                    public static void main(String[] args)
                    {
                    tcpServer server = new tcpServer();
                    server.run(1000 00);
                    }

                    void run(int N)
                    {
                    try
                    {
                    ServerSocket listener = new ServerSocket(99 91);
                    Socket socket = listener.accept ();
                    DataInputStream dis = new
                    DataInputStream (socket.getInpu tStream());
                    DataOutputStrea m dos = new
                    DataOutputStrea m(socket.getOut putStream());
                    ObjectInputStre am ois = new ObjectInputStre am(dis);
                    ObjectOutputStr eam oos = new ObjectOutputStr eam(dos);

                    int i = 0;
                    while (i < N)
                    {
                    ++i;
                    Msg obj = (Msg)ois.readOb ject();

                    // reply
                    Integer ii = new Integer(i);
                    oos.writeObject (ii);

                    }


                    }
                    catch (Exception e)
                    {
                    e.printStackTra ce();
                    }


                    }

                    }

                    Client
                    --------------------------------------------------------------------------------------------
                    class tcpClient
                    {
                    public static void main(String[] args)
                    {
                    tcpClient client = new tcpClient();
                    client.run(1000 00);
                    }

                    void run(int N)
                    {
                    try
                    {

                    InetAddress ia = InetAddress.get ByName("machine 1");
                    Socket socket = new Socket(ia, 9991);
                    ObjectOutputStr eam oos = new
                    ObjectOutputStr eam(socket.getO utputStream());


                    DataInputStream dis = new
                    DataInputStream (socket.getInpu tStream());
                    ObjectInputStre am ois = new ObjectInputStre am(dis);

                    int i = 0;
                    Calendar beginTime = null;
                    while (i < N)
                    {
                    if (i == 0)
                    {
                    beginTime = Calendar.getIns tance();
                    }

                    ++i;

                    Msg object = new Msg();
                    object.msgType_ = 101;
                    object.seqNum_ = i;
                    object.symbol_ = "IBM";
                    object.quoteId_ = i;
                    object.response Level_ = 1;
                    object.eqiRole_ = 1;
                    object.bidPrice _ = 100.21f;
                    object.offerPri ce_ = 102.31f;
                    object.bidSize_ = 100; object.offerSiz e_ = 200;
                    oos.writeObject (object);

                    // read ack
                    Integer ii = (Integer)ois.re adObject();


                    }
                    Calendar endTime = Calendar.getIns tance();
                    System.out.prin tln("Time: " +
                    (endTime.getTim eInMillis() - beginTime.getTi meInMillis()) );

                    }
                    catch (Exception e)
                    {
                    e.printStackTra ce();
                    }


                    }

                    }


                    Message Object
                    -------------------------------------------------------------------------
                    public class Msg implements Serializable
                    {
                    public int msgType_;
                    public int seqNum_;
                    public String symbol_;
                    public int quoteId_;
                    public int responseLevel_;
                    public int eqiRole_;
                    public float bidPrice_;
                    public float offerPrice_;
                    public int bidSize_;
                    public int offerSize_;
                    public float liquidityBidPri ce_;
                    public float liquidityOfferP rice_;
                    public int liquidityBidSiz e_;
                    public int liquidityOfferS ize_;
                    public char[] text_ = new char[512];
                    public int checkSum_;

                    Comment

                    • ajou_king@yahoo.com

                      #11
                      Re: .NET Remoting/Serialization way too slow vs C++ binary/tcp

                      Another interesting test. I added Java Serialization into the mix,
                      basically I ported the C# code to Java - same logic, but the results
                      are much in better, in favor of Java.

                      The Java Serialization results:
                      For 50,000 Iterations: 19 seconds
                      For 100,000 Iterations: 46 seconds
                      For 500,000 Iterations: 250 seconds

                      That averages to about 6x slower than binary C++ version, but __MUCH__
                      better than the .NET version which averaged about 35x slower.

                      Why is .NET's serialization/processing so much worse than Java's ?


                      Here is the Java Code:
                      Server
                      -------------------------------------------------------------------------------------------
                      class tcpServer
                      {
                      public static void main(String[] args)
                      {
                      tcpServer server = new tcpServer();
                      server.run(1000 00);
                      }

                      void run(int N)
                      {
                      try
                      {
                      ServerSocket listener = new ServerSocket(99 91);
                      Socket socket = listener.accept ();
                      DataInputStream dis = new
                      DataInputStream (socket.getInpu tStream());
                      DataOutputStrea m dos = new
                      DataOutputStrea m(socket.getOut putStream());
                      ObjectInputStre am ois = new ObjectInputStre am(dis);
                      ObjectOutputStr eam oos = new ObjectOutputStr eam(dos);

                      int i = 0;
                      while (i < N)
                      {
                      ++i;
                      Msg obj = (Msg)ois.readOb ject();

                      // reply
                      Integer ii = new Integer(i);
                      oos.writeObject (ii);

                      }


                      }
                      catch (Exception e)
                      {
                      e.printStackTra ce();
                      }


                      }

                      }

                      Client
                      --------------------------------------------------------------------------------------------
                      class tcpClient
                      {
                      public static void main(String[] args)
                      {
                      tcpClient client = new tcpClient();
                      client.run(1000 00);
                      }

                      void run(int N)
                      {
                      try
                      {

                      InetAddress ia = InetAddress.get ByName("machine 1");
                      Socket socket = new Socket(ia, 9991);
                      ObjectOutputStr eam oos = new
                      ObjectOutputStr eam(socket.getO utputStream());


                      DataInputStream dis = new
                      DataInputStream (socket.getInpu tStream());
                      ObjectInputStre am ois = new ObjectInputStre am(dis);

                      int i = 0;
                      Calendar beginTime = null;
                      while (i < N)
                      {
                      if (i == 0)
                      {
                      beginTime = Calendar.getIns tance();
                      }

                      ++i;

                      Msg object = new Msg();
                      object.msgType_ = 101;
                      object.seqNum_ = i;
                      object.symbol_ = "IBM";
                      object.quoteId_ = i;
                      object.response Level_ = 1;
                      object.eqiRole_ = 1;
                      object.bidPrice _ = 100.21f;
                      object.offerPri ce_ = 102.31f;
                      object.bidSize_ = 100; object.offerSiz e_ = 200;
                      oos.writeObject (object);

                      // read ack
                      Integer ii = (Integer)ois.re adObject();


                      }
                      Calendar endTime = Calendar.getIns tance();
                      System.out.prin tln("Time: " +
                      (endTime.getTim eInMillis() - beginTime.getTi meInMillis()) );

                      }
                      catch (Exception e)
                      {
                      e.printStackTra ce();
                      }


                      }

                      }


                      Message Object
                      -------------------------------------------------------------------------
                      public class Msg implements Serializable
                      {
                      public int msgType_;
                      public int seqNum_;
                      public String symbol_;
                      public int quoteId_;
                      public int responseLevel_;
                      public int eqiRole_;
                      public float bidPrice_;
                      public float offerPrice_;
                      public int bidSize_;
                      public int offerSize_;
                      public float liquidityBidPri ce_;
                      public float liquidityOfferP rice_;
                      public int liquidityBidSiz e_;
                      public int liquidityOfferS ize_;
                      public char[] text_ = new char[512];
                      public int checkSum_;

                      Comment

                      • Willy Denoyette [MVP]

                        #12
                        Re: .NET Remoting/Serialization way too slow vs C++ binary/tcp


                        <ajou_king@yaho o.com> wrote in message
                        news:1110898272 .233605.189020@ o13g2000cwo.goo glegroups.com.. .[color=blue]
                        >I was running some tests on my Win32 1GHZ processor to see how long it
                        > would take to transmit objects numerous times via TCP/IP using C#
                        > .NET Remoting vs the C++ trustworthy method of binary streams. I ran
                        > the test for 50K, 100K, 500K iterations, where each iteration consists
                        > of sending an object from a client process to a server process, and the
                        > server process sends back an ack.
                        > Here are the results:
                        >
                        > .NET Remoting C++ Binary TCP/IP
                        > -------------- ------------------
                        > 50,000 Iterations: 128 seconds 3 seconds
                        > 100,000 Iterations: 300 seconds 8 seconds
                        > 500,000 Iterations: 1459 seconds 43 seconds
                        >
                        > In the above tests the .NET remoting overhead was 42.6x, 37.5x, and
                        > 33.9x slower than the c++ version.
                        >
                        > Here is the object that was used:
                        > ---------------------------------------------------------------
                        > [Serializable]
                        > public class Msg
                        > {
                        > public int msgType_;
                        > public int seqNum_;
                        > public String symbol_;
                        > public int quoteId_;
                        > public int responseLevel_;
                        > public int eqiRole_;
                        > public float bidPrice_;
                        > public float offerPrice_;
                        > public int bidSize_;
                        > public int offerSize_;
                        > public float liquidityBidPri ce_;
                        > public float liquidityOfferP rice_;
                        > public int liquidityBidSiz e_;
                        > public int liquidityOfferS ize_;
                        > public int checkSum_;
                        > }
                        >
                        > The Server Process:
                        > ------------------------------------------------------
                        > using System;
                        > using Messages;
                        >
                        >
                        > namespace tcpServer
                        > {
                        >
                        > using System;
                        > using System.Net;
                        > using System.Net.Sock ets;
                        > using System.Runtime. Serialization.F ormatters.Binar y;
                        > using System.IO;
                        >
                        >
                        > /// <summary>
                        > /// Summary description for Class1.
                        > /// </summary>
                        > class Class1
                        > {
                        > /// <summary>
                        > /// The main entry point for the application.
                        > /// </summary>
                        > [STAThread]
                        > static void Main(string[] args)
                        > {
                        > int port = 7627;
                        > int N = 50000;
                        > int i = 0;
                        >
                        > BinaryFormatter bF = new BinaryFormatter ();
                        >
                        > TcpListener tcpListener = new TcpListener(por t);
                        > tcpListener.Sta rt();
                        > Socket soTcp = tcpListener.Acc eptSocket();
                        > Console.WriteLi ne("SampleClien t is connected through TCP.");
                        > NetworkStream stream = new NetworkStream(s oTcp,
                        > FileAccess.Read Write, true);
                        > BinaryReader bReader = new BinaryReader(st ream);
                        >
                        > DateTime beginTime = new DateTime();
                        >
                        > while (i < N)
                        > {
                        > if (i == 0)
                        > {
                        > beginTime = System.DateTime .Now;
                        > }
                        >
                        > ++i;
                        > Byte[] received = new Byte[1024];
                        > Messages.Msg msg = new Messages.Msg();
                        > msg = (Messages.Msg)b F.Deserialize(s tream);
                        > String returningString = Convert.ToStrin g(99);
                        > Byte[] returningByte =
                        > System.Text.Enc oding.ASCII.Get Bytes(returning String.ToCharAr ray());
                        >
                        > //Returning a confirmation back to the client.
                        > soTcp.Send(retu rningByte, returningByte.L ength, 0);
                        > }
                        > DateTime endTime = System.DateTime .Now;
                        > Console.WriteLi ne(endTime - beginTime);
                        > }
                        > }
                        > }
                        >
                        > The Client Process
                        > ---------------------------------------------------------------------
                        > using System;
                        > using System.Net;
                        > using System.Net.Sock ets;
                        > using System.IO;
                        > using System.Runtime. Serialization.F ormatters.Binar y;
                        > using Messages;
                        >
                        > namespace tcpClient
                        > {
                        > /// <summary>
                        > /// Summary description for Class1.
                        > /// </summary>
                        > ///
                        >
                        >
                        >
                        > class Class1
                        > {
                        > /// <summary>
                        > /// The main entry point for the application.
                        > /// </summary>
                        > [STAThread]
                        > static void Main(string[] args)
                        > {
                        > int port = 7627;
                        > int N = 100000;
                        > int i = 0;
                        >
                        > TcpClient tcpClient = new TcpClient("mach ine1", port);
                        > NetworkStream tcpStream = tcpClient.GetSt ream();
                        > BinaryWriter bWriter = new BinaryWriter(tc pStream);
                        > BinaryFormatter bF = new BinaryFormatter ();
                        >
                        > while (i < N)
                        > {
                        > ++i;
                        >
                        > Messages.Msg m = new Messages.Msg();
                        > m.msgType_ = 101;
                        > m.bidPrice_ = 123.32f;
                        > m.bidSize_ = 100;
                        > m.eqiRole_ = 1;
                        > m.liquidityBidP rice_ = 122.12f;
                        > m.liquidityOffe rPrice_ = 192.32f;
                        > m.offerPrice_ = 154.25f;
                        > m.quoteId_ = i;
                        > m.responseLevel _ = 10;
                        > m.symbol_ = "IBM";
                        > bF.Serialize(tc pStream, m);
                        >
                        > // Read back the Ack
                        > Byte[] received = new Byte[1024];
                        > tcpStream.Read( received, 0, received.Length );
                        >
                        > }
                        > }
                        > }
                        > }
                        >
                        >
                        >
                        >
                        > The C++ binary streams method was to simply simulate the above but no
                        > serialization involved - just send the byte stream of the class via
                        > TCP/IP sockets.
                        >
                        > If .NET Remoting / Serialization is so slow, why would anyone ever use
                        > it over C++ for performance critical applications that transmit
                        > hundreds of thousands of messages per day ? Is the tradeoff really
                        > worth it ? What are people's thoughts ?
                        >[/color]


                        You should never use the BinaryFormatter to serialize an object over a
                        NetworkStream.
                        Or you should use the Remoting infrastructure, or use a MemoryStream to
                        serialize the object to a byte array and send the client the length of the
                        array (as an int) followed by the byte array. The receiving side has to
                        deserialize the byte array back into an object and cast it to the desired
                        object instance.

                        Following class illustrates the process:

                        public class Serializer {
                        public Serializer(){}

                        public byte[] Serialize(objec t o){
                        byte[] buffer;
                        using(MemoryStr eam ms = new MemoryStream())
                        {
                        BinaryFormatter b = new BinaryFormatter ();
                        b.Serialize(ms, o);
                        if (ms.Length > int.MaxValue)
                        throw new ArgumentExcepti on("Serialized object is larger than
                        can fit into byte array");
                        buffer = ms.GetBuffer();
                        }
                        return buffer;
                        }

                        public object DeSerialize(byt e[] bytes){
                        object o;
                        using(MemoryStr eam ms = new MemoryStream(by tes))
                        {
                        BinaryFormatter b = new BinaryFormatter ();
                        o = b.Deserialize(m s);
                        }
                        return o;
                        }
                        }

                        Usage:
                        //Client....
                        ....
                        Serializer se = new Serializer();
                        while(...)
                        ...
                        sb = se.Serialize(m) ;
                        bWriter.Write(s b.Length); // Write size in bytes of serialized
                        array
                        bWriter.Write(s b);

                        }

                        Serializer se = new Serializer();
                        while (i < N)
                        {
                        ++i;
                        int length = bReader.ReadInt 32();
                        byte[] data = bReader.ReadByt es(length);
                        Msg o = se.DeSerialize( data) as Msg;
                        ....
                        }

                        Using this method it should be possible to achieve results approaching these
                        obtained with C++.

                        Willy.




                        Comment

                        Working...