标题: MarshalByRefObject 的性能损失
- 编写人生 2008-04-29 15:20 阅读:173
- 评论:1 查看评论 | 添加评论

以前看过文章说MarshalByRefObject 会造成性能的损失,我比较相信自己,所以亲自测试了一下,下面是代码:

[图片][图片]测试代码
[图片]using System;
[图片]using System.Diagnostics;
[图片]
[图片][图片]namespace ConsoleApplication1 [图片]{
[图片][图片]    class Program [图片]{
[图片][图片]        static void Main(string[] args) [图片]{
[图片]            Stopwatch watch = new Stopwatch();
[图片]            watch.Start();
[图片]            B b = new B();
[图片][图片]            for (int i = 0; i < 100000000; i++) [图片]{
[图片]                b.Add(3, 5);
[图片]            }
[图片]            watch.Stop();
[图片]            Console.WriteLine("B 花费时间:" + watch.ElapsedMilliseconds.ToString());
[图片]            watch.Reset();
[图片]
[图片]            watch.Start();
[图片]            A a = new A();
[图片][图片]            for (int i = 0; i < 100000000; i++) [图片]{
[图片]                a.Add(3, 5);
[图片]            }
[图片]            watch.Stop();
[图片]            Console.WriteLine("A MarshalByRefObject 花费时间:" + watch.ElapsedMilliseconds.ToString());
[图片]            watch.Reset();
[图片]
[图片]            //=======================
[图片]            watch.Start();
[图片]            a = new A();
[图片][图片]            for (int i = 0; i < 100000000; i++) [图片]{
[图片]                a.Add(3, 5);
[图片]            }
[图片]            watch.Stop();
[图片]            Console.WriteLine("A MarshalByRefObject 花费时间:" + watch.ElapsedMilliseconds.ToString());
[图片]            watch.Reset();
[图片]
[图片]            watch.Start();
[图片]            b = new B();
[图片][图片]            for (int i = 0; i < 100000000; i++) [图片]{
[图片]                b.Add(3, 5);
[图片]            }
[图片]            watch.Stop();
[图片]            Console.WriteLine("B 花费时间:" + watch.ElapsedMilliseconds.ToString());
[图片]            watch.Reset();
[图片]            Console.Read();
[图片]        }
[图片]    }
[图片]
[图片][图片]    class A : MarshalByRefObject [图片]{
[图片][图片]        public int Add(int a, int b) [图片]{
[图片]            return a + b;
[图片]        }
[图片]    }
[图片]
[图片][图片]    class B [图片]{
[图片][图片]        public int Add(int a, int b) [图片]{
[图片]            return a + b;
[图片]        }
[图片]    }
[图片]}
[图片]

测试的结果是:
 B 花费时间:55
A MarshalByRefObject 花费时间:957
A MarshalByRefObject 花费时间:972
B 花费时间:56                  

总结:像这样在本地环境下,性能仍然损失了近17.4倍。当然,此17被不能简单的理解为你的应用就慢了17倍,这里仅表示发起调用损失了17倍。
查看评论 | 添加评论
返回顶部 | 返回首页