Wednesday, June 29, 2011

Choosing the Right Collection Class

In .net we have number of collection classes that are very useful in managing collections of object. But the question is which one should we use to get the best performance. The answer is that what is the scenario and the output that one requires from the collection, depending on which the selection of the collection type should be made. The following table explains the basic parameters for collections that should be kept in mind while selection the type of collection.


Collection
Ordered?
Contiguous Storage?
Direct Access?
Lookup Efficiency
Notes
Dictionary
No
Yes
Via Key
Key:
O(1)
Best for high performance lookups.
Sorted
Dictionary
Yes No Via Key Key:
O(log n)
Compromise of Dictionary speed and ordering, uses binary search tree.
SortedList
Yes
Yes
Via Key
Key:
O(log n)
Very similar to SortedDictionary, except tree is implemented in an array, so has faster lookup on preloaded data, but slower loads.
List
No
Yes
Via Index
Index: O(1)
Value: O(n)
Best for smaller lists where direct access required and no ordering.
LinkedList
No
No
No
Value:
O(n)
Best for lists where inserting/deleting in middle is common and no direct access required.
HashSet
No
Yes
Via Key
Key:
O(1)
Unique unordered collection, like a Dictionary except key and value are same object.
SortedSet
Yes
No
Via Key
Key:
O(log n)
Unique ordered collection, like SortedDictionary except key and value are same object.
Stack
No
Yes
Only Top
Top: O(1)
Essentially same as List except only process as LIFO
Queue
No
Yes
Only Front
Front: O(1)
Essentially same as List except only process as FIFO

For more read this good article.

Thursday, June 16, 2011

The 4 Types of Emails Programmers Receive


The 4 Types of Emails Programmers Receive

 

The Vague Email

Feature XYZ isn’t working. Please fix.
Provides next to no information other than at some point, a feature — possibly XYZ — didn’t function the way a specific user expected it to. Issue could be anything ranging from a catastrophic system failure to a simple misunderstanding on the part of the user. Usually requires a series of follow-up emails, phone calls, and conferences in order to clarify the problem at hand.

 

The End of the World Email

URGENT!!! System is down! NONE of our users can do XYZ! Please advise!!!11!
Usually flagged as important in Outlook and CC’ed to everyone in the user’s contact list. Grossly overstates the magnitude of the problem either to guarantee an expedited fix or simply because the user would rather yell at someone rather than bother checking. Rarely turns out to be as terrible as originally advertised. Unfortunately, that still won’t save you from having to put out all the fires the email started.

 

The Red Herring Email

Can you check if XYZ is working?
Actual problem winds up being completely unrelated to XYZ. Unfortunately, you won’t find that out until after you’ve spent several hours poring over code trying to envision the perfect storm of events that would have triggered a failure in XYZ.

 

The Ideal Email

When I do XYZ, ABC happens. I expected DEF to happen instead. Here are some screenshots showing what happened.
Clear, concise, and doesn’t make any assumptions about the issue at hand. Gives enough information for a programmer to at least approach the problem and is oftentimes sufficient without any additional follow-up. These are rare and far between, yet the amount of effort to type them up isn’t much more than any of the three other types of emails. Wouldn’t life be a lot simpler if all emails came in this format?


For more refer to this good article. I loved it..

Wednesday, June 15, 2011

Practical steps towards a greener, energy-efficient cloud

Official Google Blog: Practical steps towards a greener, energy-efficien...: "(Cross-posted from the European Public Policy Blog ) Update June 14, 9:14am: Videos of all the presentations at the Data Center Summit are now available on our website.

Data centers are very important to us—they’re critical to the cloud services we deliver. Over the last 12 years, we’ve put a lot of effort into minimizing the amount of energy, water and other resources we use—because it makes financial sense, and because it’s good for the environment too. That work means that today, we use half the energy of a typical industry data center. ..."

Here Google talks about the data-centers and how to work and operate them efficiently, thus saving electricity and making data-centers environment friendly. A must read.

Friday, June 10, 2011

Apple iCloud - Cloud Computing in Action

Now Apple too is on the Cloud ! Apple has launched its own Cloud Computing offering - iCloud. It is a set of cloud services that work seamlessly with applications on your iPad, iPhone, iPod touch, Mac, or PC to automatically and wirelessly store your content and push it to all your devices. And the great news is that it is FREE. iCloud services include new versions of Contact, Calendar, and Mail; iCloud Backup and Storage; Photo Stream; and iTunes in the Cloud. And for just $24.99 a year, iTunes Match will give you all of the benefits of iTunes in the Cloud for music you haven’t purchased from iTunes.






For more on it read at official iCloud site.


Wednesday, June 8, 2011

Serialization in .Net

Many times we are faced with the situation where we need to save records in different tables and these are inter-linked and having a relationship. Now the problem is how to send these records from application to database and then save them maintaining their relationships intact. Further all this should be fail proof so that if any error is encountered in the whole operation then all is rolled back. In this post I am going to discuss about how to handle this situation by an example. The key points that I am going to touch is Serialization and XML.

For example we have a application where we are registering a student. For this we are saving the basic information of the student, the subjects that he has chosen. Thus the outline of the student registration data would be as follows:

Student
   --BasicInfo
         --RollNo
         --Name
         --Age
         --Address
         --Class
         --Gender


   --Subject
         --SubjectId
         --SubjectName

Now the number of records in Subject for a student can be more then one i.e. for a single BasicInfo record of a student there can be multiple Subject records i.e. a one to many relationship. The entity for Student will be as follows:


public class Student
{
    public int RollNo { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public string Address { get; set; }
    public int Class { get; set; }
    public bool Gender { get; set; }

    public List<Subjects> ListSubject { get; set; }

}

public class Subjects
{
    public Subjects(int subjectId, string subjectName)
    {
        SubjectId = subjectId;
        SubjectName = subjectName;
    }

    public int SubjectId { get; set; }
    public string SubjectName { get; set; }   
}


Here to maintain the relationships of one to many I have used List collection for subjects. This collection would contain all the subjects that the student chooses. We fill the entity as follows:





Student obj = new Student();
obj.Name = "Jai";
obj.RollNo = 100;
obj.Age = 27;
obj.Class = 10;
obj.Gender = true;
obj.Address = "New Delhi, India";

obj.ListSubject = new List<Subjects>();
       
obj.ListSubject.Add(new Subjects(1, "English"));
obj.ListSubject.Add(new Subjects(2, "Maths"));
obj.ListSubject.Add(new Subjects(3, "Physics"));
obj.ListSubject.Add(new Subjects(4, "Computers"));

Now to convert this into XML we would serialize the Student object. The code for it is as follows:


StringBuilder builder = new StringBuilder();
System.Xml.Serialization.XmlSerializer x =
new System.Xml.Serialization.XmlSerializer(obj.GetType());

using (StringWriter writer = new StringWriter(builder))
{
x.Serialize(writer, obj);
}

string myXml = builder.ToString();

The XML generated for it is as follows:


Thus we have generated the XML of the Student entity class using serialization and we have also maintained the one to many relationship between the StudentInfo and Subjects. In the next post I will discuss how to use this consume this XML in sql server and save the data in the tables.

LinkWithin

Related Posts with Thumbnails