.NET Remoting / Serialization方式对C ++ binary / tcp来说太慢了 [英] .NET Remoting/Serialization way too slow vs C++ binary/tcp

查看:62
本文介绍了.NET Remoting / Serialization方式对C ++ binary / tcp来说太慢了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Win32 1GHZ处理器上运行了一些测试,看看使用C#

..NET多次通过TCP / IP传输对象需要花多长时间? Remoting vs C ++值得信赖的二进制流方法。我在50K,100K,500K迭代的测试中运行了

,其中每次迭代包括从客户端进程向服务器进程发送对象的
,以及

服务器进程发回一个确认。

结果如下:


.NET Remoting C ++二进制TCP / IP

-------------- ------------------

50,000迭代次数:128秒3秒

100,000迭代次数:300秒8秒

500,000迭代次数:1459秒43秒


在上面的测试中.NET远程处理开销是42.6x,37.5x和

比c ++版本慢33.9倍。


这是使用的对象:

----------------------------------------------- ----------------

[可序列化]

公共类Msg

{

public int msgType_;

public int seqNum_;

public Stri ng symbol_;

public int quoteId _;

public int responseLevel_;

public int eqiRole_;

public float bidPrice_ ;

public float offerPrice_;

public int bidSize_;

public int offerSize_;

public float liquidityBidPrice _;

public float liquidityOfferPrice_;

public int liquidityBidSize_;

public int liquidityOfferSize_;

public int checkSum _;

}


服务器流程:

-------------------- ----------------------------------

使用系统;

使用消息;

命名空间tcpServer

{


使用系统;

使用系统.Net;

使用System.Net.Sockets;

使用System.Runtime.Serialization.Formatters.Binary;

使用System.IO;

///< summary>

/// Class1的摘要说明。

///< / summary>

class Class1

{

///< summary>

///应用程序的主要入口点。

/ //< / summary>

[STAThread]

static void Main(string [] args)

{

int port = 7627;

int N = 50000;

int i = 0;


BinaryFormatter bF = new BinaryFormatter ();


TcpListener tcpListener = new TcpListener(port);

tcpListener.Start();

Socket soTcp = tcpListener .AcceptSocket();

Console.WriteLine(SampleClient通过TCP连接。);

NetworkStream stream = new NetworkStream(soTcp,

FileAccess.ReadWrite,true);

BinaryReader bReader = new BinaryReader(stream);


DateTime beginTime = new DateTime();


while(i< N)

{

if(i == 0)

{

beginTime = System.DateTime.Now ;

}


++我;

字节[]收到=新字节[1024];

Messages.Msg msg = new Messages.Msg();

msg =(Messages.Msg)bF.Deserialize(stream);

String returningString = Convert。 ToString(99);

Byte [] returningByte =

System.Text.Encoding.ASCII.GetBytes(returnsStrin g.ToCharArray());


//将确认返回给客户。

soTcp.Send(returnsByte,returnsByte.Length,0);

}

DateTime endTime = System.DateTime.Now;

Console.WriteLine(endTime - beginTime);

}

}

}


客户流程

-------------------- -------------------------------------------------

使用System;

使用System.Net;

使用System.Net.Sockets;

使用System.IO ;

使用System.R untime.Serialization.Formatters.Binary;

使用消息;


命名空间tcpClient

{

///< summary>

/// Class1的摘要说明。

///< / summary>

///


class Class1

{

///< summary>

///申请的主要入口点。

///< / 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.GetStream();

BinaryWriter bWriter = new BinaryWriter(tcpStream);

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.liquidityBidPrice_ = 122.12f;

m.liquidityOfferPrice_ = 192.32f;

m.offerPrice_ = 154.25f;

m.quoteId_ = i;

m.responseLevel_ = 10;

m.symbol_ =" IBM";

bF.Serialize(tcpStream,m);


//回读Ack

字节[]收到=新字节[1024];

tcpStream.Read(收到,0,收到。长度);


}

}

}

}


C ++二进制流方法只是简单地模拟上面的内容,但没有涉及

序列化 - 只是通过

TCP / IP套接字发送类的字节流。


如果.NET Remoting / Serialization如此慢,为什么有人会使用

它超过C ++的性能c每天传输数十万条消息的ritical应用程序?这笔交易真的值得吗?b $ b值得吗?人们的想法是什么?

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 liquidityBidPrice_;
public float liquidityOfferPrice_;
public int liquidityBidSize_;
public int liquidityOfferSize_;
public int checkSum_;
}

The Server Process:
------------------------------------------------------
using System;
using Messages;
namespace tcpServer
{

using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
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(port);
tcpListener.Start();
Socket soTcp = tcpListener.AcceptSocket();
Console.WriteLine("SampleClient is connected through TCP.");
NetworkStream stream = new NetworkStream(soTcp,
FileAccess.ReadWrite, true);
BinaryReader bReader = new BinaryReader(stream);

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.ToString(99);
Byte[] returningByte =
System.Text.Encoding.ASCII.GetBytes(returningStrin g.ToCharArray());

//Returning a confirmation back to the client.
soTcp.Send(returningByte, returningByte.Length, 0);
}
DateTime endTime = System.DateTime.Now;
Console.WriteLine(endTime - beginTime);
}
}
}

The Client Process
---------------------------------------------------------------------
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
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.GetStream();
BinaryWriter bWriter = new BinaryWriter(tcpStream);
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.liquidityBidPrice_ = 122.12f;
m.liquidityOfferPrice_ = 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 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 ?

推荐答案

Remoting不仅仅是拍摄一个类的内容而不是

电线。通过远程处理,您可以进行方法调用,接收

,它们在到达呼叫站点之前操纵呼叫(在
结束时等等) 。这肯定有助于通过远程控制打电话所需的总时间




此外,通过序列化,您可以开箱即用,很少或没有

修改你的课程(Serializable

属性除外)。在C ++中,我已经看到了一些非常人为的方法来序列化

信息以通过网络发送。当你需要序列化大对象图时,序列化真的在.NET中闪耀。


你的示例类有点简单。当然,它可能是你所发送的所有内容,但在更复杂的情况下,我认为C ++解决方案

将变得难以实现(我在这里可能会非常错误) (b)b。最后,你必须衡量你的需求。如果

在1459秒内处理500K方法(平均每秒342次调用)

是不够的,那么就不要使用它了。但是,如果你只需要处理

说,每秒40次操作,那么这就足够了,而且我将需要.NET解决方案,因为它会更容易实现。


希望这会有所帮助。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


< aj ******* @ yahoo.com>在消息中写道

news:11 ********************** @ o13g2000cwo.googlegr oups.com ...
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.com

<aj*******@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
我在Win32 1GHZ处理器上运行了一些测试,看看使用C#
.NET Remoting和C ++值得信赖的二进制方法,通过TCP / IP多次传输对象需要多长时间流。我为50K,100K,500K迭代运行了测试,其中每次迭代包括从客户端进程向服务器进程发送对象,并且服务器进程发送回ack。
以下是结果:

.NET Remoting C ++二进制TCP / IP
-------------- ------- -----------
50,000次迭代:128秒3秒
100,000次迭代:300秒8秒
500,000次迭代:1459秒43秒

以下是使用的对象:
------------------------------------------------- --------------
[Serializable]
public class Msg
{public int msgType_;
public int seqNum _;
public String symbol_;
pu blic int quoteId_;
public int responseLevel_;
public int eqiRole_;
public float bidPrice_;
public float offerPrice_;
public int bidSize_;
public int offerSize_;
public float liquidityBidPrice_;
public float liquidityOfferPrice_;
public int liquidityBidSize_;
public int liquidityOfferSize_;
public int checkSum_;
}

服务器流程:
------------------------------------- -----------------
使用系统;
使用消息;

命名空间tcpServer
{

使用System;
使用System.Net;
使用System.Net.Sockets;
使用System.Runtime.Serialization.Formatters.Binary;
使用System.IO ;

///< summary>
/// Class1的摘要说明。
///< / summary>
class Class1
{
///< summary>
///主要入口点对于应用程序。
///< / summary>
[STAThread]
static void Main(string [] args)
{
int port = 7627;
int N = 50000;
int i = 0;

BinaryFormatter bF = new BinaryFormatter();

TcpListener tcpListener = new TcpListener(port) ;
tcpListener.Start();
Socket soTcp = tcpListener.AcceptSocket();
Console.WriteLine(SampleClient通过TCP连接。);
NetworkStream流= new NetworkStream(soTcp,
FileAccess.ReadWrite,true);
BinaryReader bReader = new BinaryReader(stream);

DateTime beginTime = new DateTime();

而(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.ToString(99);
Byte [] returningByte =
System.Text.Encoding.ASCII.GetBytes(returnsStrin g.ToCharArray());

//将确认返回给客户端。
soTcp.Send(returningByte,returnsByte.Length,0);
}
DateTime endTime = System.DateTime.Now;
Console.WriteLine(endTime - beginTime);
}
}


客户流程
-------- -------------------------------------------------- -----------
使用System;
使用System.Net;
使用System.Net.Sockets;
使用System.IO;
使用System.Runtime.Serialization.Formatters.Binary;
使用消息;

命名空间tcpClient
{
///< summary>
/ // Class1的摘要描述。
///< / summary>
///

类Class1
{
///< summary>
///应用程序的主要入口点。
///< / 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.GetStream();
BinaryWriter bWriter = new BinaryWriter(tcpStream);
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.liquidityBidPrice_ = 122.12f;
m.liquidityOfferPrice_ = 192.32f;
m.offerPrice_ = 154.25f;
m.quoteId_ = i;
m.responseLevel_ = 10;
m.symbol_ =" IBM" ;;
bF.Serialize( tcpStream,m);

//回读Ack
字节[]收到=新字节[1024];
tcpStream.Read(收到,0,收到。长度) ;

}
}



C ++二进制流方法是简单地模拟上面但不是
序列化涉及 - 只需通过TCP / IP套接字发送类的字节流。

如果.NET Remoting / Serialization如此慢,为什么有人会使用
它通过C ++用于每天传输数十万条消息的性能关键应用程序?权衡真的值得吗?人们的想法是什么?
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 liquidityBidPrice_;
public float liquidityOfferPrice_;
public int liquidityBidSize_;
public int liquidityOfferSize_;
public int checkSum_;
}

The Server Process:
------------------------------------------------------
using System;
using Messages;
namespace tcpServer
{

using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
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(port);
tcpListener.Start();
Socket soTcp = tcpListener.AcceptSocket();
Console.WriteLine("SampleClient is connected through TCP.");
NetworkStream stream = new NetworkStream(soTcp,
FileAccess.ReadWrite, true);
BinaryReader bReader = new BinaryReader(stream);

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.ToString(99);
Byte[] returningByte =
System.Text.Encoding.ASCII.GetBytes(returningStrin g.ToCharArray());

//Returning a confirmation back to the client.
soTcp.Send(returningByte, returningByte.Length, 0);
}
DateTime endTime = System.DateTime.Now;
Console.WriteLine(endTime - beginTime);
}
}
}

The Client Process
---------------------------------------------------------------------
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
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.GetStream();
BinaryWriter bWriter = new BinaryWriter(tcpStream);
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.liquidityBidPrice_ = 122.12f;
m.liquidityOfferPrice_ = 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 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 ?



这听起来不对。首先,一个术语点。

这不是远程b / c你没有使用远程处理

类(RemotingConfiguration,ChannelServices等)。 br />
但它是序列化的,我希望它会这样做

这个。


跳出来的一个项目是内存分配。

您的服务器在每次迭代中都在创建内存。

当然,如果你在原生C ++中这样做,这是公平的

比较。


可以你也发布了C ++吗?
This doesn''t sound right. First, a terminology point.
This isn''t remoting b/c you''re not using the remoting
classes (RemotingConfiguration, 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?
-----原始消息-----
我在Win32 1GHZ处理器上运行了一些测试到
看看使用C#.NET Remoting与二进制
流的C ++值得信赖的方法,通过
TCP / IP多次传输对象需要多长时间。我尝试进行50K,100K,500K迭代测试,其中每个
迭代包括从客户端进程向服务器
进程发送一个对象,服务器进程发回一个确认。
这是结果:

.NET Remoting C ++
二进制TCP / IP -------------- -------
--- -------- 50,000迭代次数:128秒3
秒100,000次迭代次数:300秒8
秒500,000迭代次数:1459秒43

在上面测试.NET远程开销是42.6x,
37.5x,比c ++版本慢33.9x。

以下是使用的对象:
---- -------------------------------------------------- ----
----- [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 liquidityBidPrice_;
public float liquidityOfferPrice_;
public int liquidityBidSize_;
public int liquidityOfferSize_;
public int checkSum_;
}

服务器进程:
--------------------------------------------- ---------
使用系统;
使用消息;

命名空间tcpServer
使用系统; 使用系统; <使用System.Net;
使用System.Net.Sockets;
使用
System.Runtime.Serialization.Formatters.Binary;使用System.IO;

///< summary>
/// Class1的摘要说明。
///< / summary>
class Class1
{
///< summary>
///
应用程序的主要入口点。 ///< / summary>
[STAThread]
static void Main(string [] args)
{
int port = 7627;
int N = 50000 ;
int i = 0;

BinaryFormatter bF = new
BinaryFormatter();
TcpListener tcpListener = new
TcpListener(port); tcpListener.Start();
Socket soTcp =
tcpListener.AcceptSocket(); Console.WriteLine(SampleClient是通过TCP连接的
。); NetworkStream stream = new
NetworkStream(soTcp,FileAccess.ReadWrite,true);
BinaryReader bReader = new
BinaryReader(stream);
DateTime beginTime = new DateTime
( );
while(i< N)
{
if(i == 0)
{
beginTime =
System.DateTime.Now; }

++ i;
字节[]收到=新字节
[1024]; Messages.Msg msg = new
Messages.Msg(); msg =(Messages.Msg)
bF.Deserialize(stream); String returningString =
Convert.ToString(99); Byte [] returningByte =
System.Text.Encoding.ASCII.GetBytes
(returningString.ToCharArray());
//将
确认返回给客户端。 soTcp.Send(returningByte,
returningByte.Length,0); } DateTime endTime =
System.DateTime.Now; Console.WriteLine(endTime -
beginTime); }
}

客户流程
------------------------ ----------------------------------
-----------使用系统;
使用System.Net;
使用System.Net.Sockets;
使用System.IO;
使用System.Runtime.Serialization.Formatters.Binary;
使用消息;

命名空间tcpClient
{
///< summary>
/// Class1的摘要描述。
///< / summary>
///

类Class1
{
///< summary>
/// $的主要切入点b $ b申请。 ///< / 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.GetStream(); BinaryWriter bWriter = new
BinaryWriter(tcpStream); 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.liquidityBidPrice_ = 122.12f;
m.liquidityOfferPrice_ = 192.32f; m.offerPrice_ =
154.25f; m.quoteId_ = i;
m.responseLevel_ =
10; m.symbol_ =" IBM";
bF.Serialize
(tcpStream,m);
//读回
Ack Byte []收到=
new字节[1024]; tcpStream.Read
(收到,0,收到。长度);
}
}
}
}


C ++二进制流方法是简单地模拟上面的
但涉及的是noserialization - 只需通过TCP / IP套接字发送
类的字节流。

如果.NET Remoting / Serialization是这么慢,为什么每个人都会使用C ++来获得性能关键的应用程序
每天传输成千上万封邮件?这是
的权衡吗?人们的想法是什么?

-----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 liquidityBidPrice_;
public float liquidityOfferPrice_;
public int liquidityBidSize_;
public int liquidityOfferSize_;
public int checkSum_;
}

The Server Process:
------------------------------------------------------
using System;
using Messages;
namespace tcpServer
{

using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary; 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(port); tcpListener.Start();
Socket soTcp = tcpListener.AcceptSocket(); Console.WriteLine("SampleClient is connected through TCP."); NetworkStream stream = new NetworkStream(soTcp,FileAccess.ReadWrite, true);
BinaryReader bReader = new BinaryReader(stream);
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.ToString(99); Byte[] returningByte =
System.Text.Encoding.ASCII.GetBytes (returningString.ToCharArray());
//Returning a confirmation back to the client. soTcp.Send(returningByte, returningByte.Length, 0); }
DateTime endTime = System.DateTime.Now; Console.WriteLine(endTime - beginTime); }
}
}

The Client Process
---------------------------------------------------------- -----------using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
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.GetStream(); BinaryWriter bWriter = new BinaryWriter(tcpStream); 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.liquidityBidPrice_ = 122.12f; m.liquidityOfferPrice_ = 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 noserialization 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 ?

.



>如果.NET Remoting / Serialization如此之慢,那么为什么有人会在C ++上使用
> 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 ?




如果你每天只做几十万,那么它就是这样的事实

需要花费5分钟来处理100,000次迭代意味着你可以每24小时处理
近2900万次迭代。


但为什么不在C#中做同样的事情那你用C ++做什么如果

性能至关重要?这是权衡。如果你想要性能,打开一个套接字,将数据打包成一个字节数组并发送出去。 .NET中的序列化

的东西必然很慢,因为它需要反思。因为我没有必要使用它,所以我不能用b $ b来代替远程用品。如果

表现是一个问题,序列化不是你的朋友。


Pete



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


这篇关于.NET Remoting / Serialization方式对C ++ binary / tcp来说太慢了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆