Friday, September 23, 2011

New Hero of .NET 4.0 :Task Parallel Library


The Task Parallel Library (TPL) is a set of public types and APIs in the System.Threading and System.Threading.Tasks namespaces in the .NET Framework version 4. The purpose of the TPL is to make developers more productive by simplifying the process of adding parallelism and concurrency to applications.

Starting with the .NET Framework 4, the TPL is the preferred way to write multithreaded and parallel code. However, not all code is suitable for parallelization; for example, if a loop performs only a small amount of work on each iteration, or it doesn't run for many iterations, then the overhead of parallelization can cause the code to run more slowly. Furthermore, parallelization like any multithreaded code adds complexity to your program execution. Although the TPL simplifies multithreaded scenarios, we recommend that you have a basic understanding of threading concepts, for example, locks, deadlocks, and race conditions, so that you can use the TPL effectively. For more information about basic parallel computing concepts, see the "http://go.microsoft.com/fwlink/?LinkID=160570"

Data Parallelism (Task Parallel Library)

Data parallelism refers to scenarios in which the same operation is performed concurrently (that is, in parallel) on elements in a source collection or array. Data parallelism is supported by several overloads of the For and ForEach methods . In data parallel operations, the source collection is partitioned so that multiple threads can operate on different segments concurrently. TPL  provides method-based parallel implementations of for and foreach loops. You write the loop logic for a Parallel.Foror Parallel.ForEach loop much as you would write a sequential loop. You do not have to create threads or queue work items. In basic loops, you do not have to take locks. The TPL handles all the low-level work for you. The following code example shows a simple foreach loop and its parallel equivalent.


// Sequential version            
foreach (var item in sourceCollection)
{
    Process(item);
}

// Parallel equivalent
Parallel.ForEach(sourceCollection, item => Process(item));
When a parallel loop runs, the TPL partitions the data source so that the loop can operate on multiple parts concurrently. Behind the scenes, the Task Scheduler partitions the task based on system resources and workload. When possible, the scheduler redistributes work among multiple threads and processors if the workload becomes unbalanced.


Some useful links about TPL are:


How to: Write a Simple Parallel.For Loop


How to: Write a Simple Parallel.ForEach Loop


How to: Stop or Break from a Parallel.For Loop


How to: Speed Up Small Loop Bodies


The most helpful one is


TPL With Other Asynchronous Patterns



Thursday, September 15, 2011

Make your page lighther and faster

It is a common scenario where even the regular visitors tend to get frustrated with a webpage which takes longer to load. By decreasing the website load time, businesses can ensure that the visitors are served well and increase the chances of getting potential customers and repeated visitors as well.


This will be a series of topic and inputs from you people will be greatly appreciated.


In course of web site development we use a lot of CSS and images and after some time either we forget to remove unused CSS or forget to format images on site.these things led to increase Page load time.

Things to take care about CSS:

Google Chrome Audit - Useful tool for finding unused CSS styles


It's free and is part of Google Chrome - the browser.


Open Properties > Tools > Developer Tools > Click the Audits tab, 
and then run the testIt shows lots of useful info for you to trim your page down to the bare minimum, to get rid of legacy CSS styles you are no longer using! Of course, the results must be taken with a grain of salt, since for global CSSs it is normal for some styles not to be used in a specific page, but for inline CSSs it's the real deal.

Audit tools not only tell about CSS but also about JS,Images,gzip compression etc.
which help us to decrease page load time.

I had run this on www.msn.com, See the audit below




 

Another useful tool to do this - Dust-Me Selectors- works as a Firefox extension and actually keeps track of CSS usage across different pages. It even comes with a handy Automation tool to scrape out your entire website based on a Sitemap file!




Things to take care about Images :                                                                    

Making use of optimized images
You can find the required tools in your graphic editing program. Those tools can help you with optimizing your website images to best suit the online use. The tools hold the capability to compress the images to almost eighty percent without disturbing its quality but decreasing the file size.
Use of static images instead of animations
In comparison to animated videos and graphics, static images take remarkably less time to load. The use of static images within your webpages can make the page lighter hence making it faster to be loaded in the browser.
Large images to be replaced with thumbnails
Thumbnails are nothing but a shrunken versions of your images. If you take an overview, at most places large images are not at all essential. If you still insist on keeping the large images, you can provide an option to users where they can click on the thumbnail to view a larger image. This technique would further add to your efforts of increasing the speed of your website.
Making adjustments to the tables to load the webpages gradually
Divide the web-page content in smaller chunks of data to help the page load faster. This should keep the visitor interested until the rest of the page gets loaded.
Use of common files for images
This actually helps in two ways. One that, it eliminates the possibility of image duplication. Secondly, the users web browser is able to find the images with minimum efforts which in-turn results in faster page loads.