2013-03-02 1 views
-3

루프와 멀티 스레드 모두 동일한 작업을 실행하는 경우 어느 것이 가장 오래 걸릴까요? 멀티 스레드의 실행 시간 내 프로그램에서루프 또는 멀티 스레드의 실행 시간이 오래 걸립니까?

이 내 프로그램이 하나의 스레드에 대한 기능 및 루프

에 대한
#include <pthread.h> 
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <time.h> 

int ** A; 
int ** B; 
int ** C; 
int ** D; 

int k; 
int r ; 

clock_t begin_1, end_1, begin_2, end_2; 
double time_spent_1, time_spent_2; 


struct arguments{ 
int i; 
int j; 
    int n; 
}; 

void initialize(){ 

A = (int **)malloc(2*sizeof(int *)); 
int i; 
for(i = 0; i<2; i++) 
    A[i] = (int*) malloc(2*sizeof(int)); 

B = (int **)malloc(2*sizeof(int *)); 
for(i = 0; i<2; i++) 
    B[i] = (int*)malloc(2*sizeof(int)); 

    C = (int **)malloc(2*sizeof(int *)); 
for(i = 0; i<2; i++) 
    C[i] = (int*)malloc(2*sizeof(int)); 

    D = (int **)malloc(2*sizeof(int *)); 
for(i = 0; i<2; i++) 
    D[i] = (int*)malloc(2*sizeof(int)); 


    } 

void generate(){ 

int i,j; 

for (i=0; i<2; i++) 
    for (j=0; j<2; j++){ 
     r = rand()%100; 
     A[i][j]=r; 
    } 

for (i=0; i<2; i++) 
    for (j=0; j<2; j++){ 
     r = rand()%100; 
     B[i][j]=r; 
    } 
    } 

void mutip_1() 
{ 
    int i,j,temp,k; 

begin_1 = clock(); 

for (i=0; i<2; i++){ 
    for (j=0; j<2; j++){ 
     temp = A[i][j]; 
     for(k=0;k<2;k++){ 
      C[i][k]+=temp*B[j][k]; 
     } 
    } 
    } 

    end_1 = clock(); 

} 

void* mul_mat(void* args) 
{ 
struct arguments * temp = (struct arguments*) args; 
int i = temp->i; 
int j = temp->j; 
int n = temp->n; 

pthread_detach(pthread_self()); 

free(temp); 

int k; 

for(k=0;k<2;k++){ 
    D[i][k]+=n*B[j][k]; 
} 

pthread_exit(NULL); 
} 

int main(){ 

initialize(); 
generate(); 
mutip_1(); 

pthread_t* tid = (pthread_t*)malloc((2*2)*sizeof(pthread_t)); malloc((2*2)*sizeof(pthread_t)); 

int i, j,k=0; 
int n; 

begin_2 = clock(); 

for(i=0; i<2; i++){ 
    for(j=0; j<2; j++){ 
     n=A[i][j]; 
     struct arguments *args=(struct arguments*)malloc((2*2)*sizeof(struct arguments)); 
     args->i = i; 
     args->j = j; 
     args->n=n; 
     if (pthread_create(&tid[k++], NULL, (void*)mul_mat, (void*)args)){ 
      perror("Thread Problem"); 
      exit(1); 
     } 
    } 
} 
end_2 = clock(); 

time_spent_1 = (double)(end_1 - begin_1)/CLOCKS_PER_SEC; 
time_spent_2 = (double)(end_2 - begin_2)/CLOCKS_PER_SEC; 

printf("Matrix A: \n"); 
for (i=0; i<2; i++){ 
for (j=0; j<2; j++) 
printf("%d ", A[i][j]); 
printf("\n"); 
} 

printf("Matrix B: \n"); 
for (i=0; i<2; i++){ 
for (j=0; j<2; j++) 
printf("%d ", B[i][j]); 
printf("\n"); 
} 

printf("multiplication using loop: \n"); 
for (i=0; i<2; i++){ 
for (j=0; j<2; j++) 
printf("%d ", C[i][j]); 
printf("\n"); 
} 
printf("multiplication using thread: \n"); 
for (i=0; i<2; i++){ 
    for (j=0; j<2; j++) 
     printf("%d ", D[i][j]); 
    printf("\n"); 
} 

printf("First time using loop %f \n", time_spent_1); 
printf("second time using thread %f \n", time_spent_2); 

return 0; 
} 
+1

이 질문은 매우 불분명합니다. –

+0

내 프로그램에서 2 행렬 곱셈을 계산하기 위해 루프를 사용했다면, 같은 작업을 수행하기 위해 멀티 스레드를 사용했는데, 루프의 시간은 스레드 시간보다 길어 집니까? – fatimah

+0

왜 테스트하지 않습니까?! – alk

답변

1

I 사이

의 다른입니다

LANG

C로 가장 긴
for i=1 to 50 
    do something 
next i 

및 II

for i=1 to 50 
    create thread that does the same something 
next i 
,

는 II 계산을 병렬로 실행할 수 있도록 제공되고, 이론적 결과 스레드 (무료로) 여러 개의 CPU/코어에서 실행할 수 있는지

  • 가 II가 빠르게 처리 될 수있다
  • 스레드가 동일한 실행한다면 CPU (코어)로 인해 스레드 관리 오버 헤드가 약간 더 빨라야합니다.
+2

이것은 그림의 작은 부분 일뿐입니다. 예를 들어, 짧은 작업 (CPU가 수백 초의 OPs/초를 처리 할 수 ​​있음)의 경우 새로운 스레드를 만드는 오버 헤드가 모든 이점보다 클 수 있습니다. –

0

스레드 풀에 대한 스레드 생성 및/또는 발행 작업은 시스템 호출 및 스레드 간 통신으로 인해 상당한 오버 헤드를 가지므로 멀티 코어 CPU :

매트릭스의 치수 = 2, 한 스레드의 루프가 빠릅니다.

매트릭스 크기 = 2000000, 다중 스레드의 루프가 빠릅니다.

요약 : 사소한 CPU 사용량이 많은 작업을 처리하지 마십시오.

관련 문제