调用静态类的静态方法与通过接口(interface)调用对应方法的性能比较会是怎样的呢?有人进行过测试或统计么?

解决方案 »

  1.   

    我提这个问题的原由是这样的,现在我们公司一套系统是由微软的mcs写的,基本使用的都是静态方法,我认为他这么做的主要原因是能够提高速度,但这样就导致系统的可扩展性受到限制,我们想提高系统可扩展的能力,但必然要改变他的这种做法,所以我才有此一问,想知道它们之间的速度差别有多大,来考虑我们能进行多大的改动
      

  2.   

    静态方法也是封装自对象的。除非你使用的不是oop语言。对象使用接口或者继承,在编译时自动生成相关结构维护或者继承代码,增加了所谓的“可扩展能力”。示例就不用了吧。
      

  3.   

    看来我想从这里得到个确切的答案是比较难的了,因为大家认为这是个无聊的话题啊,呵呵,不过我不死心,自己写了点测试代码,大致找到了答案,对于这个帖的有始有终,我把代码贴出来
    ------------------------------------------------------------------------------
    /// <summary>
    /// 静态方法测试类
    /// </summary>
    public class TestStatic
    {
    public static string Test()
    {
    string ret = "";
    for(int i = 0;i < 100; i ++)
    {
    ret += "Test" + i + "<br>";
    }
    return ret;
    }
    }
      

  4.   

    /// <summary>
    /// TestInterface 的摘要说明。
    /// </summary>
    public interface TestInterface
    {
    string Test();
    }--------------------------------------------------------
    /// <summary>
    /// TestClass 的摘要说明。
    /// </summary>
    public class TestClass:TestInterface
    {
    public TestClass()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    }
    #region TestInterface 成员 public string Test()
    {
    string ret = "";
    for(int i = 0;i < 100; i ++)
    {
    ret += "Test" + i + "<br>";
    }
    return ret;
    } #endregion
    }
      

  5.   

    private void Page_Load(object sender, System.EventArgs e)
    {
    Response.Write("C-S<br>");
    int s_total = 0;
    int c_total = 0;
    for(int i = 0; i < 1000; i ++)
    {
    int s = TestClassMethod(i);
    int c = TestStaticMethod(i);
    s_total += s;
    c_total += c;
    if(s > 0 || c > 0)
    {
    Response.Write(c.ToString() + "-" + s.ToString() + "<br>");
    }
    }
    Response.Write("----------------<br>");
    Response.Write(c_total.ToString() + "-" + s_total.ToString() + "<br>");
    } private int TestStaticMethod(int sort)
    {
    int start = System.Environment.TickCount;
    TestStatic.Test();
    int end = System.Environment.TickCount;
    return end - start;
    } private int TestClassMethod(int sort)
    {
    int start = System.Environment.TickCount;
    TestInterface test = new TestClass();
    test.Test();
    int end = System.Environment.TickCount;
    return end - start;
    }