Variables vs. On-The-Fly Programming

I

im2dxtreme

I have included two pseudo-code examples below. The first shows me
allocating memory via a variable and using it repeatedly. The second
shows me taking the "direct route" to the data.

For a web application performing multiple operations in a short amount
of time, which procedure would maximize speed on the server? (Is it
better to use a variable? Is it best to use the "direct route"? Or is
it a moot point because they both use the same resources to get where
they need to go anyway?)

Please note that I am looking for answers *ONLY* pertaining to server
speed and resources. I understand that variables are "easier" for a
programmer to use, but I want to know what makes the server run
smoother.

Thanks in advance!


EXAMPLE 1:

(coll1 is a collection of directory strings...)

Dim temp as String = New IO.DirectoryInfo(coll1.Item(i)).name

if InStr(temp, "xyz") then...
if temp = "xyz123" then...
if Right(temp, 3) = "123" then...


EXAMPLE 2:

(coll1 is a collection of directory strings...)

if InStr(New IO.DirectoryInfo(coll1.Item(i)).name, "xyz") then...
if New IO.DirectoryInfo(coll1.Item(i)).name = "xyz123" then...
if Right(New IO.DirectoryInfo(coll1.Item(i)).name, 3) = "123" then...
 
J

Jay B. Harlow [MVP - Outlook]

For a web application performing multiple operations in a short amount
of time, which procedure would maximize speed on the server? (Is it
better to use a variable? Is it best to use the "direct route"? Or is
it a moot point because they both use the same resources to get where
they need to go anyway?)
Moot point! See 80/20 rule below.

If I really needed the explaining variable (your temp) I would include it,
if I didn't need it I would not. Need of the explaining variable is based on
readability of the code, not whether it performed better or not. The
compiler or the JIT during optimization may use a temp variable if you don't
include it, or it may ignore the temp variable if you do, hence I would not
be concerned with it unless its been identified & proven (see 80/20 rule
below)

http://www.refactoring.com/catalog/introduceExplainingVariable.html
http://www.refactoring.com/catalog/inlineTemp.html
http://www.refactoring.com/catalog/replaceTempWithQuery.html


Consider the 80/20 rule! That is 80% of the execution time of your program
is spent in 20% of your code. I will optimize (worry about performance) the
20% once that 20% has been identified & proven to be a performance problem
via profiling (CLR Profiler is one profiling tool).

For info on the 80/20 rule & optimizing only the 20% see Martin Fowler's
article "Yet Another Optimization Article" at
http://martinfowler.com/ieeeSoftware/yetOptimization.pdf



Please note that I am looking for answers *ONLY* pertaining to server
speed and resources. I understand that variables are "easier" for a
programmer to use, but I want to know what makes the server run
smoother.


Info on the CLR Profiler:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html/scalenethowto13.asp

http://msdn.microsoft.com/library/d...y/en-us/dndotnet/html/highperfmanagedapps.asp

In addition to the above articles, The following articles provide
information on writing .NET code that performs well. I believe one of the
covers how to use Performance Monitor under Control Panel to monitor the
memory usage of your app.

http://msdn.microsoft.com/architecture/default.aspx?pull=/library/en-us/dnpag/html/scalenet.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/fastmanagedcode.asp

http://msdn.microsoft.com/library/d...y/en-us/dndotnet/html/highperfmanagedapps.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/vbnstrcatn.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchperfopt.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/dotnetperftechs.asp


Hope this helps
Jay
 
J

Jay B. Harlow [MVP - Outlook]

I should add, that ACT (Application Center Test) is a utility included with
VS.NET allows you to stress test & performance test you web applications.

"Performance Testing - Microsoft .NET Web Applications" from MS Press by
Microsoft Application Consulting and Engineering (ACE) Team provides a
wealth of information on performance & stress testing your applications.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top