Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Security.Cryptography;
- using System.Text;
- using System.Threading;
- namespace ThreadTest
- {
- class Program
- {
- const int Count = 1_000_000;
- const int Cores = 4;
- static Thread[] Threads;
- static void Main(string[] args)
- {
- Threads = new Thread[Cores];
- var countPerThread = Count / Cores;
- for (var i = 0; i < Cores; i++)
- {
- Threads[i] = new Thread(() => { ThreadFunction(i, countPerThread); });
- Threads[i].Start();
- Console.WriteLine($"Thread {i} dispatched.");
- }
- }
- static void ThreadFunction(int id, int count)
- {
- var sw = new Stopwatch();
- sw.Start();
- var rand = new Random();
- for (int i = 0; i < count; i++) ComputeSha256Hash(rand.Next().ToString());
- sw.Stop();
- Console.WriteLine($"Finished: {sw.ElapsedMilliseconds}ms.");
- }
- // Heavy task copied from https://www.c-sharpcorner.com/article/compute-sha256-hash-in-c-sharp/
- static string ComputeSha256Hash(string rawData)
- {
- // Create a SHA256
- using (SHA256 sha256Hash = SHA256.Create())
- {
- // ComputeHash - returns byte array
- byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData));
- // Convert byte array to a string
- StringBuilder builder = new StringBuilder();
- for (int i = 0; i < bytes.Length; i++)
- {
- builder.Append(bytes[i].ToString("x2"));
- }
- return builder.ToString();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement