4

我使用这个非常快的方法递归地找到目录中的所有文件。

无论如何,我将信息存储在结构中的每个文件中:

struct Info 
{
    public bool IsDirectory;
    public string Path;
    public FILETIME ModifiedDate;
}

所以现在我正试图决定天气将辅助方法放置在该结构内或其他地方以提高效率。

辅助方法是:

struct Info 
{
    public bool IsDirectory;
    public string Path;
    public FILETIME ModifiedDate;

    // Helper methods:
    public string GetFileName(){ /* implementation */ }
    public string GetFileSize(){ /* implementation */ }
    public string GetFileAtributes() { /* implementation */ }
    // etc many more helper methods
}

我在内存中保存了数千个文件,我不知道在 Info 中使用这些方法是否会影响性能。换句话说,删除这些方法并将它们作为扩展方法会更好:

public static class ExtensionHelperMethods
{
    static public string GetFileName(this Info info){ /* implementation */ }
    static public string GetFileSize(this Info info){ /* implementation */ }
    static public string GetFileAtributes(this Info info) { /* implementation */ }
    // etc many more helper methods
}

所以我的问题是,是因为Info实例结构然后在内部拥有这些方法会导致更多内存吗?如果Info是实例结构,那么每个方法在内存中都有不同的地址吗?

我已经尝试了这两种技术,但我似乎看不出有什么不同。也许我需要尝试更多的文件。


编辑

这是为了证明@Fabio Gouw是正确的:

// This program compares the size of object a and b
class Program
{
    static void Main(string[] args)
    {
        InfoA a = new InfoA();
        InfoB b = new InfoB();

        if (ToBytes(a).Length == ToBytes(b).Length)
        {
            Console.Write("Objects are the same size!!!");
        }

        Console.Read();
    }

    public static byte[] ToBytes(object objectToSerialize)
    {
        BinaryFormatter bf = new BinaryFormatter();
        MemoryStream memStr = new MemoryStream();

        try
        {
            bf.Serialize(memStr, objectToSerialize);
            memStr.Position = 0;

            var ret = memStr.ToArray();

            return ret;
        }
        finally
        {
            memStr.Close();
        }
    }

    [Serializable]
    struct InfoA
    {
        public bool IsDirectory;
        public string Path;
    }

    [Serializable]
    struct InfoB
    {
        public bool IsDirectory;
        public string Path;

        public string GetFileName()
        {
            return System.IO.Path.GetFileName(Path);
        }
    }
}
4

2 回答 2

5

方法不会影响对象大小,只有字段会影响(方法是行为;字段是数据,它们存储在内存中)。将它们放在 Info 类中或作为扩展方法的决定只是一个设计问题。

这个问题与您的问题相似:将方法转换为静态方法时的内存使用情况

于 2012-09-05T17:49:46.100 回答
0

我倾向于将结构限制为对象形状而不是行为。类更适用于行为。一个更大的潜在问题是您如何传递您的类型,特别是在将类型作为方法参数传递时注意装箱操作和堆栈/堆分配。

That being said, extension methods on classes are mostly syntactic sugar over calling the native static method. The compiler translates your extension method into a call to the Extension method, thus there shouldn't be a runtime performance difference between static and extension methods.

Extension methods do open up the possibility of shooting yourself in the foot if you later add a similar method to the underlying class/struct and find that the native method is used when you expected the extension method to be used. Also, Extension methods are harder to disambiguate as compared to fully qualified names or namespace aliases that you can use with normal static methods. For more information on how extension methods are compiled, see http://www.thinqlinq.com/Post.aspx/Title/DLinq-Extension-Methods-Decomposed.

于 2012-09-05T17:52:32.117 回答