开发者

Web site performance - what is it about? Readability and ignorance Vs. Performance

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 ord开发者_StackOverflow社区er 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!


See the Stack Overflow discussion here:

What is the most important effect on performance in a database-backed web application?

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!


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜