我正在尝试使用一个简单的求和任务来测试多线程,我想在其中比较单线程与多线程。
单线程:
long long summation(int start, int end)
{
long long total = 0;
for (int i = start; i < end; i++)
{
total += i;
}
return total;
}
Run Code Online (Sandbox Code Playgroud)
和多线程:
long long threadedSummation(int numThreads, int start, int end)
{
long long total = 0;
int summationRange = (start + end) / numThreads; //The range of numbers for every thread to calculate
for (int i = 0; i < numThreads; i++)
{
int start = summationRange * i;
int …Run Code Online (Sandbox Code Playgroud)