请教:WCF速度似乎比Remoting慢
Posted kevin-y
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了请教:WCF速度似乎比Remoting慢相关的知识,希望对你有一定的参考价值。
两段极为相似的代码,主要想看看过输与序列化过程两者的用时差异,结果10000次的调用,WCF用了11秒多,remoting用了5秒不到!
这是测试的源代码
WCF的服务端
public class RemotCalc : MarshalByRefObject, ICalc { public CalcInfo Calc(CalcInfo pInfo) { CalcInfo info = new CalcInfo(); info.Method = string.Format("back_{0}", pInfo.Method); info.Para1 = pInfo.Para1; info.Para2 = pInfo.Para2; info.Result = pInfo.Result + 999; return info; } }
Remoting的服务端
public class WcfCalc : Srv.Interface.ICalc { public CalcInfo Calc(CalcInfo pInfo) { CalcInfo info = new CalcInfo(); info.Method = string.Format("back_{0}", pInfo.Method); info.Para1 = pInfo.Para1; info.Para2 = pInfo.Para2; info.Result = pInfo.Result + 999; return info; } }
代码可以理解为一样的,以下的客户端,两客户端也可以视为一样的过程
WCF客户端
static void Main(string[] args) { using(ChannelFactory<Srv.Interface.ICalc> factory = new ChannelFactory<Srv.Interface.ICalc>("Calc2")) { Srv.Interface.ICalc calc = factory.CreateChannel(); CalcInfo info = new CalcInfo(); info.Method = "test"; Console.WriteLine("press any key to run..."); Console.ReadLine(); int max = 10000; Console.WriteLine("it‘s run..."); DateTime start = DateTime.Now; for (int i = 0; i < max; i++) { CalcInfo res = calc.Calc(info); } TimeSpan sp = DateTime.Now - start; Console.WriteLine("run {0} times use {1}ms ", max, sp.TotalMilliseconds); Console.ReadLine(); } }
Remoting客户端
static void Main(string[] args) { ChannelServices.RegisterChannel(new TcpClientChannel(), false); ICalc remoteobj = (ICalc)Activator.GetObject(typeof(ICalc), "tcp://localhost:6666/Calc"); CalcInfo info = new CalcInfo(); info.Method = "test"; Console.WriteLine("press any key to run..."); Console.ReadLine(); int max = 10000; DateTime start = DateTime.Now; for (int i = 0; i < max; i++) { CalcInfo res = remoteobj.Calc(info); } TimeSpan sp = DateTime.Now - start; Console.WriteLine("run {0} times use {1}ms ", max, sp.TotalMilliseconds); Console.ReadLine(); }
以上是关于请教:WCF速度似乎比Remoting慢的主要内容,如果未能解决你的问题,请参考以下文章
具有嵌套任务的 C# 代码比仅在顶层具有任务的相同代码运行速度慢
Guava ImmutableMap 的访问速度明显比 HashMap 慢