computablee / DotMP

A collection of powerful abstractions for parallel programming in .NET with an OpenMP-like API.
https://computablee.github.io/DotMP/
GNU Lesser General Public License v2.1
29 stars 8 forks source link

[PERFORMANCE] `GetThreadNum` calls `Convert.ToInt32` upon each call #110

Closed computablee closed 10 months ago

computablee commented 10 months ago

Identify the function(s) which are causing performance issues.

The internal implementation of GetThreadNum calls Convert.ToInt32 on the thread's name each time it's called. String->int conversion is notably slow, so this can be drastically improved by caching the results on the first call into a thread-local variable.

Provide example code.

public static int GetThreadNum()
{
    if (!InParallel())
    {
        throw new NotInParallelRegionException("Cannot get current thread number outside of a parallel region.");
    }

    return Convert.ToInt32(Thread.CurrentThread.Name);
}

This is the implementation of GetThreadNum.

Describe the performance expected vs. performance observed.

TODO: fill in later

Desktop (please complete the following information):

Additional context?

Self-assigning this issue for now.