0

I have a list of points which I am using to add points.

I have two approaches but I just want to know which one is better.

Following is the code for first approach:

 List<Point> borderPoints = new List<Point>();

                Point p1 = new Point(0, 0);
                Point p2 = new Point(Width, 0);

                borderPoints.AddRange(new[] { p1, p2 });

in the second approach I add the points like below:

 borderPoints.Add(p1);
 borderPoints.Add(p2);

I have to add the points four times, so which approach I should follow and why, or it doesn't matter, it's just the case of style ?

Embedd_0913
  • 16,125
  • 37
  • 97
  • 135
  • If you need too add points **four times** then there is really nothing too discuss. Remember - premature optimization is the root of all evil. If you need to add thousands of Points, then read Tigran's answer. – Michal B. May 09 '12 at 06:53
  • I can't imagine this having a measurable impact on performance. It's primarily a style decision - IMHO. Having said that, any time you know the size of a list you can provide that value so it can be allocated more efficiently. Add verse AddRange is more complex - Add can be more performant (http://stackoverflow.com/questions/2123161/listt-addrange-implementation-suboptimal) – Rob P. May 09 '12 at 07:31

6 Answers6

2

Definitely choose the first one, where you can. AddRange acts much faster then every single Add. (if you think about list rsize due the growing of the list's collection, it becomes clear)

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • @Reniuz: look here: [Add vs Addrange](http://geekswithblogs.net/sdorman/archive/2007/09/21/Add-vs.-AddRange.aspx), for example. – Tigran May 09 '12 at 06:59
  • @Reniuz: on every element Add List has to ensure if it need to grow, so for 1000 Adds it has to do 1000 times, for AddRange of 1000 elements it will do it only ones. Clear performance benefit. – Tigran May 09 '12 at 07:00
  • That's true. I'll keep in my mind this :) Thanks for explanation. – Renatas M. May 09 '12 at 07:05
2

Use a third approach:

List<Point> borderPoints = new List<Point>(new Point[] {
    new Point(0, 0),
    new Point(Width, 0),
});
Eric
  • 6,364
  • 1
  • 32
  • 49
1

You are right. In your situation it is more a case of your programming style.

Add() is simple and convenient in cycles while AddRange() looks more elegant when used at once.

Thinking about performance:

AddRange() adds an array of previously created tree nodes to the collection while Add() adds a new tree node to the collection (MSDN documentation).

So if you are only adding just 4 of nodes or adding nodes infrequently, use the Add().

  • But when you need to add items to list and some extra operations are performed behind the scenes(for example adding controls to container which fire resize and invalidate) calling Add method multiply times results in multiply calls of implicit operations. Calling AddRange invokes extra operations one time (in most cases - assuming that AddRange is well-designed). – adams May 09 '12 at 07:46
0

1) If you know by advance how many point you gonna add to your list, use the list constructor with a "capacity" parameter.

2) Unless you use it many times, instantiating a new array only to pass points to AddRange is pure waste.

Nicolas Repiquet
  • 9,097
  • 2
  • 31
  • 53
0

The answer is simple, if you have a range use AddRange and if you have a single item use Add.

But in your case I wouldn't use a list at all. For me it seems like you are missing a concept called BorderPoints. Something like this:

public class BorderPoints {
    public BorderPoints(Point p1, Point p2) {
        _p1 = p1;
        _p2 = p2;
    }
}

Then you can just use that instead of the list.
Tomas Jansson
  • 22,767
  • 13
  • 83
  • 137
0

I would use the second approach.

The first approach instantiates an array unnecessarily, so will be very slightly slower.

Only use AddRange if the items to add are already in a collection (or enumeration).

In general, if you want to squeeze every last nanosecond of performance, you can do so by ensuring the capacity of your list is large enough for all the items you are going to add:

var list = new List<T>(capacity);
... add items ...

or

list.Capacity = capacity;
... add items ...

But copying individual items into an array before adding them to the list gains you nothing.

Joe
  • 122,218
  • 32
  • 205
  • 338