473,722 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

.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 ?

Nov 16 '05 #1
11 11957
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]
- mv*@spam.guard. caspershouse.co m

<aj*******@yaho o.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
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 ?

Nov 16 '05 #2
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?
-----Original Message-----
I was running some tests on my Win32 1GHZ processor to see how long itwould take to transmit objects numerous times via TCP/IP using C#..NET Remoting vs the C++ trustworthy method of binary streams. I ranthe test for 50K, 100K, 500K iterations, where each iteration consistsof sending an object from a client process to a server process, and theserver process sends back an ack.
Here are the results:

.NET Remoting C++ Binary TCP/IP -------------- ------- -----------50,000 Iterations: 128 seconds 3 seconds100,000 Iterations: 300 seconds 8 seconds500,000 Iterations: 1459 seconds 43 seconds
In the above tests the .NET remoting overhead was 42.6x, 37.5x, and33.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.Rea dWrite, 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) bF.Deserialize( stream); String returningString = Convert.ToStrin g(99); Byte[] returningByte =
System.Text.En coding.ASCII.Ge tBytes (returningStrin g.ToCharArray() );
//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 ("machine1", 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 (tcpStream, 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 noserializatio n involved - just send the byte stream of the class viaTCP/IP sockets.

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

.

Nov 16 '05 #3
> 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 ?


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
Nov 16 '05 #4
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" <pd******@NOSPA M.hotmail.com> wrote in message
news:BM******** ************@gi ganews.com...
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 ?

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

Nov 16 '05 #5
Another thought:
Try running this through the JetBrains and CLR profilers to see where the
CPU cycles and memory allocations look like.

Dave

<aj*******@yaho o.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
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 ?

Nov 16 '05 #6
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.

Nov 16 '05 #7
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

<aj*******@yaho o.com> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.com...
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.

Nov 16 '05 #8
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.

Nov 16 '05 #9
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_;

Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4250
by: Mountain Bikn' Guy | last post by:
We have an app that computes a lot of numeric data. We would like to save to disk about 1-2 gigabytes of computed data that includes ints, doubles, strings and some complex objects that contain hashtables. We would like to read this data back into the app and reuse those values and state to compute more new data. Up to this point we just write out comma separated ASCII values for everything. And we read/parse the ascii back in. The ASCII...
3
2274
by: Sunny | last post by:
Hi all, I'm creating client/server app in C# (VS. 2003). So, I need the client to call the server with some auth info (user and pass). If the auth is OK, server will do some work and will prepare a zip-file and some other text and numeric data and will notify the client that everything is ready. And the client will fetch the data. So if it was all console/desktop application, I know what to do (in
6
2715
by: Uttam | last post by:
Hello, We are at a very crucial decision making stage to select between .Net and Java. Our requirement is to download a class at runtime on the client computer and execute it using remoting or rmi. Just to keep my question short I am posting trimmed version of my code. //file: Serializable.cs
3
2872
by: Steve | last post by:
I've been following a couple remoting tutorials on the web, they are all pretty much the same. I've got my different applications built(client, server and remote object (dll)) The client is able to get a reference to the remote object and that works fine. When I try to make a call to a remote object's method I get an exception: System.Runtime.Serialization.SerializationException: Cannot find the assembly ProcessTest,...
13
1919
by: Ron L | last post by:
I am working on an application that is a front-end for a SQL database. While it is not an immediate requirement, the application will probably be required to be able to connect via the internet at a later date, so we are implementing the data connections via remoting. The remoting is implemented via the HTTP channel, with the binary formatter. We have noticed, however, that the remoting calls seemed to be much slower that the OLEDB calls,...
1
4697
by: Remy De Almeida | last post by:
Hi, I have a method on RemoteServer that returns a ArrayList which contains objects. The Remote server has been throwing this error whihc i am not been able to understand .. please can someone let me know what could be happeining. Error The internal array cannot expand to greater than Int32.MaxValue elements.: Server stack trace:
2
1283
by: TonyJ | last post by:
Hello! I read on a page in the NET this text "What makes Remoting slow is not so much the communication protocol but the serialization." Can somebody explain if I use remoting for sending message will it be slow if I use remoting then? //Tony
1
1363
by: Dan Holmes | last post by:
I traced this all the to the domain boundary. I also but a breakpoint in the server. That was never hit. It happens after it leaves the client but before it calls into the "add" method in the server. I don't know how to debug this any further. exact error message and code follows this is what fails. it fails on the add. TestContactInfo tci = new TestContactInfo(); tci.Address1 = "address1";
1
5395
by: sandeepbhutani304 | last post by:
have 2 projects communicating each other with .NET remoting. But when I am trying to call these functions I am getting the error: The input stream is not a valid binary format. The starting contents (in bytes) are: 53-79-73-74-65-6D-2E-54-79-70-65-4C-6F-61-64-45-78 ...
0
8863
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8739
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9238
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5995
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4502
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.