简体   繁体   中英

Best way to retrieve a List of int from a list of object[] in c#

I am trying to retrieve a list of IDs from a list of object[] . Here is my code:

private static List<int> getReceivedIds(object[] objectList)
{
    var received = new List<int>();
    foreach (object[] b in objectList)
    {
        if (b != null)
        {
            received.Add(int.Parse((b[0].ToString())));
        }
    }
   return received;
}

I am looking for a performance optimised solution for this code.

I changed the code to this:

    private static List<int> getReceivedIds2(object[] objectList)
    {
        var received = new List<int>();
        foreach (object[] b in objectList)
        {
            if (b != null)
            {
                received.Add((int)b[0]);
            }
        }
        return received;
    }

Also I compared the LINQ query and foreach statement performance and here is the result: Performance test results

The test shows that the foreach statement is 6 times faster than LINQ.

Anybody can improve the performance of this code?

Here is the code for my test:

class Program
{
    static void Main(string[] args)
    {
        for (int test = 0; test < 100; test++)
        {
            object[] objectList = new object[1000];
            Random rnd = new Random();
            for (int i = 0; i < 1000; i++)
            {
                objectList[i] = new object[] { rnd.Next(1, 10000), rnd.Next(1, 10000), rnd.Next(1, 10000), rnd.Next(1, 10000), rnd.Next(1, 10000), rnd.Next(1, 10000), rnd.Next(1, 10000), rnd.Next(1, 10000), rnd.Next(1, 10000), rnd.Next(1, 10000), rnd.Next(1, 10000), rnd.Next(1, 10000), rnd.Next(1, 10000), rnd.Next(1, 10000), rnd.Next(1, 10000), rnd.Next(1, 10000), rnd.Next(1, 10000), rnd.Next(1, 10000), rnd.Next(1, 10000), rnd.Next(1, 10000), rnd.Next(1, 10000) };
            }
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            for (int i = 1; i < 100000; i++)
            {
                getReceivedIds(objectList);
            }
            stopWatch.Stop();
            var t1 = stopWatch.Elapsed.TotalMilliseconds;

            stopWatch.Restart();
            for (int i = 1; i < 100000; i++)
            {
                getReceivedIds2(objectList);
            }
            stopWatch.Stop();
            var t2 = stopWatch.Elapsed.TotalMilliseconds;

            Console.WriteLine(string.Format("LINQ: {0}  -  ForEach: {1}", t1, t2));
        }
    }


    private static List<int> getReceivedIds(object[] objectList)
    {
        List<int> received = objectList.Cast<object[]>()
        .Where(b => b != null)
        .Select(b => (int)b[0])  // or Convert.ToInt32(b[0])
        .ToList();
        return received;
    }

    private static List<int> getReceivedIds2(object[] objectList)
    {
        var received = new List<int>();
        foreach (object[] b in objectList)
        {
            if (b != null)
            {
                received.Add((int)b[0]);
            }
        }
        return received;
    }
}

If the first item in the object[] is actually an int you don't need to parse it, you can cast it. You could use this LINQ query if you want:

List<int> received = objectList.Cast<object[]>()
    .Where(b => b != null)
    .Select(b => (int)b[0])  // or Convert.ToInt32(b[0])
    .ToList(); 

try this:

 int[] resultArray = Array.ConvertAll(inputArray, x => Convert.ToInt32(x));

Note : Ensure that the values are int.
For reference : msdn link for ConvertAll()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM