How to define a list of many elements

0

I'm trying to define a list of a lot of elements, so many elements that it does not even compile me.

        long longitud = 100000;
        List<int> lista = new List<int>(longitud);

The problem is that it says that it can not be converted from long to int. I would appreciate if someone tells me how you can make lists with a high number of items or if there is some other type of data similar to the list you can use,

Edit: I put all the code so that whoever wants can try and see if there is any other structure than List that allows this.

namespace Tiempos
{
class Program
{
    private const long longitud = 100000;
    private const int vueltas = 1000;

    static void Main(string[] args)
    {

        long resA = 0;
        long resB = 0;
        for (int a = 0; a < vueltas; a++)
        {
            resA += lanzaNormal();
            resB += lanzaLista();
        }

        Console.WriteLine("Media metodo normal: " + (resA / vueltas).ToString());
        Console.WriteLine("Media metodo lista: " + (resB / vueltas).ToString());
        Console.Read();
    }


    private static long lanzaNormal()
    {

        int[] array = new int[longitud];
        for (int i = 0; i < longitud; i++)
        { array[i] = -1; }

        Stopwatch sw = new Stopwatch();
        sw.Start();

        Random r = new Random(DateTime.Now.Millisecond);

        int totales = 0;
        while (totales != longitud)
        {
            long a = Convert.ToInt32(r.NextDouble() * (longitud - 1));
            if (array[a] == -1)
            {
                totales++;
                array[a] = 0;
            }
        }

        sw.Stop();
        //Console.WriteLine("Total de milisegundos: " + sw.ElapsedMilliseconds);
        return sw.ElapsedMilliseconds;
    }

    private static long lanzaLista()
    {
        int[] array = new int[longitud];
        for (int i = 0; i < longitud; i++)
        { array[i] = -1; }

        Stopwatch sw = new Stopwatch();
        sw.Start();

        List<int> lista = new List<int>();
        for (int i = 0; i < longitud; i++)
        {
            lista.Add(i);
        }

        Random r = new Random(DateTime.Now.Millisecond);

        int totales = 0;
        while (totales != longitud)
        {
            long a = Convert.ToInt32(r.NextDouble() * (longitud - 1));
            array[a] = 0;
            lista.RemoveAt(a); // Esto no compila por ser long en vez de int                    
            totales++;
        }


        sw.Stop();
        //Console.WriteLine("Total de milisegundos: " + sw.ElapsedMilliseconds);
        return sw.ElapsedMilliseconds;
    }

}

}

Thank you.

    
asked by U. Busto 08.06.2018 в 12:07
source

1 answer

6

100000 are not as many elements , the problem is that the constructor of List<T> that expects a capacity, expects a int , not a long . If you change the type of the variable longitud will compile:

int longitud = 100000;
List<int> lista = new List<int>(longitud);

In any case, I do not know why you restrict the capacity in the constructor, when List is a class that is dynamic and grows automatically by adding elements.

Edited

As it says @ SJuan76, List uses internally a array , so defining a maximum capacity a priori increases the performance of the list since you do not have to resize the internal array.

This is correct, but I always advocate not worrying too much about "micro-optimizations". In this case, although it is true what was mentioned above, this optimization refers only to the process of adding (and perhaps removing) elements to the list. The difference in performance in this process, in a number of elements that are not too large, such as 100,000 in the case in question, is practically imperceptible.

    
answered by 08.06.2018 в 12:16