Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it more efficient to replace methods from a class into extension methods?
    primarykey
    data
    text
    <p>I recursively find all the files in a directory using <a href="https://stackoverflow.com/a/724184/637142">this</a> apreach which is realy fast.</p> <p>anyways I store the info in each file in the struct:</p> <pre><code>struct Info { public bool IsDirectory; public string Path; public FILETIME ModifiedDate; } </code></pre> <p><strong>So now I am trying to decide weather to place the helper methods inside that struct or somewhere else for efficiency purposes.</strong></p> <p>The helper methods are:</p> <pre><code>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 } </code></pre> <p>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:</p> <pre><code>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 } </code></pre> <p><strong>So my question is</strong> because <code>Info</code> is an instance struct then having those methods inside result in wising more memory? If <code>Info</code> is an instance struct then each method will have a different address in memory?</p> <p>I have tried both techniques and I cannot seemed to see a difference. Maybe I need to try with more files.</p> <hr> <h2>Edit</h2> <p>Here is to prove that <a href="https://stackoverflow.com/users/250329/fabio-gouw">@Fabio Gouw</a> is wright:</p> <pre><code>// 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); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload