Skip to content

Instantly share code, notes, and snippets.

@honux77
Last active December 15, 2015 12:18
Show Gist options
  • Save honux77/5258765 to your computer and use it in GitHub Desktop.
Save honux77/5258765 to your computer and use it in GitHub Desktop.
call by ref test 프로그램 값에 의한 호출과, 참조에 의한 호출을 여러번 반복하고 걸리는 시간을 비교해 보는 프로그램
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <time.h>
struct bigdata
{
int dataid;
char data[4096];
};
// (1) src의 문자열을 bd.data에 복사
// 문자열 복사에는 표준 문자열 함수 strcpy(char *dest, const char *src) 사용
struct bigdata bad_strcpy_bigdata(struct bigdata bd, const char *src)
{
//여기 구현
return bd;
}
// (2) src의 문자열을 p->data에 복사
void strcpy_bigdata(struct bigdata *p, const char *src)
{
//여기 구현
}
int main()
{
struct bigdata b1;
clock_t t;
double sec;
int i;
t = clock();
for(i=0; i < 5000000; i++)
b1 = bad_strcpy_bigdata(b1, "very big data");
t = clock() -t;
sec = (double) t / CLOCKS_PER_SEC;
printf("data: %s, time %.4fs\n", b1.data,sec );
assert(strcmp(b1.data,"very big data")==0);
// (3) strcpy_bigdata 함수 사용 b1.data를 'more big data'
// 로 변경
t = clock();
for(i=0; i < 5000000; i++)
strcpy_bigdata( ,"more big data");
t = clock() -t;
sec = (double) t / CLOCKS_PER_SEC;
printf("data: %s, time %.4fs\n", b1.data,sec );
assert(strcmp(b1.data,"more big data")==0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment