C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 300,449 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,572 people online right now. Registration is fast and FREE... Join Now!




DataTable vs Array

 

DataTable vs Array, What's the difference?

jase81

2 Jul, 2009 - 01:04 PM
Post #1

D.I.C Head
**

Joined: 2 Mar, 2009
Posts: 57



Thanked: 1 times
My Contributions
I'm learning .NET (vb and c#) and I'm trying to work out which data structures are better for which task. I've been writing a lot of for each and do loops and I'm trying to figure out when it's better to use a datatable, array, arraylist etc.

I would like to make my algorithms more efficient by either hashing values, searching instead of looping through all values or removing keys from the search list once theyre found.

My question is: how can you speed up loops by changing your data structure? (I realise it might be a little ambiguous and there are probably hundreds of answers)

Any ideas would be greatly appreciated.

User is offlineProfile CardPM
+Quote Post


Renagado

RE: DataTable Vs Array

2 Jul, 2009 - 02:20 PM
Post #2

D.I.C Head
Group Icon

Joined: 14 Jun, 2009
Posts: 180



Thanked: 16 times
Dream Kudos: 100
My Contributions
I've been experimenting with that a bit.
Just run some small tests with what you want to do, and use this:
CODE
DateTime starttime = DateTime.Now;
//add code
DateTime endtime = DateTime.Now;
TimeSpan time = endtime - starttime;
Console.WriteLine(time);//or some other way to display it


Some things I've found is that a normal array is much quicker than List<T> when you constantly need to change values of a set of data you know the size of, but when you dont know the size of your dataset List<T> wins since you need to constantly need to copy the array to a new array with the added data etc. And the c# book I have said to chose List<t> over arraylist since it's faster, although I haven't tested it yet. So to be short, it depends on the situation, but feel free to experiment a bit.

To give you some results: here I added 100.000 int values to an array, and a List<int>, and then redid that 100.000 times.

CODE
static void Main(string[] args)
        {
            DateTime starttijd = DateTime.Now;
            int[] i_array = new int[100000];
            for (int i = 0; i < 100000; i++)
            {
                for (int j = 0; j < 100000; j++)
                {
                    i_array[j] = j;
                }
            }
            DateTime eindtijd = DateTime.Now;
            TimeSpan tijd = eindtijd - starttijd;
            Console.WriteLine(tijd);


             starttijd = DateTime.Now;
            List<int> i_list = new List<int>();
            for (int i = 0; i < 100000; i++)
            {
                i_list.Clear();
                for (int j = 0; j < 100000; j++)
                {
                    i_list.Add(j);
                }
            }
             eindtijd = DateTime.Now;
             tijd = eindtijd - starttijd;
            Console.WriteLine(tijd);

        }


The results on my laptop are: about 10 seconds for the array, about 50 seconds for the list. So in this type of situation where you know exactly in advance how much data you will have, array clearly wins. But in another project, where I didn't know that, the List won.

This post has been edited by Renagado: 2 Jul, 2009 - 02:34 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 01:49AM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month