Lambda based reflection Vs Normal reflection Vs Direct call
One friend of mine sent me the following code sample that really surprised me.
There are three different approaches being used to call a method from a different class. The approaches are -
- Direct Call
- Call through Lambda based reflection
- Call through Normal Reflection
It was really surprising to see the execution speed comparison between three different approaches. The lambda based approach was found to be the fastest by the order of 1000 times.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Linq.Expressions; using System.Reflection; namespace LambdaReflection { class MyClass { public void CallIt() { } } class Program { delegate void LocalDel<t>(T mc); static void Main(string[] args) { System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); MethodInfo minfo = typeof(MyClass).GetMethod("CallIt", BindingFlags.Instance | BindingFlags.Public); MyClass obj = new MyClass(); //compile expression tree LocalDel<myclass> lambdaExpression = FuncCallIt<myclass>("CallIt"); sw.Reset(); sw.Start(); //Direct call by using object for (int a = 0; a < 10000; a++) { obj.CallIt(); } sw.Stop(); Console.WriteLine(string.Format("Time taken by Direct call {0}", sw.ElapsedTicks)); sw.Reset(); sw.Start(); //dynamically compiled lambda call for (int a = 0; a < 10000; a++) { lambdaExpression(obj); } sw.Stop(); Console.WriteLine(string.Format("Time taken by lambda-expression compilation {0}", sw.ElapsedTicks)); sw.Reset(); sw.Start(); //reflection call for (int a = 0; a < 10000; a++) { minfo.Invoke(obj, null); } sw.Stop(); Console.WriteLine(string.Format("Time taken by Reflection call {0}", sw.ElapsedTicks)); Console.ReadLine(); } private static LocalDel<t> FuncCallIt<t>(string MethodName) { var target = Expression.Parameter(typeof(T), "a"); MethodInfo minfo = typeof(T).GetMethod(MethodName, BindingFlags.Instance | BindingFlags.Public); var methodinvokeExpression = Expression.Call(target, minfo); var lambda = Expression.Lambda<localdel><t>>(methodinvokeExpression, new ParameterExpression[] { target }); return lambda.Compile(); } } } |
Following is the result of the execution on my system:
Time taken by Direct call 2384625
Time taken by lambda-expression compilation 902430
Time taken by Reflection call 518534265
I didn’t expect lambda based call to be faster than direct call but I need to explore as how best I can make use of this knowledge in my real programming scenarios. The compiled lambda demands different compiled lambda expressions for each method being used. However, if performance is the prime objective in the application then I think this tangential approach is worth considering.
What is your opinion on it ?
Mark File for deletion on reboot
Requirement: Delete files from a given folder using C#.
Challenge: The file may be in use i.e. locked.
Approach: Use System Call to mark file for deletion on restart
Solution: Use PInvoke for MoveFileEx.
/// /// Consts defined in WINBASE.H /// internal enum MoveFileFlags { MOVEFILE_REPLACE_EXISTING = 1, MOVEFILE_COPY_ALLOWED = 2, MOVEFILE_DELAY_UNTIL_REBOOT = 4, MOVEFILE_WRITE_THROUGH = 8 } /// <summary> /// Marks the file for deletion during next system reboot /// </summary> /// <param name="lpExistingFileName">The current name of the file or directory on the local computer.</param> /// <param name="lpNewFileName">The new name of the file or directory on the local computer.</param> /// <param name="dwFlags">MoveFileFlags</param> /// <returns>bool</returns> /// <remarks>http://msdn.microsoft.com/en-us/library/aa365240(VS.85).aspx</remarks> [System.Runtime.InteropServices.DllImportAttribute("kernel32.dll",EntryPoint="MoveFileEx")] internal static extern bool MoveFileEx(string lpExistingFileName, string lpNewFileName, MoveFileFlags dwFlags); //Usage for marking the file to delete on reboot MoveFileEx(fileToDelete, null, MoveFileFlags.MOVEFILE_DELAY_UNTIL_REBOOT);
The file is marked for deletion in registry in following location
HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerPendingFileRenameOperations
Uninstallation from C#
I wanted to programatically uninstall an application installed in my system. Following code is a working piece to uninstall any custom application from the system.
Note: Following code snippet is not the best example of good coding standards ! It was just a sample I was trying to use to achieve something. The code is a mixture of coding styles lifted across various sites. Please excuse for that
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | private static int Uninstall() { Microsoft.Win32.RegistryKey Fregistry = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE") .OpenSubKey("Microsoft").OpenSubKey("Windows").OpenSubKey("CurrentVersion") .OpenSubKey("Installer").OpenSubKey("UserData") .OpenSubKey("S-1-5-18").OpenSubKey("Products"); string[] Names = Fregistry.GetSubKeyNames(); string uninstall = ""; string ApplicationName = "My Custom Application"; for (int i = 0; i < Names.Length; i++) { Microsoft.Win32.RegistryKey FTemp = Fregistry.OpenSubKey(Names[i]).OpenSubKey("InstallProperties"); if (FTemp == null) continue; string appFound = (FTemp.GetValue("DisplayName") == null) ? String.Empty : FTemp.GetValue("DisplayName").ToString(); if (!String.IsNullOrEmpty(appFound)) System.Diagnostics.Trace.WriteLine("Found Software: " + appFound); if (String.Compare(appFound, ApplicationName, true) == 0) { String installedVersion = FTemp.GetValue("DisplayVersion").ToString(); if (!installedVersion.StartsWith("3.1."))//can be removed if only one version is installed continue; object obj = FTemp.GetValue("UninstallString"); if (obj == null) uninstall = ""; else uninstall = obj.ToString(); i = Names.Length; } } if (String.IsNullOrEmpty(uninstall)) { return -1; } System.Console.WriteLine(uninstall); System.Diagnostics.Process FProcess = new System.Diagnostics.Process(); string temp = "/passive /x{" + uninstall.Split("/".ToCharArray())[1].Split("I{".ToCharArray())[2]; FProcess.StartInfo.FileName = uninstall.Split("/".ToCharArray())[0]; FProcess.StartInfo.Arguments = temp; FProcess.StartInfo.UseShellExecute = false; FProcess.Start(); FProcess.WaitForExit(); if (FProcess.ExitCode != 0) return -1; else { return 0; } } |
Remote Debugging with VS2008
This is a very common development requirement and many of you might be knowing this thing already. However, I am just putting this information as it might help someone. In a distributed development scenario it becomes imminent to have remote debugging possible lest you have your development environment both on your regular development system and the remote server where the webapp is deployed. However, this problem could be resolved if we try to use remote debugging. It is easy to do with Visual Studio.NET. I am using VS2008 but the same should apply to 2005 as well with minor changes.
All you need to do is to copy C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\Remote Debugger\ folder from your local development system having VS2008 to the remote system which you want to debug. On remote system you can launch msvsmon.exe and also you can create a desktop shortcut for the same to make it easy to launch everytime. The user launching this app must be an admin to provide full previledges else you should use run as to launch this application with administrator rights.
More information could be found from
http://www.wictorwilen.se/Post/How-to-get-Remote-Debugging-work-properly.aspx
Delete .svn folders recursively
Very often scenarios crop up when we want to clean our mammoth source code repository without having to export it elsewhere. I was looking for an elegant way to delete hidden .svn folders which are created by tortoise svn to keep track of the repository state on the client. I found this site which provided an elegant way which precisely meets my requirement.
I modified a minor glitch in the command to enable directory deletion from the folders which contain a space in name.
FOR /F "tokens=*" %%G IN ('DIR /B /AD /S *.svn*') DO RMDIR /S /Q "%%G"
Link: http://blog.snakehit.be/2007/08/29/svn-recursively-delete-svn-folders


