정리필요2

Call by Value 와 Call by Reference 의 속도 차이

ShineWithMe 2008. 9. 1. 23:18

#include <iostream>
#include <time.h>
#include <string>
#include <windows.h>
using namespace std;
#define REPEAT 1000000

void swapReference(string *s1, string *s2);
void swapValue(string s3, string s4);
double showReference(string *s1, string *s2);
double showValue(string string3, string string4);

void main(void)
{
 double timeo, timen;
 char str1[100000] = "kangwon.national.univ.";
 char str2[] = "com.eng.c";
 char str3[100000] = "kangwon.national.univ.";
 char str4[] = "com.eng.c";

 string string1 = str1;
 string string2 = str2;
 string string3 = str3;
 string string4 = str4;


 printf("## swap 함수 호출 방식에 따른 속도 ##\n\n");
 timeo = showReference(&string1, &string2);
 timen = showValue(string3, string4);
 printf("-----------------------------------------------\n");
 printf("처리시간차이 : Value - Reference = %lf\n\n", timen - timeo);

 return;
}


double showReference(string *string1, string *string2)
{
 clock_t start1, end1;
 int i = 0, num = 0;

 start1 = clock(); //시간측정 start
 while(i<REPEAT)
 {  
  swapReference(string1, string2);
  i++;
 }
 //Sleep(100); // 보기쉬운 시간비교를 위해 똑같은 딜래이
 end1 = clock(); //시간측정 end

 num = (int)string1->length();
 printf("-----------------Reference 함수 ---------------\n");
 printf("swap함수 %d번 호출시 속도 : %f\n", REPEAT, (end1-start1)/(double) CLOCKS_PER_SEC);
 printf("원본문자열 길이는 : %d\n\n", num);
 return (end1-start1)/(double) CLOCKS_PER_SEC;

}

double showValue(string string3, string string4)
{
 clock_t start2, end2;
 int i = 0, num = 0;


 start2 = clock(); //시간측정 start
 while(i<REPEAT)
 {  
  swapValue(string3, string4);
  i++;
 }
 //Sleep(100); // 보기쉬운 시간비교를 위해 똑같은 딜래이
 end2 = clock(); //시간측정 end

 num = (int)string3.length();
 printf("----------------Value 함수---------------\n");
 printf("swap함수 %d번 호출시 속도 : %f\n", REPEAT, (end2-start2)/(double) CLOCKS_PER_SEC);
 printf("원본문자열 길이는 : %d\n\n", num);
 return (end2-start2)/(double) CLOCKS_PER_SEC;
}

void swapReference(string *s1, string *s2)
{
 string *temp;

 temp = s1;
 s1 = s2;
 s2 = temp;
}
void swapValue(string s3, string s4)
{
 string temp;
 
 temp = s3;
 s3 = s4;
 s4 = temp;
}