Detecting a right way to use variables in .Net
-
Hi, I am using Dim str as string frequently in my windows application mostly to fill any dataset to query anything and more. basically how I use this is as follows: Dim dSetTemp As New DataSet Dim str As String str = "select trans_vchr.trans_vchr_id, trans_vchr.session_cfee_id, blah blah...." dSetTemp = clscmn.Fill_DS(str) --> fill_ds is a function created to increase the productivity. Here if I like I can easily do the same like this dSetTemp = clscmn.Fill_DS("select trans_vchr.trans_vchr_id, trans_vchr.session_cfee_id, blah blah....") Here my question is whether I use str variable or not, actually I use the variable as it make my debugging so easy I just copy the string and paste in sql to test the query. Here that gives me productivity while debugging but if it is at the cost of performance. I would compromise with debugging facility or If it is negligible, it can be left. Can you suggest what will be best for me. I like to save bytes to be loaded in memory and like to have best performance. Thanks in Advance. Jay Khatri
-
Hi, I am using Dim str as string frequently in my windows application mostly to fill any dataset to query anything and more. basically how I use this is as follows: Dim dSetTemp As New DataSet Dim str As String str = "select trans_vchr.trans_vchr_id, trans_vchr.session_cfee_id, blah blah...." dSetTemp = clscmn.Fill_DS(str) --> fill_ds is a function created to increase the productivity. Here if I like I can easily do the same like this dSetTemp = clscmn.Fill_DS("select trans_vchr.trans_vchr_id, trans_vchr.session_cfee_id, blah blah....") Here my question is whether I use str variable or not, actually I use the variable as it make my debugging so easy I just copy the string and paste in sql to test the query. Here that gives me productivity while debugging but if it is at the cost of performance. I would compromise with debugging facility or If it is negligible, it can be left. Can you suggest what will be best for me. I like to save bytes to be loaded in memory and like to have best performance. Thanks in Advance. Jay Khatri
IMO the cost difference is very small and irrelevant; the debugging comfort is a perfect reason for using the string variable. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
IMO the cost difference is very small and irrelevant; the debugging comfort is a perfect reason for using the string variable. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
Hi, I am using Dim str as string frequently in my windows application mostly to fill any dataset to query anything and more. basically how I use this is as follows: Dim dSetTemp As New DataSet Dim str As String str = "select trans_vchr.trans_vchr_id, trans_vchr.session_cfee_id, blah blah...." dSetTemp = clscmn.Fill_DS(str) --> fill_ds is a function created to increase the productivity. Here if I like I can easily do the same like this dSetTemp = clscmn.Fill_DS("select trans_vchr.trans_vchr_id, trans_vchr.session_cfee_id, blah blah....") Here my question is whether I use str variable or not, actually I use the variable as it make my debugging so easy I just copy the string and paste in sql to test the query. Here that gives me productivity while debugging but if it is at the cost of performance. I would compromise with debugging facility or If it is negligible, it can be left. Can you suggest what will be best for me. I like to save bytes to be loaded in memory and like to have best performance. Thanks in Advance. Jay Khatri
Yeah, I agree with Luc, this seems like such a small impact that the benefits of being able to 'see' the string in the debugger might outweigh the efficiency of the hard coded string... But if you ever have questions like this again you can also do a few things... 1) Code it both ways and open the executables up in reflector to see the IL. That sometimes shows you that no matter how you 'code' it, the compiler may just be optimizing it out for you. 2) Code it both ways and throw it in a huge loop then run it a few thousand times to get some performance numbers. See how it uses memory. See how the GC acts, etc...
-
Yeah, I agree with Luc, this seems like such a small impact that the benefits of being able to 'see' the string in the debugger might outweigh the efficiency of the hard coded string... But if you ever have questions like this again you can also do a few things... 1) Code it both ways and open the executables up in reflector to see the IL. That sometimes shows you that no matter how you 'code' it, the compiler may just be optimizing it out for you. 2) Code it both ways and throw it in a huge loop then run it a few thousand times to get some performance numbers. See how it uses memory. See how the GC acts, etc...
-
nice one ray and impressive too. Thanks alot for your anticipation. I have more queries which arise time by time. Hope I will get this kind of reply then. Best Regards Jay Khatri
Ray is correct, but he left out one little detail. If you run the code compiled under Debug build, optimizations are turned off. This can make the code run a bit differently (and slower) in Debug then it would in Release. So if you want to test this, make sure you compile a Release build.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Ray is correct, but he left out one little detail. If you run the code compiled under Debug build, optimizations are turned off. This can make the code run a bit differently (and slower) in Debug then it would in Release. So if you want to test this, make sure you compile a Release build.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Ray is correct, but he left out one little detail. If you run the code compiled under Debug build, optimizations are turned off. This can make the code run a bit differently (and slower) in Debug then it would in Release. So if you want to test this, make sure you compile a Release build.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Not a problem. It's something that I was reminded of about a week ago. ;)
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Hi, I am using Dim str as string frequently in my windows application mostly to fill any dataset to query anything and more. basically how I use this is as follows: Dim dSetTemp As New DataSet Dim str As String str = "select trans_vchr.trans_vchr_id, trans_vchr.session_cfee_id, blah blah...." dSetTemp = clscmn.Fill_DS(str) --> fill_ds is a function created to increase the productivity. Here if I like I can easily do the same like this dSetTemp = clscmn.Fill_DS("select trans_vchr.trans_vchr_id, trans_vchr.session_cfee_id, blah blah....") Here my question is whether I use str variable or not, actually I use the variable as it make my debugging so easy I just copy the string and paste in sql to test the query. Here that gives me productivity while debugging but if it is at the cost of performance. I would compromise with debugging facility or If it is negligible, it can be left. Can you suggest what will be best for me. I like to save bytes to be loaded in memory and like to have best performance. Thanks in Advance. Jay Khatri
I generally go with whatever is more readable (if its a long string and i use it inline then it pushes the rest of the method off the screen) and easiest to debug. One thing to consider though, if you have a string that is potentially being declared inline then that string is a constant, why not make it a constant? As part of my 'final tidyup' i generally go through my code looking for " characters and numbers, if i find any i ask myself why they aren't constant, 9 times out of 10 they should be :D