Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace NiebezpiecznaKolekcja
- {
- class Program
- {
- // static readonly object blocker = new object();
- static void Main(string[] args)
- {
- List<Thread> watki = new List<Thread>();
- List<int> liczby = new List<int>(1000000);
- Random rand = new Random();
- Stopwatch stopWatch = new Stopwatch();
- stopWatch.Start();
- for (int i = 0; i < 100; i++)
- {
- var watek = new Thread(() =>
- {
- for (int l = 0; l < 10000; l++)
- // lock(blocker)
- {
- liczby.Add(rand.Next());
- }
- });
- watki.Add(watek);
- watek.Start();
- }
- foreach (var watek in watki)
- {
- watek.Join();
- }
- stopWatch.Stop();
- TimeSpan ts = stopWatch.Elapsed;
- string elapsedTime = String.Format("{0:00}.{1:00}",
- ts.Seconds,
- ts.Milliseconds / 10);
- Console.WriteLine("RunTime: " + elapsedTime);
- Console.WriteLine("Liczba elementów w liście zwykłej:" + liczby.Count);
- Console.ReadLine();
- }
- }
- }
- ***********
- bez locka
- 00.83
- liczba elementów 907406
- z
- 01.23
- liczba elementów 1000000
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace ConcurrentBag
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<Thread> watki = new List<Thread>();
- ConcurrentBag<int> liczby = new ConcurrentBag<int>();
- Random rand = new Random();
- Stopwatch stopWatch = new Stopwatch();
- stopWatch.Start();
- for (int i = 0; i < 100; i++)
- {
- var watek = new Thread(() =>
- {
- for (int l = 0; l < 10000; l++)
- liczby.Add(rand.Next());
- });
- watki.Add(watek);
- watek.Start();
- }
- foreach (var watek in watki)
- {
- watek.Join();
- }
- stopWatch.Stop();
- TimeSpan ts = stopWatch.Elapsed;
- string elapsedTime = String.Format("{0:00}.{1:00}",
- ts.Seconds,
- ts.Milliseconds / 10);
- Console.WriteLine("RunTime: " + elapsedTime);
- Console.WriteLine("Liczba elementów w liście zwykłej:" + liczby.Count);
- Console.ReadLine();
- }
- }
- }
- *************************
- 01.95
- 1000000
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace KolekcjaUnikalnaLock
- {
- public static class Extensions
- {
- public static bool IsUnique(this List<Thread> lista)
- {
- if (lista.Distinct().Count() == lista.Count()) //jeżeli liczba unikalnych jest równa liczbie wszystkich to wszystkie są unikalne.
- {
- return true;
- }
- // Console.WriteLine($"Liczba disctinct: {lista.Distinct().Count()} jest różna od liczby elementów: {lista.Count()}");
- return false;
- }
- public static bool IsUnique(this ConcurrentBag<int> cb)
- {
- if (cb.Distinct().Count() == cb.Count()) return true;
- else return false;
- }
- }
- class Program
- {
- static readonly object blocker = new Object();
- static void Main(string[] args)
- {
- List<Thread> watki = new List<Thread>();
- ConcurrentBag<int> liczby = new ConcurrentBag<int>();
- Stopwatch sw = new Stopwatch();
- sw.Start();
- Random rand = new Random();
- for (int i = 0; i < 100; i++)
- {
- var watek = new Thread(() =>
- {
- for (int l = 0; l < 100; l++)
- {
- int liczba = rand.Next(10001);
- lock (blocker)
- {
- while (liczby.Any(x => x == liczba))
- {
- liczba = rand.Next(10001);
- };
- liczby.Add(liczba);
- //Console.WriteLine(liczby.Distinct().Count());
- }
- }
- });
- watki.Add(watek);
- watek.Start();
- }
- foreach (var watek in watki)
- {
- watek.Join();
- }
- sw.Stop();
- TimeSpan ts = sw.Elapsed;
- string elapsedTime = String.Format("{0:00}.{1:00}",
- ts.Seconds,
- ts.Milliseconds / 10);
- Console.WriteLine("RunTime: " + elapsedTime);
- if (liczby.IsUnique()) //zastosowanie metody rozszerzającej dla CB
- Console.WriteLine("wszystkie liczby są unikalne");
- else
- Console.WriteLine("Nie wszystkie liczby są unikalne");
- Console.ReadLine();
- }
- }
- }
- ********************************************************
- 16.35
- 17.42
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement