Friday, September 29, 2006
C++ Optimizations
These optimizations are fairly easy to apply to existing code and in some cases can result in big speedups. Remember the all-important maxim though, the fastest code is code that isn't called.
Use Initialization Lists
Always use initialization lists in constructors. For example, use
TMyClass::TMyClass(const TData &data) : m_Data(data)
{
}
rather than
TMyClass::TMyClass(const TData &data)
{
m_Data = data;
}
Without initialization lists, the variable's default constructor is invoked behind-the-scenes prior to the class's constructor, then its assignment operator is invoked. With initialization lists, only the copy constructor is invoked.
Optimize For Loops
Whereever possible, count down to zero rather than up to n. For example, use
for (i = n-1; i >= 0; --i)
rather than
for (i = 0; i < n; ++i)
The test is done every iteration and it's faster to test against zero than anything else. Note also that
++i
is faster than
i++
when it appears in the third part of the for loop statement.
Use 'int'
Always use the int data type instead of char or short wherever possible. int is always the native type for the machine.
Make Local Functions Static
Always declare local functions as static, e.g.,
static void foo()
This means they will not be visible to functions outside the .cpp file, and some C++ compilers can take advantage of this in their optimizations.
Optimize If Statements
Factor out jumps. For example, use
bar();
if (condition)
{
undoBar();
foo();
}
rather than
if (condition)
{
foo();
}
else
{
bar();
}
Use a profiler and good judgement to decide if undoing the bar() operation is faster than jumping.
Optimize Switch Statements
Put the most common cases first.
Avoid Expensive Operations
Addition is cheaper than multiplication and multiplication is cheaper than division. Factor out expensive operations whereever possible.
Initialize on Declaration
Whereever possible, initialize variables at the time they're declared. For example,
TMyClass myClass = data;
is faster than
TMyClass myClass;
myClass = data;
Declaration then initialization invokes the object's default constructor then its assignment operator. Initializing in the declaration invokes only its copy constructor.
Pass By Reference
Always try to pass classes by reference rather than by value. For example, use
void foo(TMyClass &myClass)
rather than
void foo(TMyClass myClass)
Delay Variable Declarations
Leave variable declarations right until the point when they're needed. Remember that when a variable is declared its constructor is called. This is wasteful if the variable is not used in the current scope.
Use 'op='
Wherever possible, use 'op=' in favour of 'op'. For example, use
myClass += value;
rather than
myClass = myClass + value;
The first version is better than the second because it avoids creating a temporary object.
Inline Small Functions
Small, performance critical functions should be inlined using the inline keyword, e.g.,
inline void foo()
This causes the compiler to duplicate the body of the function in the place it was called from. Inlining large functions can cause cache misses resulting in slower execution times.
Use Nameless Classes
Whereever possible, use nameless classes. For example,
foo(TMyClass("abc"));
is faster than
TMyClass myClass("abc");
foo(myClass);
because, in the first case, the parameter and the class share memory.
Wednesday, September 27, 2006
25 Signs That, Sadly, You've Grown Up
2. Having sex in a twin bed is out of the question.
3. You keep more food than beer in the fridge.
4. 6:00 AM is when you get up, not when you go to bed.
5. You hear your favorite song on an elevator.
6. You watch the Weather Channel.
7. Your friends marry and divorce instead of hook up and break up.
8. You go from 130 days of vacation time to 14.
9. Jeans and a sweater no longer qualify as "dressed up."
10. You're the one calling the police because those damn kids next door won't turn down the stereo.
11. Older relatives feel comfortable telling sex jokes around you.
12. You don't know what time Taco Bell closes anymore.
13. Your car insurance goes down and your payments go up.
14. You feed your dog Science Diet instead of McDonalds leftovers.
15. Sleeping on the couch makes your back hurt.
16. You no longer take naps from noon to 6 PM.
17. Dinner and a movie is the whole date instead of the beginning of one.
18. Eating a basket of chicken wings at 3 AM would severely upset, rather than settle your stomach.
19. You go to the drug store for ibuprofen and antacid, not condoms and pregnancy tests.
20. A $4.00 bottle of wine is no longer "pretty good stuff".
21. You actually eat breakfast food at breakfast time.
22. "I just can't drink the way I used to," replaces, "I'm never going to drink that much again."
23. 90% of the time you spend in front of a computer is for real work.
24. You drink at home to save money before going to a bar.
25. You read this entire list looking desperately for one sign that doesn't apply to you and can't find one to save Your sorry old ass.
Friday, September 15, 2006
MASTERING THE INTERVIEW By Sean Bosker
The job interview is your proving ground, the place where you must demonstrate why you are the best person for the job. Making that powerful statement that you're the best of all the candidates requires the three Ps: Preparation, Presentation and Perception. Warren Davis, the Director of Recruiting and Employment for RadioShack, emphasizes this point. “Your resume and application are fair game. Candidates should study themselves and the company with whom they’re interviewing.” Read industry trade magazines, visit the company web site, and do a company search on Yahoo! Finance to find current news about your prospective employer. Be prepared to demonstrate what you know about the company and the industry. Michele Stagg, the Director of Human Resources at Banana Republic, says she is consistently impressed when candidates work their skills into the context of company news. “The more an informed candidate can tie past experience to the requirements of the job they are interviewing for, particularly in terms of what the company is doing, the better.” Another important part of preparation is making sure you look the part. Choosing what you wear is so important that it deserves its own article - Interview in Style. Effective presentation includes being in the right place, at the right time. If you're late for the interview, you could inadvertently tell your interviewer that you're not right for the job. Once you arrive, introduce yourself to the receptionist and turn off that cell phone. “Having a phone go off during an interview is a real turn off,” says Ackerson. According to psychologist Albert Mehrabian, more than half of our communication is nonverbal or body language. Stagg agrees. “Body language is exceptionally important. Positive, upright and open body language shows self confidence and interest.” During introductions give a firm handshake and then take a seat facing the interviewer. When you go over your resume focus on your accomplishments instead of reiterating job descriptions. Presenting yourself as an active problem solver will show an employer that you can contribute and succeed in the role. Stagg agrees that this technique can make a fantastic impact. “Give very specific examples of your qualifications. If you have qualifications in financial analysis, give examples of projects you worked on where your analysis was necessary. Describe your experiences that tie in to your skills or qualifications. Even better, tell me how those will help you meet the requirements of the role you might fill in our company.” | |||
| |||