Új hozzászólás Aktív témák

  • joysefke

    veterán

    Sziasztok, csináltam egy példa kódot List<List<Color>> struktúra létrehozására. Tudom, hogy a listakreálás lassú, stb, de szükségem van a felxibilitására.

    Készítettem egy benchmarkot úgy hogy párhuzamosság nélkül kreálok egy 4000 elemű List<> szerkezetet amit List<Color> elemeket tartalmaz (4000 elemű lista üres List<Color> elemekkel). Majd ugyanezt a listát megkreálom 250 elemű szublisták konkatenálásával, melyeket külön taskonként asszinkron módon hozok létre.

    A kód változtatás és többletmunka nélkül fordul Visual Studióban

    azt látom, hogy a párhuzamosítással nem tudok érdemi sebességelőnyt elérni. Én csinálok valamit nagyon rosszul, vagy a .NET olyan ravasz, hogy a List<List<>> kreálását már magától párhuzamosan végzi???

    Előre is köszi !

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Drawing;
    using System.Diagnostics;

    namespace ConsoleApp30
    {
    class Program
    {
    private const int width = 4000;
    private const int height = 3000;

    //Ez lesz a delegate függvény a Taskok megkreálásához
    static public List<List<Color>> createSublist (int n, int height)
    {
    List<List<Color>> result = new List<List<Color>>(n);

    for (int i = 0; i< n; ++i)
    {
    result.Add(new List<Color>(height));
    }
    return result;
    }


    static void Main(string[] args)
    {

    Stopwatch stopwatch = new Stopwatch();
    Stopwatch stopwatch1 = new Stopwatch();
    List<List<Color>> pixelList = new List<List<Color>>(width);

    //Listakreálás párhuzamosítás nélkül
    stopwatch.Start();
    for (int i = 0; i < width; ++i)
    {
    pixelList.Add(new List<Color>(height));
    }
    stopwatch.Stop();
    Console.WriteLine("Sequential :" + stopwatch.ElapsedMilliseconds);
    stopwatch.Reset();

    int numOfCores = Environment.ProcessorCount;
    int numOfTasks = numOfCores * 4;

    Task<List<List<Color>>>[] tasks = new Task<List<List<Color>>>[numOfTasks];

    long[] threadCreations = new long[numOfTasks];
    stopwatch.Start();
    for (int i= 0; i< numOfTasks; ++i)
    {
    stopwatch1.Start();
    tasks[i] = new Task<List<List<Color>>> (() => createSublist(width / numOfTasks, height));
    stopwatch1.Stop();
    threadCreations[i] = stopwatch1.ElapsedMilliseconds;
    stopwatch1.Reset();

    }
    for (int i=0; i< numOfTasks; ++i)
    {
    tasks[i].Start();
    }

    for (int i = 0; i < numOfTasks; ++i)
    {
    Console.WriteLine("Task nr {0} state: {1}", i, tasks[i].Status.ToString());
    }

    Task.WaitAll(tasks);
    stopwatch.Stop();

    Console.WriteLine("Creating {0} sublists in parallel took: {1} ms", numOfTasks, stopwatch.ElapsedMilliseconds);
    stopwatch.Reset();

    for (int i = 0; i < numOfTasks; ++i) {
    Console.WriteLine("Creating task nr {0} took: {1} ms", numOfTasks, threadCreations[i]);
    }


    stopwatch.Start();
    IEnumerable<List<Color>> result = tasks[0].Result.AsEnumerable();
    for (int i = 1; i < numOfTasks; ++i)
    {
    result = result.Concat(tasks[i].Result);
    }

    var cList = result.ToList();
    stopwatch.Stop();

    Console.WriteLine("Concat of {0} Lists took :{1} ms", numOfTasks, stopwatch.ElapsedMilliseconds);
    stopwatch.Reset();

    }
    }
    }

Új hozzászólás Aktív témák