Is it more efficient to replace class methods with extension methods?

I recursively detect all files in a directory using this apreach, which is fast.

In any case, I save the information in each file in the structure:

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

So now I am trying to decide that the weather places helper methods inside this structure or somewhere else for efficiency.

Helper Methods:

 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 store thousands of files in memory, and I don’t know if using these methods inside Info will affect performance. In other words, it would be better to remove these methods and make them extension methods like:

 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 structure, after which these methods internally lead to more memory? If Info is an instance structure, will each method have a different address in memory?

I tried using both methods and I do not see the differences. Maybe I need to try with a lot of files.


Edit

Here's to prove that @Fabio Gouw is right:

 // 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
source share
2 answers

Methods do not interfere with the size of the object, only fields (methods are behavior, fields are data, and they are stored in memory). The decision to place them in the Info class or as extension methods will only be a design problem.

This question is similar to yours: Using memory when converting methods to static methods

+5
source

I tend to limit structures to the shape of the object, rather than behavior. Classes are more for behavior. A big potential problem is how you pass your type, especially watching the actions of the box and the stack / heap distributions when passing the type as method parameters.

Speaking of which, class extension methods are basically syntactic sugar that calls its own static method. The compiler translates your extension method into a call to the extension method, so there should be no performance difference during the execution of static and extension methods.

Extension methods open up the possibility of shooting yourself in the foot if you later add a similar method to the / struct base class and find that your own method is used when you expected the extension method to be used. In addition, extension methods are much more difficult to eliminate when compared with fully qualified names or aliases namespaces that can be used with conventional static methods. For more information on how extension methods are compiled, see http://www.thinqlinq.com/Post.aspx/Title/DLinq-Extension-Methods-Decomposed .

0
source

Source: https://habr.com/ru/post/1432540/


All Articles