Is it more efficient to replace the methods of a class with extension methods?

advertisements

I recursively find all the files in a directory using this apreach which is realy fast.

anyways I store the info in each file in the struct:

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

So now I am trying to decide weather to place the helper methods inside that struct or somewhere else for efficiency purposes.

The helper methods are:

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
}

I am saving thousands of files on memory and I don't know if having those methods inside Info will affect performance. In other words will it be better to remove those methods and make them extension methods as:

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
}

So my question is because Info is an instance struct then having those methods inside result in wising more memory? If Info is an instance struct then each method will have a different address in memory?

I have tried both techniques and I cannot seemed to see a difference. Maybe I need to try with more files.


Edit

Here is to prove that @Fabio Gouw is wright:

// 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);
        }
    }
}


Methods don't interfere with object size, only fields do (methods are behavior; fields are data and these are stored in memory). The decision on putting them inside Info class or as extension methods will only be a design issue.

This question is similar as yours: Memory usage when converting methods to static methods