Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I require a fast speed in processing my page. Which one of the above is preferred? Support with a valid reason.

C#
Edit: For eg:

string str = "a,b,c"; //Count of the number of elements in str is not fixed
string[] arr = str.Split(',');

ArrayList al = new ArrayList();
al.Add(str.Split(','));


Which of them gives faster results?
Posted
Updated 5-Jun-21 3:41am
v3
Comments
Sunny_Kumar_ 30-May-12 7:58am    
depends on what kind of processing do you need to do...please expose some details the subject so that we can have a better picture to help you :)
Member 8491154 31-May-12 1:16am    
i have posted an example

The capacity of an Array is fixed.
Where as, ArrayList can increase and decrease size dynamically.

An Array is a collection of similar items.
Where as, ArrayList can hold item of different types.

Array is in the System namespace.
Where as, ArrayList is in the System.Collections namespace.

An Array can have multiple dimensions.
Where as, ArrayList always has exactly one dimension.

We can set the lower bound of an Array.
Where as, the lower bound of an ArrayList is always zero.

Array is faster and that is because ArrayList uses a fixed amount of array.
However when you add an element to the ArrayList and it overflows. It creates a new Array and copies every element from the old one to the new one.
You will only feel this if you add to often.

Since the add from ArrayList is O(n) and the add to the Array is O(1).

However because ArrayList uses an Array is faster to search O(1) in it than normal lists O(n).

List over arrays.
If you do not exceed the capacity it is going to be as fast as an array.
Better handling and much easier way of doing things.

Please refer:
ArrayList and List Collection Types[^]
 
Share this answer
 
Comments
Member 8491154 30-May-12 9:21am    
I have heard that ArrayList has become obsolete. Is it true?
Looks like a small world out there.. I guess you have asked the same question in the following sites also...

http://forums.asp.net/p/1808968/5004003.aspx/1?Which+is+better+array+ArrayList+or+List+T+in+terms+of+performance+and+speed+


http://stackoverflow.com/questions/10815565/which-is-better-array-arraylist-or-listt-in-terms-of-performance-and-speed


Array is undoubtadly faster than the other two. List<t> is much more sophisticated than array / arraylist.. Arraylist is in between blending the speed of array and convenience of List<t>
 
Share this answer
 
v2
The highest performing collection is the hashtable. Arrays, Arraylists and lists are collections of objects which means finding them again is slower than using a key in an optimised collection such as the hashtable.
 
Share this answer
 
C#
// The main thing I can add to this is that a LINQ query is almost always faster than a loop such as a foreach, etc. Here are a couple of prime examples in optimized code form.

// Try to use queries rather than loops.

    /// <summary> This one accepts the Collection of Dictionaries for
    /// ViewAllAssociations(); Has it's own special Size & Location Settings.
    /// It directly calls the Mother Method of them all below this one.
    /// NOTE: "var x = ..." below does a ton of work,
    /// & faster because instead of a loop it's a LINQ query!
    /// 
    /// <param name="listOfLists">
    private static void DisplayResults
      (IEnumerable<Dictionary<string, string>> listOfLists)
    {
      #region was
      //var i = 0;
      //var text = "";
      //foreach (var dic in listOfLists)
      //{
      ////  if (i == reportsListBox.Items.Count) break;
      //  text += GetDisplayString(dic, reportsListBox.Items[i++].ToString());
      //}
      #endregion was
      var i = 0;
      var
        x = listOfLists.Aggregate("", (current, dic) => current +
        GetDisplayString(dic, OPS.Default.reportsListBoxItems[i++]));

      const string t = "";
      var s = AUS.Default.OMA_D4_S;
      var l = AUS.Default.OMA_D4_L;
        DisplayResults(x, t, ref s, ref l);
      AUS.Default.OMA_D4_S = s;
      AUS.Default.OMA_D4_L = l;
      AUS.Default.Save();
    }

    /// <summary> This overload is where all end up.
    /// It allows for total flexibility of usages for all args.
    /// While providing critical defaults to reduce duplicity.
    /// if title is empty it defaults to:
    /// "Folder Associations Information:"
    /// 
    /// <param name="x">
    /// <param name="t">
    /// <param name="s">
    /// <param name="l">
    private static void DisplayResults
      (string x, string t, ref Size s, ref Point l)
    {
      if (string.IsNullOrEmpty(t)) t =
        "Folder Associations Information:";
      using (var f = new ShowMeForm(x, t))
      {
        f.Size     = s;
        f.Location = l;
          f.ShowDialog();
        l = f.Location;
        s = f.Size;
      }
    }

    /// <summary> This was split out so it can be used by both
    /// the View2Associations and the ViewAllAssociations: v4.7.3.75
    /// Added the string dir arg to the sig to interface it with both.
    /// 
    /// <param name="dic">
    /// <param name="dir">
    /// <returns>
    private static string GetDisplayString
      (Dictionary<string, string> dic, string dir)
    { // Sweet! Great integration.
      #region Was
      //foreach (var pair in list)
      //{
      //  // Concatenate the text string and show it.
      //  text += "The Data Folder:\n" + pair.Key +
      //    "\n\nWhich is set to the Data file: \n" +
      //    pair.Value + "\n\n";
      //}
      #endregion Was
      string
        title = string.Format("\nResults for: {0}\n\n", dir),
        text  = title +
          "The following currently use this Reports Folder:\n\n";
      return
        dic.Aggregate(text, (current, pair) => current +
          ("The Data Folder:\n" + pair.Key +
            "\n\nWhich is set to the Data file: \n" +
              pair.Value + "\n\n"));
    }
// Whereever it says .Aggregate here are good examples and it can be many Types.

// Take the first one:
// var x = listOfLists.Aggregate blah blah...
// Think for a minute what that is doing, it's iterating through a Collection of Dictionary Collections and getting the appropriate string for each and very very rapidly and efficiently!
// Follow the code path through that down to this other one above and you will see what I mean. From the Collection of Dictionaries down to this does a LOT. 
 
Share this answer
 
v10
Comments
[no name] 19-May-17 11:09am    
If you are going to continue to unnecessarily resurrect ancient already answered "questions" despite being told not to do this, you could at the very least provide accurate information.
If you are looping through the collections the array list appears to be fastest. Using VB below gives the following results in milliseconds to loop 1000000 times and write to the collection and do a read of the collection item.

It's not even close. It seems illogical but it is what it is:
Array List
Start Time = 7.373s
Finish Time = 7.500s
Array List Time = 127ms
Array
Start Time = 7.500s
Finish Time = 8.110s
Array Time = 610ms
List
Start Time = 8.110s
Finish Time = 8.741s
List Time = 631ms

Dim a(0) As String
Dim l As New List(Of String)
Dim al As New ArrayList
Dim atimer As New DateTime
Dim ltimer As New DateTime
Dim altimer As New DateTime
Dim atime As Integer
Dim ltime As Integer
Dim altime As Integer
Dim iterations As Integer = 1000000
ReDim a(iterations)
Dim finish As DateTime
Dim read As Integer
For x = 0 To iterations
    l.Add(0)
    al.Add(0)
    a(x) = 0

Next
altimer = Now
For x = 0 To iterations
    al(x) = x
    read = al(x)
Next
finish = Now
altime = (finish.Second - altimer.Second) * 1000 + finish.Millisecond - altimer.Millisecond
Console.WriteLine("Start Time = " & altimer.Second & "." & altimer.Millisecond)
Console.WriteLine("Finish Time = " & finish.Second & "." & finish.Millisecond)
Console.WriteLine("Array List Time = " & altime)
atimer = Now
For x = 0 To iterations
    a(x) = x
    read = a(x)
Next
finish = Now
atime = (finish.Second - atimer.Second) * 1000 + finish.Millisecond - atimer.Millisecond
Console.WriteLine("Start Time = " & atimer.Second & "." & atimer.Millisecond)
Console.WriteLine("Finish Time = " & finish.Second & "." & finish.Millisecond)
Console.WriteLine("Array Time = " & atime.ToString)

ltimer = Now
For x = 0 To iterations
    l(x) = x
    read = l(x)
Next
finish = Now
ltime = (finish.Second - ltimer.Second) * 1000 + finish.Millisecond - ltimer.Millisecond
Console.WriteLine("Start Time = " & ltimer.Second & "." & ltimer.Millisecond)
Console.WriteLine("Finish Time = " & finish.Second & "." & Format(finish.Millisecond, "000"))
Console.WriteLine("List Time = " & ltime)
 
Share this answer
 
Comments
CHill60 7-Jun-21 5:41am    
Whilst I commend your desire to help, you have added nothing new to this 9 year old thread, and by using VB to demonstrate the timingsm, you have gone off-topic (this question is tagged C#)
Answering old questions is a technique used by rep-point farmers so some more trigger-happy members will view this as abuse of the site and start the process to get the account banned.
Stick to answering newer posts, where the OP still needs help, and you should be fine
Richard Deeming 7-Jun-21 12:34pm    
In addition to the other comment, using DateTime to measure code is extremely bad.

At the very least, you should be using the Stopwatch class[^].

But you'll still have errors caused by JIT and warm-up time, which you need to take into account. That's where a proper benchmarking tool like BenchmarkDotNet[^] (free + open-source) will shine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900