|
Recently (read: near the time I wrote this) a poster was commenting on how the next release of Visual Studio recommends 1GB of available (available, not installed) memory. I threw in a comment about optimization in response - that this was yet another example why optimization is (and likely always will be) important.
A response I received here[^] was interesting. The poster actually seemed to believe that writing efficient code meant that the code would be less maintainable!
Now, I will admit that the poster is younger than I and very likely has not been involved in software nearly as long as I have, but one has to wonder where ideas like that come from. Did (s)he come up with it on his/her own? Did they simply believe some other developer that wrote it? Did they optimize something and by doing so turn the code into a mess?
Developers have to remember - just you cannot do it does not mean that someone else cannot, either. Just because you had some bad experience regarding XYZ does not mean that XYZ is bad. A gun in the hand of a cop or a criminal is still a gun - its application is what matters.
If your only exposure to firearms in by the hands of criminals, or if you have no otherwise positive experiences with them, you are more likely to take a negative position against them, without realizing that this is actually a position of ignorance, because you are not aware of the positive aspects. (Granted, I am making a liberal comparison here.)
The same thing applies to optimization - just because all of your experiences have been bad does not mean that optimization is bad. It just means that you need some positive ones. Wanna get those positive experiences? Try starting with not knocking those that have more (positive) experience than you and know how to apply optimization correctly.
OK - rant over.
Peace!
-=- James Please rate this message - let me know if I helped or not!<HR> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
It is interesting to see how things work in the software development world these days. Once someone mentions a need for efficient code or optimization, someone else always has to jump in with something like:
"Today's compilers optimize enough"
"The speed of today's computers make optimization unimportant"
"[It] already works fast enough"
"Premature Optimization! "
But these responses raise other questions that should be addressed:
Today's compilers optimize what enough? They are pretty decent at things like expression optimization and making specific sections of code work better, but they generally do not help poor design or implementation. For example - they cannot replace a heavyweight/slow approach with a simple/faster one, change normal file access into memory mapped file access, change heap memory to stack, or change a poll-spin-poll/poll-sleep-poll multithreaded implementation to an event-based one. The day we all are using compilers that can automatically (and correctly!) do things like optimize a bubble-sort into a Quicksort is the day we can rely solely on the compiler for optimizations.
The speed of what computer makes it run fast enough? What is your minimum hardware requirement? Note that a minimum requirement should not be the minimum required for the application to launch, but to actually perform useful work with a certain level of performance. (A database implementation is worthless if it will launch on a PII with 256MB of RAM, but will not run so much as execute SELECT 1 without thrashing for 5 seconds.) Developers tend to not develop on their target hardware. We develop on high-end machines and see acceptable performance, but it becomes a dog when put on commodity hardware. Oh, and what else is running on that machine at the same time? Likely lots of other applications written by other developers that also think that they do not have to optimize because "the computer is fast enough."
It works fast enough for who? It may be fast enough for you, but unless you know exactly what every single user is going to be using your product for, and with what amounts of data (if applicable), you cannot be sure. Generally, it is safer to err on the side of "it could be faster" than "it is fast enough." "It could be faster" leaves open the possibility that there is a really big world out there, whereas "it is fast enough" is likely limited to the small area of knowledge you reside in. Besides, when is the last time you heard a user complain: "Damn, your product just runs too fast - you guys need slow it down a bit. I am getting too much done!" Riiiight....
Additionally, those initial responses fail to reflect human reality. Generally people do not realize how slow something is until they get something faster, or more to the point, they do not realize how bad something is until they get something better. Walking was great until we started to ride animals. Horses were great transportation until automobiles. Driving was great until aircraft came along. There is usually always a way to improve things, and most of the time, you are unaware of the benefits until you actually make the change. But once you know something better is available, you tend to want it.
Second, just about everything computer-related is rated by its performance. Faster is better - we all want the workstation that can execute more instructions in less time (i.e. faster), the faster database (what is its TPS?), and ultimately the faster application. We all expect our companies to provide us with large multicore/multi-CPU systems with 4GB of memory and the latest (fastest?) versions of software/applications. If we want the fastest/best, why not our users?
All things being equal, which would you want:
-- To spend $45 on a game that plays on your system at 640x480x8 at 8FPS or one that plays at 1024x768x32 at full-motion speed?
-- To have to render a full-length movie using software that can render 1 frame every minute and a half, or one that can do it in 1 minute?
-- To use a browser that can load and render Code Project pages in an average of 6 seconds each, or one that takes an average of 2 seconds each with no thrashing?
Right. That is what I thought. Case closed.
I think that part of the problem is that developers do not think like users. Developers want the faster machine so that general development tasks go quicker, and a faster toolset for saving time on building larger projects, but seem to rarely think of doing anything to speed up the software that they are working on so that their users get the same benefit. When your product's minimum requirements involve hardware less than 1 year old, you are likely doing something wrong, or you have a very specialized product and target audience.
Another possible part of the problem with software optimization is that it takes one to know one. Taking an example from the paper Unskilled and Unaware of It[^] (from the Journal of Personality and Social Psychology), you have to know proper English grammar in order to (1) produce proper English grammar, and (2) be able to recognize proper English grammar. If you do not know English, you cannot tell if another English sentence is grammatically correct or not. And more to the point, if you have poor English skills, you may be unable to spot a proper English sentence when you see it.
This same thing happens with knowledge of optimization and recognition of things like poor/inefficient coding techniques or poor/inefficient implementations - if you do not know that your approach/implementation is poor and/or inefficient, then you are unlikely to recognize a superior approach/implementation when one is presented to you. Throw in the fact that developers tend to be pretty proud of their code and do not want to hear that it might be less than optimal.
If you do not know about the different kinds of optimization that may (or may not) be required, you cannot recognize that you may have a need for it. This lack of skill often manifests itself in a resistive attitude, which usually starts with at least one of the three statements mentioned above. People that have more of an academic than practical background also seem to fall victim to the belief that optimization is not important, usually citing the same above reasons.
Also, beware when someone says the words premature optimization. This is often the response from someone with a lack of real-world experience (or at least less than you). Look at it this way... If you have an application that grinds to a halt each time the user performs a sort on something, and you have a look at the code and see a bubble sort implementation being used to sort 1000+ random items, you do not need to profile the application just to state that the bubble sort is causing a performance problem.
Just like how you learned in school that a bubble sort has poor performance and Quicksort has better performance, you also learn through years of experience and wisdom that this way of doing it is far less optimal than that way of doing it. Recent graduates seem to have the most trouble understanding the value of experience over schooling, and it seems that the higher the degree, the longer it takes to gain that understanding and to respect it.
A final note - if someone comments that your implementation is poor, never reply with something stupid like "Well, if you cannot do anything better, you should not say anything at all!" That is the #1 indicator that you lack the skills to understand your own limitations and thus recognize a better approach.
And as an example of how stupid saying something like that is, if the automatic transmission in your new car started slipping, what would you think if when you called your dealer's service department about it, they responded by saying that since you lack the ability to build a better transmission you should not be commenting on one you have. Stupid, right? You do not have to know how to build a better mousetrap to know that the one you have is working poorly. The same goes for your users.
Peace!
-- modified at 8:29 Monday 12th March, 2007
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
This post will keep a collection of code snippets derived from actual code that do things for no good reason... Unless, of course, the developer was intentionally trying to create a performance hit.
<code>
LPCTSTR cpMessage = _T( "This is the message." );
LPCTSTR cpCap = _T( "This is the caption" );
CString sMessage( cpMessage );
CString sCap( cpCap );
::MessageBox( sMessage, sCap, ( MB_ICONINFORMATION | MB_OK ) ); So, what is wrong with this code? This code demonstrates a classic abuse of dynamically allocated memory by way of CString misuse. CString s are handy objects, but the only reason you should ever create (or require, see #2 below) a wrapper object, especially one that dynamically allocates resources, is if you take advantage of the functionality of the object.
In this case, there is nothing CString -specific that is being done here, so the CString object is being wasted. Additionally, since it takes more time to manage dynamically allocated memory required for the CString , it causes a waste of time for no reason. Lastly, if this is used in an multi-threaded application, depending on where code like this is being used, it can cause contention between multiple threads that need to access the shared heap. (Which once caused an obvious performance problem in an application I had to correct that was using multiple CString s in more than 80 ON_COMMAND_UPDATE_UI functions!)
<code>
void CSomeClass::SetStatusText( CString sStatus )
{
m_scTheStatusCtrl.SetWindowText( sStatus );
return;
} So, what is wrong with this code? Basically the same thing as above. This code is especially nasty because you have now taken a bad code decision, and moved it to the design level. Not only are you using a CString for no good reason, but now you are requiring the caller of your code to satisfy the requirements of your poorly-designed function (changing to a reference does not help here at all).
A good rule of thumb: always use the "simplest" type required. If you can use a HWND , do not require a CWnd ; if you can use LPCTSTR , do not require a const CString& .
If you really do need a higher-level (wrapper) object, you can always manage one inside of the function's code. You could also create two versions of the functions, one that takes a simpler type and one that takes the higher-level one. This is often better than just creating one version of the function that requires the higher-level type because you may be able to optimize the otherwise temporary higher-level object (static variable, member variable of containing class, etc.)
Got a real, valid reason why nothing is wrong with any of the above examples, I would love to hear about it! N.B. the "Java argument" about how the performance difference is negligible on modern hardware is not a valid argument - it is inappropriate to make assumptions about the hardware being used. Besides, that argument acknowledges the difference in performance, which is what this is all about, anyway.
Peace!
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites Please rate this post appropriately!
|
|
|
|
|
A recent discussion raised the fact that C#, often called "See Sharp" is technically incorrect because the symbol used is not the musical symbol sharp, but is the octothorpe, also known as Pound, Hash, Splat, Number Sign, etc.
An example of the differences is shown here (provided your browser and current font set support it). The character above the 3 key on most US keyboards is this: # - but the musical sharp symbol is this: ♯. See the difference? Simply, one has angled vertical lines ("pound"), but the other has angled horizontal lines ("sharp").
With the semantics out of the way, how then should we pronounce "C-Sharp" when we see it written in shorthand as "C#"? There are two schools of thought on this from the pedants<super>* and those that are happy to go through life doing things incorrectly - the first want to call it as it is written, which raises interesting names ranging from C-Splat to cocktothorpe. The rest want to call it what Microsoft named it, regardless of how it is written - which is like me writing my name using the US alphabet as MIXILPLIX and demanding that you pronounce it as "James". (Now, if I wrote it in whatever native alphabet results in it being pronounced correctly, that is another thing - so what alphabet is Microsoft using?)
(*) Pedants - a word used to describe those that believe that paying attention to the details and being correct is important ("pedantic" describes this behavior) - these words are most often used by those that do not.
Peace!
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Tip for new SUV drivers: Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
(*) Pedants - a word used to describe those that believe that paying attention to the details and being correct, is important ("pedantic" describes this behavior) - these words are most often used by those that do not.
As a card carrying pedant I feel I must point out that the comma is superfluous.
'--8<------------------------
Ex Datis:
Duncan Jones
Merrion Computing Ltd
|
|
|
|
|
...And there 'ya have it!
Corrected, thanks!
Peace!
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Tip for new SUV drivers: Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
James R. Twine wrote: but the musical flat symbol is this: ♯.
That's the sharp symbol. Clickety[^] for more info.
Jeremy Falcon
|
|
|
|
|
Holy $h1t - I cannot believe that I wrote that as flat instead of "sharp" above and never even noticed!
Thanks for that - I corrected the text...
Peace!
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
I find it rather amusing that even (so-called) experienced developers will use fundamentally different concepts interchangeably.
For example, I have seen documentation by developers that mention nul columns in a database table, or worse yet, NULL -terminated strings, and NUL pointers.
Now, some that simply miss the point will be saying something like: "In C++, NULL is zero, and the NUL ASCII code has a value of zero, so they are the same thing!"
Wrong. The values of the two identifiers may be the same, but not their meanings, and thus, it is inappropriate to use them interchangeably. Here is an example. In the VC++ environment, the following identifiers are present:
<br />
NULL = 0<br />
COLOR_SCROLLBAR = 0<br />
MB_DEFBUTTON1 = 0<br />
TRUE = 1<br />
MB_OKCANCEL = 1<br />
IDOK = 1<br />
COLOR_BACKGROUND = 1<br />
But just because some of them have the same value does not mean that it is correct to do something like this:
<br />
int iRet = COLOR_SCROLLBAR; <FONT COLOR="DarkGreen">
<br />
iRet = ::MessageBox( <br />
"Please press [OK] to continue or [Cancel] to cancel the delete operation.", <br />
"Please Confirm Delete", <br />
COLOR_BACKGROUND | NULL ); <FONT COLOR="DarkGreen">
if( iRet == TRUE ) <FONT COLOR="DarkGreen">
{<br />
}<br />
According to the naive the identifiers have the same value argument, the code above is perfectly fine. Of course, as demonstrated above, that is a bull$hit argument. After all, what would you think if you saw code written like that?
Oh, and BTW... Languages like C# notwithstanding, null (lowercase) is generally used to indicate the absence of a value in a database column. RDBMSes have been using it much longer than C#.
-=- James
Tip for inexperienced drivers: "Professional Driver on Closed Course" does not mean "your Dumb Ass on a Public Road"! Articles -- Products: Delete FXP Files & Check Favorites
|
|
|
|
|
What do you mean NUL pointers? Were you referring to the bit bucket(NUL Device)?
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|
|
|
|
|
James R. Twine wrote: Why
Obivious reason: Your Impeccable service to the VC++ forum.
Press: 1500 to 2,200 messages in just 6 days? How's that possible sir?
Dr.Brad :Well,I just replied to everything Graus did and then argued with Negus for a bit.
|
|
|
|
|
Heh - saying "Why, thank you!" is like saying "Hey, thanks!", but thank you for the complement anyway.
Peace!
-=- James Please rate this message - let me know if I helped or not!<HR> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
James R. Twine wrote: saying "Why, thank you!" is like saying "Hey, thanks!
Huh, you cannot be stopped from teaching something. !!! . Thanks Thanks
Press: 1500 to 2,200 messages in just 6 days? How's that possible sir?
Dr.Brad :Well,I just replied to everything Graus did and then argued with Negus for a bit.
|
|
|
|
|
Well, you *are* an MVP now.
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|
|