简体   繁体   中英

Calculating execution time while using multi-threading

Is multi-threading more efficient for calculating the average of a large number of random generated values?

For a code that calculates the average of a large number of random generated values using three parallel threads. I tried to calculate execution time twice. Once using multi-threading and another time using only one thread but I don't understand why multi-threading takes a larger execution time.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <time.h>

long int count=0,sum=0;
HANDLE ht1,ht2,ht3,Semaphore1,Semaphore2;

LARGE_INTEGER Start_time,End_time,Elapsed_time;
LARGE_INTEGER Frequency;


DWORD WINAPI Counter(LPVOID param)
{
long int i=0;

QueryPerformanceFrequency(&Frequency);
QueryPerformanceCounter(&Start_time);

    while (count<1000000)
    {
     WaitForSingleObject(Semaphore2,INFINITE);
     if(count<1000000)
     {
     count++;
     sum=sum+rand()%100;
     i++;
     }
    ReleaseSemaphore(Semaphore2,1,0);
    }
QueryPerformanceCounter(&End_time);
Elapsed_time.QuadPart = End_time.QuadPart - Start_time.QuadPart;
Elapsed_time.QuadPart =  (Elapsed_time.QuadPart * (1000))/ Frequency.QuadPart;

printf("Thread %d generated %d numbers\n",GetCurrentThreadId(),i);

ReleaseSemaphore(Semaphore1,1,0);
}


int main() {

    DWORD ThreadID1,ThreadID2,ThreadID3;
    char c;

    srand (time(NULL));

    ht1 = CreateThread(NULL,0,Counter,NULL,CREATE_SUSPENDED,&ThreadID1);
    ht2 = CreateThread(NULL,0,Counter,NULL,CREATE_SUSPENDED,&ThreadID2);
    ht3 = CreateThread(NULL,0,Counter,NULL,CREATE_SUSPENDED,&ThreadID3);

    Semaphore1=CreateSemaphore(NULL,0,1,NULL);
    Semaphore2=CreateSemaphore(NULL,1,1,NULL);

    printf("Thread 1 id is %d \n",ThreadID1);
    printf("Thread 2 id is %d \n",ThreadID2);
    printf("Thread 3 id is %d \n",ThreadID3);

    ResumeThread(ht1);
    ResumeThread(ht2);
    ResumeThread(ht3);

    WaitForSingleObject(Semaphore1,INFINITE);

    printf("Count reached %d \n",count);
    printf("Sum reached %d \n",sum);
    printf("Average is %f \n",(float)sum/(float)count);
    printf("Time in ms %d \n",Elapsed_time.QuadPart);

    while(c != 'e') {c = getche();}
    return 0;
}

I expected time using multi-threading will be less than the time using one thread. using multi-threading output is 2899 ms using only one thread output is 947 ms

For multithreading to improve performance, some computation needs to be done outside a shared critical section by each thread. In this code, all of the computation is done by multiple threads competing for a lock on a single, shared critical section. In effect, the code is logically single-threaded, since no more than one thread can hold the lock to the critical section at once. However, adding more threads increases contention and scheduling latency on the lock, slowing down the computation that a single-threaded program would perform without locking.

Either find a way of splitting the task up amongst the threads such that they merge their results at the end, or use one thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM