Tony Davis

Simple-Talk Editor
News, views and good brews

The best and worst ways to optimize your code

Published Wednesday, April 16, 2008 1:55 PM

I was chatting to Phil Factor the other day about the slow start-up of some CLR applications. He started telling me, with apparent irrelevance, how he once, a long time ago, developed a database system in Z80 assembler code and a large eight-inch floppy disk. The point he was actually making was this: the more you know about what an application is doing, the more likely you are to be able to fix slow-running code. Through watching the bare disk drive on his desk as it whirred and clicked, he could actually watch the reads and writes, the head position, and the tracking across the drive. It gave him a lesson for life in the importance of performance metrics.

Much has changed, but the need to monitor the performance of an application hasn't. With the right information, you will see at a glance any unnecessary initialization, premature loading of data or blocking process. You still need a tool that will allow you to monitor down to the details of thread creation, thread switching and context switching,

Performance Counters are the first resort when investigating or understanding a performance problem.  There are performance counters for almost every aspect of the CLR and .NET Framework. They are always available and do not change the performance characteristics of your application. Once you have the big picture, then a profiler will soon tell you where the causes of performance problems lie.

Armed with this information, it is a matter of great job satisfaction to be able to make a big gain through a small adjustment to the code. It is possible to tweak any code to make it run faster, usually by changing the algorithm, but it will only have a perceptible effect if it is done in the right place. The art of improving performance is in knowing where to make those alterations.

 

There are some dreadful ways of making code trying to make code run fast. The worst mistake is to write your code in a machine-Friendly-human –unfriendly way, an art beloved of a few Perl programmers. Another bad idea is to compromise a good Object-oriented model in pursuit of small savings in Garbage-collection, a process equivalent to the sin of de-normalisation. It is programming without all your faculties, blind to what is going on and, even worse, without the faculty of thought.

 

We encourage your nominations for the least (and most!) effective ways to optimize your code. The best contribution, added as a comment to this blog, will win a $50 Amazon voucher. The winner last time, for their contribution to the "That's not a database, it's a spreadsheet" editorial, is digory!

 

Cheers,

Tony.

Comments

 

EricBrown said:

Top 5 Least effective ways to increase performance:
1. Put all your business logic in stored procedures.
2.Blame the DBA
3.Blame the CEO.
4.Blame the customers
5. Use a fancy new technology, while in beta for production apps.
April 16, 2008 1:49 PM
 

Cracky said:

Most effective way to increase performance wiil be, I think, comment, document and comment.

Comments don't introude into code performance, nor do they mess up with code functionallity or arquitectural design as they are deleted from final output.

Nontheless, without proper comments you can't keep track of your improvements, every piece of messy code written for improvement such as assembly sections, unsafe regions or any other "hack" wich makes application faster are allowed as long as they're properly documented in such way that their propose and meaning are made understood.

Truthfully I've always thought comments (and documentation in general) are almost the most important part of the code. I prefer a bad optimized code with good comments that an impresive architecture with no useful comments.
April 16, 2008 4:25 PM
 

AreEyeEkks said:

I've actually recently been having a discussion along this line with some of the people I work with.

I honestly feel that the worst way to optimise code is to optimise prematurely. i.e. spending time optimising code before there's an actual performance problem.  On a recent project we practised a philosophy of leaving all optimisation until the end of the project. Come the end, there were less than 5 bottlenecks which took a total of a week to clear (thanks to Ants :P )

The best way to optimise is to use the right tools to find the actual performance problems. Optimising your for loops to work in reverse because caching blah blah blah doesn't help anyone if the for loops aren't actually a problem.  Using performance profiling tools to find the actual bottlenecks is invaluable for performance optimisation.

I think either way it comes down to look before you leap?
April 17, 2008 12:08 AM
 

jrbarnett said:

I recently rewrote from scratch an application (Access database using linked tables to connect to SQL Server) and the way it was designed meant it was joining SQL Server linked tables to local access tables in the same query, doing table scans pulling multi hundred thousand rows of data between server and client.
I recreated the user selections stored locally in access as a SQL Server table, then rewrote the queries as stored procedures. What once took 3 minutes now takes just under 3 seconds.

Tips:
Use SQL Server Profiler and the query execution plan view in SQL Server Query Analyser or Management Studio to work out exactly what is happening to the underlying data.  Once you know that, you can figure out the best places to optimise.

SQL Server stored procedure execution plans are cached once executed. Use this feature - and use stored procedures for all data access from clients.

Access linked tables to SQL Server are evil - they hold open large numbers of locks, which hurts performance and can also affect concurrency access.  Replace using stored procedures called via ADO, or use an Access Project, which uses native OLEDB protocol for data access.
April 17, 2008 2:56 AM
 

GilaMonster said:

I would say that the worst way to optimise is based on 'common knowledge' and to not do any testing to see if said 'common knowledge' is true or not

I've seen too many cases where a developer 'knew' that X was slow and went to a lot of effort not to use the feature he 'knew' to be slow. In the end it often turns out that the workaround is far slower than the original feature and it's often the case that 'X' wasn't slow at all.

The best way to optimise - Use whatever profiling tools are available and optimise only where there's a problem and wher the gain is worth while. Spending a month to reduce a once a year operation from 15 minutes to 10 is not a valuable use of time.
April 17, 2008 3:02 AM
 

AndyC Manpowersoftware said:

It's often not the performance but the perception of performance that is important. Users won't accept slow performance for interactive operations but are happy with wait for a little longer for "Calculation" or "report" operations.

Typically what we do for optimisation is to check for operations that are happening very often, then try to reduce the number of times this happens or try to increase the performance of these. This gives you the biggest gains.
April 17, 2008 5:27 AM
 

Phil Factor said:

I came across a programmer once who genuinely believed that the length of the symbols he used had a bearing on the performance of his code. He named his local variables A, B and C. He even used  A123 for his subroutine names. He insisted also on 'Giving the compiler hints' by nesting everything. I put it in a test harness and tried to prove to him that he was deluded, but it was like arguing with a fence-post.

I knew a SQL Server developer who refused to use functions at all because he claimed it slowed his code down. It is strange how people generalise from a particular case.

Gail (GilaMonster)  said it perfectly. Performance testing is so simple to do, but is so seldom done. That's how performance myths manage to take such a strong hold.

I heard last week of a high-profile project in London that had to be scrapped because each business transaction was taking 27 seconds. How did it ever get that far? (No performance testing)
April 17, 2008 10:40 AM
 

GSquared said:

The worst ways I know of to speed up code are the ones that threaten the integrity of the data.  Just had a discussion on SQLServerCentral about that.

For example, speeding up code by using "with (nolock)" on every select.  Or making sure all the procs use "local fast_forward" cursors (which has multiple levels of problem if that's your solution to speed issues).

Even wasting time on code that really doesn't need to be sped up is not as bad as speed at the cost of accuracy or data corruption.

The best way to speed stuff up is to find the actual problems by testing, logging, etc., and then standardize the stuff that's actually causing the problem.

Almost any speed problem can be fixed by getting rid of the "clever solution that nobody's ever thought of before" and replacing it with the standard solution.  Just take one look at TheDailyWTF.com and you'll see plenty of "clever solutions" that were anything but.
April 24, 2008 9:55 AM
 

MVV said:

Keep your building blocks short and to the point (KISS)

If something works , dont meddle with it. If you think you can do better , write it from scratch (if it's kiss , shouldn't be so hard). Then you can pit each solution agains the other.

Hear what your elders have to say. Lore is a treasure, don't go refactoring the wheel. On the other hand , keep in mind that 90% of what they say is irrelevant.

Optimize everyday operations first, not what it takes longer , but what is most used.

Keep your target on the sigths. If , as i do , you have a tendency to drift , use post-its in the screen to keep you straigth.

Tools are there to being used to our confort , not for our pain. If a profiler exists , and you need profiling information , use it , damm it!!. But remember , having a hammer does not mean that every problem is a nail. Use what you have for what is designed. Faulty code will not be revealed by profiling away your database.

Peer review. Four eyes see more than two. If your peers see nothing , ask the janitor. Layman may give you an unexpected POV ( not a boost to your morale when they point out the obvious, but they can keep you from being busted)
April 29, 2008 11:18 AM
 

jimoc said:

My current employment encompasses all the evils of the IT industry in one neat and scary package. I work for a company that makes software for recruitment agencies using an MS Access frontend with linked tables on an SQL server.
In the course of fixing the myriad bugs in the program I've run across one particular gem which I think illustrates the worst possible 'optimization' I've seen to date.
In a loop used to send emails to clients, the former programmer has decided that he will call a function which creates an email object, adds the email body etc, then sends it.
Notice how I never said he then closes and disposes of the object while in the function.
He decided that once he leaves the loop he will dispose of the email object once 'because its more efficient'
Needless to say the entire system grinds to a halt and keels over whenever a client tries to sent more then 25 emails.
So, to cap off all the wonderfully bad programming in the first section, he decided that 'PC cannot handle more then 20 emails so stop them sending more then that'  (actual comment from the program)

Needless to say my days are filled with much headscratching and bewilderment.
May 2, 2008 9:00 AM
 

gOODiDEA.NET said:

Web Polling for loaded content instead of simple setTimeout Is HTML a Humane Markup Language? Split the
May 15, 2008 4:17 AM
You need to sign in to comment on this blog

















<April 2008>
SuMoTuWeThFrSa
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910
Identity Columns
 When Nigel Rivett takes us on a tour of the apparently innocuous subject of Identity Columns in TSQL,... Read more...

The Why and How of .NET Profiling
 Amirthalingam Prasanna gives a simple and practical guide about why you need to profile your .NET... Read more...

Execution Plan Basics
 Every day, out in the various discussion boards devoted to Microsoft SQL Server, the same types of... Read more...

NET Performance Cribsheet
 Robyn and Phil tackle the topic of how to make .NET applications perform well. As usual, they try to... Read more...

SQL Code Layout and Beautification
 William Brewer takes a look at the whole topic of SQL Code layout and beautification, an important... Read more...