|
When I write some new code or maintain some old code, I usually aim to make it as simple to read as possible, I go out of my way to make my code very easy to understand, and it usually pays off.
|
|
|
|
|
surly this will help others who watch this code. Apart from this, you will also get free to read it fast.
Sreejith Nair
[ My Articles ]
|
|
|
|
|
True Story:
I work at one of the largest contract electronics manufacturing companies in the world, and one of my responsibilities is to develop bar code label programs for our customer's products. When I first arrived I was horrified to find out that all of their bar code programs are still done on old DOS 16-bit machines, which are not networked. They literally have 200+ DOS PC's all holding mission critical data, which is not backed up anywhere, on old machines that barely run. When I looked at the programs, I was skeptical that the person who actually wrote them had a recent Computer Science degree, since the code was written at a level I would expect of a high school student.
What added insult to injury was this person was going to be my supervisor and I was required to program in the exact same manner that she did, and in so doing it usually took on the order of 3 days to write any given program. Over time I worked on improving the readability of the code and changing it so that all the highly repetitive sections are put into standard function call (all pretty basic stuff from CS 101). These improvements led to a library that when used allowed the same programs to be written in 3 hours instead of 3 days, and could be readily understood by people with only a basic understanding of C++. When my supervisor found out, she went ballistic, even though there was only ever 1 bug in the entire system (which took only 5 minutes to find and fix) she went to her boss and tried to claim that I had destroyed their entire system and none of the programs worked. I got chewed out BIG TIME even though I could prove that the programs' functionality had not been changed, only its readability, size, and speed. All programs that use the new library work great, but now I am only allowed to use it for programs that I write, and cannot change existing programs. My supervisor still codes in her old fashion. Funny, in the same time period of 3 months I have finished 20 projects and she has only finished 1, and it has lots of bugs. Yet she still remains adamant that her way is superior and that my method is totally wrong, go figure.
Daniel Petersen
|
|
|
|
|
At the end of the day, you probably feel good about what you are doing, and expect someone to take over your code w/o going through the headaches you mention you had, when you started to dig in the old code.
When I started, too, I was blammed for "rewriting everything". (Of course, I did not rewrite everything, but thats another story)
|
|
|
|
|
...Not necessarily waiting until it's "critical", but not right off the bat, either. For UI work, if response time doesn't "feel" right (on the typical system), it'll get optimized until it does. For all other systems, if it'll waste someone's time, it gets changed to waste less time.
Rule of thumb: if i want (or get asked) to add a wait cursor or progress dialog, it could probably use some optimization. Otherwise, i'm better off spending time elsewhere.
"The time has come," the Walrus said,
"To talk of many things..."
|
|
|
|
|
As an embedded programmer, I too find stock functions too inefficient. I have a slew of numeric<->string converters for such systems (and desktops), that are fractions of the size of printf-like funcs, and several hundred percent faster. Sometimes some assembly is required (pun intended), but if I can use a decent optimising compiler, I can get away without it.
I've had bosses that complain I "waste too much time on such things", while I watch other waste weeks trying to figure out why their MCU doesn't respond within the required timeframe! They come to me to fix things, and as such "I'm" not producing fast enough. Go figure.
Cheers
"There's intelligence out there, somewhere..."
|
|
|
|
|
"I've had bosses that complain I "waste too much time on such things", while I watch other waste weeks trying to figure out why their MCU doesn't respond within the required timeframe! They come to me to fix things, and as such "I'm" not producing fast enough. Go figure."
You friiiiiiiennnnnnnnd, buddyyyyyyyyyyy of misfortune !
Kochise
In Code we trust !
|
|
|
|
|
I've read this article http://www.flounder.com/optimization.htm written by J.M. Newcomer and I think I'm not experienced enough to switch on the optimization buttons of my compiler.
best regards
Thomas
|
|
|
|
|
<snort>
Yeah, after reading some of Joe's articles, anyone could end up feeling completely incompetent, and that all coding duties should be turned over to Joe, who did every task imaginable back on PDP-11's in the 1970's.
|
|
|
|
|
Hi,
thats it! With your words.... everything is said! Let's stop programming and let it all for Joe and maybe for Paul Di Lascia IF Joe needs some spare time to sleep.......
best regards
Thomas
|
|
|
|
|
And I will continue to do so, until the number one complaint changes from "Damn, this thing is slow!" to something else.
IME, complaints about speed and stability are the most often heard complaints, both of which, IMHO, are usually due to sloppy or lazy development practices.
Peace!
-=- 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
|
|
|
|
|
James R. Twine wrote:
And I will continue to do so, until the number one complaint changes from "Damn, this thing is slow!" to something else.
That's great! I feel your pain.
|
|
|
|
|
I will frequently optimise any code that works over the wire... there are still a lot of slow networks out there...
An expert is somebody who learns more and more about less and less, until he knows absolutely everything about nothing.
|
|
|
|
|
The golden rule is to use a simple non-optimised algorithm initially, and only optimise it later if it turns out to be too slow.
However, sometimes I optimise from the begining if it's obvious that that non-optimised version is going to be inadequent. Also, if the optimised code takes no additional effort to code then I always use the optimised version.
|
|
|
|
|
I optimize exactly once. If I need to optimize an algorithm, I do it. And then that's how I'll write that algorithm from that point on, unless I really can't for some reason. This applies to patterns (algorithms with loose semantics).
I mean, why not reuse something that's proven to work reasonably fast?
--
Weiter, weiter, ins verderben.
Wir müssen leben bis wir sterben.
I blog too now[^]
|
|
|
|
|
Most optimizations can be down to good programming style.
Always using const references for parameters where appropriate etc.
Calculating intermediate results only the once etc.
Other than that, with todays PC speed, I dont think you really have to worry about it.
If you vote me down, my score will only get lower
|
|
|
|
|
Here's a question:
Roger Allen wrote:
Always using const references for parameters where appropriate etc.
I work with a guy who's a rabid over-optimizer. He also reviles the use of const, saying that he does not want to place any restrictions on his implementations.
I've given up on trying to get him to use const as a matter of good coding style, or as a courtesy to others on the team; however, I've thought about approaching this from the optimzation standpoint - but I don't fully understand how const references can improve code performance. Can you enlighten me on this, or point me to some references on the subject?
Thanks -
|
|
|
|
|
He can't be a rabid over-optimiser if he doesn't use const, he probably just fancies himself as an optimiser.
Const references are one of the most important optimisation hints, without which, the [compiler's] optimiser may just toss dice to decide which objects and pointers to keep in registers. VC++ no longer honours the register keyword, so const is all we have left.
To the optimiser, const is somewhat the opposite of register; instead of keeping the object in a register, it may specifically decide that object does not need to be, since it never changes. Thus, other objects are more likely to be kept in registers. Since more than one operation can execute on modern chips at once, things can be assigned from stack vars with little or no loss if there's something else that can be done in the meantime. But that's only part of the story.
const objects may also be temporary, can be calculated as needed (sometimes concurrently with other operations, to promote parallelism, which can double efficiency), and provide several other hints. In other words, const is an invaluable optimisation hint. If this guy really loves to optimise (he really should be judicious, since many things are done automatically by the compiler), he would step through critical loops in disassembly view as I do, and notice the benefits.
Personally, I've found const saved my arse many times, too. In coding frenzies (read "deadline"), I've occassionally had the compiler squawk that I tried to modify a const object, which forces me to fix it and avoid nasty bugs.
Cheers
|
|
|
|
|
IIRC, const references/const objects are generally not more optimized than ordinary objects. I read an article on this by some C++ hot shot, but I don't remember where I read it.
const-correctness is a design strategy, enforcing read-only behaviours.
const int and the like are however replaced with compile time constants.
When I do pure C++, I try to use const as much as possible. It makes my code safe from feeble coding attempts on a late friday afternoon. However, I've been doing a lot of COM lately, where I can't use const in all it's splendor. I miss const.
--
Weiter, weiter, ins verderben.
Wir müssen leben bis wir sterben.
I blog too now[^]
|
|
|
|
|
Jörgen Sigvardsson wrote:
I read an article on this by some C++ hot shot, but I don't remember where I read it.
You read it here[^], I guess.
And yes, I agree. Const-correctness is about correctness (pun intended), not optimization. But hey, if the only way to make a guy use consts is to tell him it is good for optimizations, then just tell him it is good for optimizations
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
|
Nemanja Trifunovic wrote:
But hey, if the only way to make a guy use consts is to tell him it is good for optimizations, then just tell him it is good for optimizations
Child psychology for übergeeks 101..
When I think about it, const can be used as a tool for optimization. You can pass object parameters as const references, thus forcing the function to not modify the parameter or make a copy of it before it can do any modifications. 4 bytes on the stack vs X > 4 is an optimization I guess. But it's hardly because of some compiler magic, it's a purely hardware thingie.
Hmm.. a const ref is probably 8 bytes big on a IA-64 machine. Which could mean that passing const refs on such hardware is potentially less efficient than passing the entire object if it's smaller than 8 bytes. I can already see optimizing geeks writing macros like
#ifdef __IA64__
#define CONST_REF(T) const T
#else
#define CONST_REF(T) const T&
#endif
void MyIA64OptimizedFunction(CONST_REF(ClassName) param)
{
...
}
template <typename T, bool RefYesNo>
struct ConstRefMaybeHelper {
typedef const T& type;
};
template <typename T>
struct ConstRefMaybeHelper<T, false> {
typedef const T type;
};
template <typename T>
struct ConstRefMaybe {
typedef ConstRefMaybeHelper<T, sizeof(T) >= sizeof(T*)>::type type;
};
void MyMuchCoolerIA64OptimizedFunction(ConstRefMaybe<ClassName>::type param)
{
...
}
--
Weiter, weiter, ins verderben.
Wir müssen leben bis wir sterben.
I blog too now[^]
|
|
|
|
|
Nemanja Trifunovic wrote:
You read it here[^], I guess.
If that's the article, then it actually does point out how const benefits optimisation in certain cases. Mostly in just examining very simple cases does it find no added benefit, as I mentioned elsewhere.
Curiously, he doesn't discuss loop constructs at all, where optimisers can really be put to the test (assuming a good-sized loop). He mostly talks about passing const pointers/references/objects, where relatively little execution time may occur. Bottlenecks frequently occur in multi-line loops, that involve several objects (high register contention). The majority of truly beneficial optimisations I've done over the years, have been within loops.
|
|
|
|
|
Jörgen Sigvardsson wrote:
IIRC, const references/const objects are generally not more optimized than ordinary objects. I read an article on this by some C++ hot shot ...
This depends on the code's complexity and the compiler itself. If there's little register contention or the optimiser can already figure out the best way to do things, const hints are superfluous.
However, when there's lots of register contention (specifically when the target is an x86, with only six general-purpose registers - seven if you omit frame pointers), the optimiser can make better use of hints like const. If someone writes a relatively simple algorithm, and compares assembler output with and without const, it would seem like there's little point in bothering, as the outputs are likely identical. Of course, that would be like concluding that since one's never been in a car accident, that air bags are not important.
There's little question, as you and other posters have pointed out, that const's greatest benefit is in protecting objects and avoiding nasty bugs. Yet, providing the compiler's optimiser with as many hints as possible, can improve the likelihood of more efficient output. Staying in the habit of using const means one never has to waste time thinking about whether it will matter in a particular case. So programmers like us never lose out.
BTW, I too miss const in situations where I can't use it.
Cheers
|
|
|
|
|
Jeff J wrote:
Staying in the habit of using const means one never has to waste time thinking about whether it will matter in a particular case. So programmers like us never lose out.
Except when we have to deal with other programmers who absolutely refuse to use it, and who throw fits if "their" code is modified...
|
|
|
|
|
Jim A. Johnson wrote:
programmers who absolutely refuse to use it
To be honest, this sounds strange to me. We have coding standards and code reviews, and if someone "refused" to follow the coding standards it would not (ehm) be very good for his career.
I think you guys have organizational issues if something like this can happen.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
|