Get number of cores and core IDs in foreach

60 Views Asked by At

Is there a way to get number of cores and their IDs utilized in these two foreach loops. I need to compare CPU utilization for both cases. I have execution times from System.Diagnostics.Stopwatch, but I also need info on number of cores. Here is my code for those loops:

//foreach encryption

var foreachWatch = System.Diagnostics.Stopwatch.StartNew();
                       
foreach (KeyValuePair<long, MyClient> klijent in clients)
{
    if (klijent.Value.id != obj.id)
    {
        MyClient tmp = new MyClient();
                                
        long id = klijent.Value.id;
        byte[] pubKey = Convert.FromBase64String(klijent.Value.keyString.ToString());
        byte[] IV = Convert.FromBase64String(klijent.Value.IVString.ToString());
        byte[] secretMessage = serverDK.Encrypt(pubKey, dekriptovanaSaUserInfo);
        tmp.id = id;
        tmp.pubKey = pubKey;
        tmp.buffer = klijent.Value.buffer;
        tmp.IV = IV;
        tmp.handle = klijent.Value.handle;
        tmp.client = klijent.Value.client;
        tmp.data = klijent.Value.data;
        tmp.IVString = klijent.Value.IVString;
        tmp.keyString = klijent.Value.keyString;
        tmp.stream = klijent.Value.stream;
        Send(Poruka(secretMessage), tmp);
        tmp.data.Clear();
        tmp.handle.Set();
    }
}

foreachWatch.Stop();
                        

//parallel foreach

var parallelWatch = System.Diagnostics.Stopwatch.StartNew();
                        
Parallel.ForEach(clients, klijent =>
{
    if (klijent.Value.id != obj.id)
    {
        MyClient tmp = new MyClient();
        long id = klijent.Value.id;
        byte[] pubKey = Convert.FromBase64String(klijent.Value.keyString.ToString());
        byte[] IV = Convert.FromBase64String(klijent.Value.IVString.ToString());
        byte[] secretMessage = serverDK.Encrypt(pubKey, dekriptovanaSaUserInfo);
        tmp.id = id;
        tmp.pubKey = pubKey;
        tmp.buffer = klijent.Value.buffer;
        tmp.IV = IV;
        tmp.handle = klijent.Value.handle;
        tmp.client = klijent.Value.client;
        tmp.data = klijent.Value.data;
        tmp.IVString = klijent.Value.IVString;
        tmp.keyString = klijent.Value.keyString;
        tmp.stream = klijent.Value.stream;
        Send(Poruka(secretMessage), tmp);
        tmp.data.Clear();
        tmp.handle.Set();
    }
});

parallelWatch.Stop();

Can't seem to find solution for this specific issue. Mostly solutions are for determining the number of CPU cores using Environment.ProcessorCount...

0

There are 0 best solutions below