How to write a code (in any programming language, preferably in java), which calculates the average loading time of any website (including all embedded elements such as images, Javascript, CSS, etc.) ?
I've always enjoyed using this tool: http://www.websiteoptimization.com/services/analyze/
Depends what you mean by 'average loading time'.
If you sit at your PC with your site running locally and just hit F5 over and over again you'll get considerably different results to someone looking at the same code which is deployed in a data centre on the other side of the world.
Fiddler is also another option. See the Fiddler home page
A nice screenshot of the transfer timeline is here.
Google just released Page Speed (a plug-in for firebug, just like YSlow) and it's got some pretty cool features.
This has a nice JavaScript way to check load time of a web page: http://javascript.internet.com/text-effects/loading-time-indicator.html
I had used souptag framework to parse html page and then found individual src attribute of all the tags, Then individually found size of each page mentioned in src attribute and then according to my internet speed found out average loading time.
Perceived loading time is important too. External scripts will delay rendering of everything below them until they've loaded and executed, in all browsers, so it's best to push them to the very bottom of the page when possible, as opposed to the very top which is the habit of many developers. Another source of delay in some browsers is img tags where the width and height are unspecified. The browser will give the image some time to load so that it can get the layout right the first try. Fixing both those issues will make your site appear a lot more responsive, even if total loading time is unchanged.