views:

114

answers:

4

Let me cut to the chase...
On one hand, many of the programming advices given (here and on other places) emphasize the notion that code should always be as readable and as clear as possible, at (almost?!) any pefromance cost. On the other hand there are SO many slow web sites (at least one of whom, I know from personal experience).
Obviously round trips and DB access, are issues a web developer should always keep in mind. But the trade-off between readability and what not to do because it slows things down, for me is very unclear.
Question are-
1.What else?
2.Is there a rule (preferably simple, but probably quite general) one should adhere to in order to make sure his code does not slow things down too much?
General best practices as well as specific advices would be much appreciated. Advices based on experience would be especially appreciated.

Thanks.

Edit: A little clarification: General peformance advices aren't hard to find. That's not what I'm looking for. I'm asking about two things- 1. While trying to make my code as readable as possible, when should I stop and say: "Now I'm hurting performance too much".
2. Little, less known things like- is selecting just one column faster than selecting all (Thanks Otávio)...

Thanks again!

+7  A: 

See the Stack Overflow discussion here:

http://stackoverflow.com/questions/2241717/what-is-the-most-important-effect-on-performance-in-a-database-backed-web-applic

The top voted answer was, "write it clean, and use a profiler to identify real problems and address them."

In my experience, the biggest mistake (using C#/asp.net/linq) is over-querying due to LINQ's ease-of-use. One huge query is usually much faster than 10000 small ones.

The other ASP.NET gotcha I see a lot is when the viewstate gets extremely fat and bloated. EnableViewState=false is your best friend, start every new project with it!

Scott Stafford
Interesting.. But I want to know as I actually write code and queries, what to pay attention to, which I think is quite different. But thanks anyhow.
Oren A
Exactly. And let me just emphasise the that point you should never assume anything. You should *always* base optimisations on concrete observations.
troelskn
One of the thing I hope to get here is other's observations, which will probably save time and effort for all of us..
Oren A
@Oren A: Since the answer to your q is specific to language, platform, usage, data, and common patterns, you must find it for your specific project yourself. Write some stuff and profile it, and you'll find the customized answer.
Scott Stafford
@Scott: I apologize, I answered only to the link, and not to your complete reply. "One huge query is usually much faster than 10000 small ones" Is really of the kind of things I'm looking for, and is very helpful (and still isn't specific to any language platform...). The view state advice is helpful too (-:
Oren A
+2  A: 

For web applications that have a database back end, it is extremely important that:

  • indexing is done properly
  • retrieval is done for what is needed (avoid select * when selecting specific fields will do - even more so if they are part of a covered index)

Also, whenever possible an appropriate caching strategy can help performance

Otávio Décio
A: 

Optimizing your code.

While making your code as readable as possible is very important. Optimizing it is equally as important. I've listed some items that will hopefully get you in the right direction.

For example in regards to Databases:

  • When you define the schema of your database, you should make sure that it is normalized and the indexes of fields are defined properly.
  • When running a query, specifically SELECT, only select the fields you need.
  • You should only make one connection to the database per page load.
  • Re-factor. This is probably the most important factor in producing clean, optimized code. Always go back and look at your code and see what can be done to improve it.

PHP Code:

  • Always test your work with a tool like PHPUnit.
  • echo is faster than print.
  • Wrap your string in single quotes (‘) instead of double quotes (“) is faster because PHP searches for variables inside “…” and not in ‘…’, use this when you’re not using variables you need evaluating in your string.
  • Use echo’s multiple parameters (or stacked) instead of string concatenation.
  • Unset or null your variables to free memory, especially large arrays.
  • Use strict code, avoid suppressing errors, notices and warnings thus resulting in cleaner code and less overheads. Consider having error_reporting(E_ALL) always on.
  • Incrementing an undefined local variable is 9-10 times slower than a pre-initialized one.
  • Methods in derived classes run faster than ones defined in the base class.
  • Error suppression with @ is very slow.

Website Optimization

  • A good place to start is here (http://developer.yahoo.com/performance/rules.html)

Performance is a huge topic and there are a lot of things that you can do to help improve the performance of your website. It's something that takes time and experience.

Best, Richard Castera

rcastera
A: 

Scott and Rcastera did a good job covering DB and querying optimization. To address your question from a HTML / CSS / JavaScript standpoint:

CSS: Readability is key. CSS is rendered so fast that you should never feel it is necessary to sacrifice readability for performance. As such, focus on adding in as many comments as necessary to document the code, why certain rules (like hacks) are there, and whatever else floats your comment boat. In CSS there are a few obvious rules to follow: 1) Use external stylsheets. 2) Limit external stylesheets to limit GET requests.

HTML: Like CSS, HTML is read so fast by the browser you should really only focus on writing clean code. Use whitespace, indentation, and comments to properly document your work. Only major things in HTML to remember are: 1) declare the <meta charset /> early within the head section. 2) Follow this guys advice to minimize browser reflows. *this rule actually applies to CSS as well.

JavaScript: Most optimizations for JavaScript are really well known by now so these'll seem obvious, like initializing variables outside of loops, pushing javascript to bottom of body so DOM loads before scripts start tying up all of the resources, avoiding costly statements like eval() or with(). Not to sound like a broken record, but keeping a well commented and easily readable script should still be a priority when developing JavaScript code. Especially since you can just minimize and compress away all the excess when you deploy it.

Moses
"focus on adding in as many comments as necessary to document the code" - Can adding comments to code harm performance in ANY scenario (other than adding giga-bytes of them...)?
Oren A
I reiterated the comments part so many times because in my (admittedly small) experience, people are often either too lazy to comment or don't know how to leave good comments. To answer your question: adding HTML comments to certain parts of the DOM can break descendant/ascendant selectors in IE6 and IE7. Comments can also break a REGEX if it doesn't take into account all possible locations of the comment. Harm performance? Probably not. Break performance? Yeah.
Moses