tgool
Tgool
tgool
전체 방문자
오늘
어제
  • 분류 전체보기
    • Data Science
      • AI
      • Data Mining
      • ML(Machine Learning)
    • Computer Science
      • 자료구조
      • 알고리즘
      • 시스템 프로그래밍
      • 운영체제
      • 컴퓨터 구조
      • 컴퓨터 네트워크
      • 데이터 베이스
      • 파이썬
      • 자바
      • 아두이노
    • Math
      • 통계학
      • 확률론
      • 선형대수학
      • 수리통계학
      • 회귀분석
    • TOFEL
    • Git
    • Plan
    • Book
    • Working out
      • 영양과 생활
      • 운동 정보
      • 운동 기록

인기 글

최근 글

최근 댓글

hELLO · Designed By 정상우.
tgool

Tgool

[CSAPP] 5.3 Program Example(프로그램 예제)
Computer Science/컴퓨터 구조

[CSAPP] 5.3 Program Example(프로그램 예제)

2023. 1. 30. 00:09

5.3 Program Example(프로그램 예제)

  • 해당 절은 향후 나올 절에 나올 예시들을 설명하기 위한 절이다. 
  • 코드를 최적화하는 과정을 보여주기 위해 Vector 데이터 구조체를 예시로 이용하겠다.
  • 벡터 데이터 구조체는 다음과 같이 코드와 그림으로 표현된다.
  • data_t는 데이터 유형이다. typedef long data_t; 와 같이 벡터로 이루어진 데이터의 유형(int, long, double 등)을 설정한다.
/* Create abstract data type for vector */
typedef struct {
	long len;
	data_t *data;
} vec_rec, *vec_ptr;

  • 아래 코드는 벡터를 생성하고, 벡터 요소에 접근하고, 벡터의 길이를 결정하기 위한 과정이다.
  • 벡터 엑서스 루틴인 get_vec_element가 모든 벡터 참조에 대해 경계 검사(Bounds checking)를 수행한다.
    • 경계 검사(Bounds checking): index 유효 범위 확인
  • 경계 검사(Bounds checking)는 프로그램 오류 가능성을 줄이지만, 프로그램 실행 속도를 늦출 수 있다.
/* Create vector of specified length */
vec_ptr new_vec(long len)
{
	/* Allocate header structure */
	vec_ptr result = (vec_ptr) malloc(sizeof(vec_rec));
	data_t *data = NULL;
	if (!result)
    	return NULL; /* Couldn’t allocate storage */
	result->len = len;
10 /* Allocate array */
	if (len > 0) {
		data = (data_t *)calloc(len, sizeof(data_t));
		if (!data) {
			free((void *) result);
			return NULL; /* Couldn’t allocate storage */
		}
	}
/* Data will either be NULL or allocated array */
	result->data = data;
	return result;
}

/*
* Retrieve vector element and store at dest.
* Return 0 (out of bounds) or 1 (successful)
*/
int get_vec_element(vec_ptr v, long index, data_t *dest)
{
	if (index < 0 || index >= v->len)
		return 0;
	*dest = v->data[index];
	return 1;
}

/* Return length of vector */
long vec_length(vec_ptr v)
{
	return v->len;
}
  • 아래 코드는 최적화의 예로 컴파일 시간 상수 IDENT와 연산자 OP의 서로 다른 정의를 사용하여 데이터에 대해 서로 다른 작업이 가능하다. 앞으로 다른 절의 예시에서 사용될 예정이다. 
    • IDENT가 덧셈 연산일 때는 0으로 정의된 이유는 덧셈 연산은 0부터 시작하기 때문이고, 곱셈 연산일 때는 1로 정의된 이유는 곱셈 연산은 1부터 시작하기 때문이다. OP가 + 면 덧셈 연산, *면 곱셈 연산

/* Implementation with maximum use of data abstraction */
	void combine1(vec_ptr v, data_t *dest)
{
	long i;

	*dest = IDENT;
	for (i = 0; i < vec_length(v); i++) {
		data_t val;
		get_vec_element(v, i, &val);
		*dest = *dest OP val;
	}
}
  • 또한, CPE 성능을 측정하기 위해 해당 책에서는 Intel Core i7 Haswell processor(머신)를 사용했고 머신과 컴파일러의 조합에 따라 동일한 결과가 아닐 수도 있지만, 일반적으로 해당 책에서 측정한 결과와 유사하게 나올 것이다. 
  • 머신이 32bit 인지 64bit인지 중요하지 않다. 나눗셈을 제외한 연산에서는 동일한 성능을 가지기 때문이다. 
  • 아래 그림은 최적화하지 않았을 때와, 최적화 옵션 -01를 적용했을 때 CPE를 보여준다. 
  • 최적화 옵션을 제공함으써, 프로그램 성능이 2배 이상 향상된 것을 알 수 있다.
  • 따라서 최적화 옵션을 주는 것을 습관화하자.

 

'Computer Science > 컴퓨터 구조' 카테고리의 다른 글

[CSAPP] 5.5 Reducing Procedure Calls(프로시저 호출 줄이기)  (0) 2023.01.30
[CSAPP] 5.4 Eliminating Loop Inefficiencies(루프 비효율성 제거하기)  (0) 2023.01.30
[CSAPP] 5.2 Expressing Program Performance(프로그램 성능의 표현)  (0) 2023.01.29
[CSAPP] 5.1 Capabilities and Limitations of Optimizing Compilers(컴파일러 최적화의 능력과 한계)  (0) 2023.01.29
[CSAPP] Chapter 05. Optimizing Program Performance(프로그램 성능 최적화 하기)  (0) 2023.01.29
    'Computer Science/컴퓨터 구조' 카테고리의 다른 글
    • [CSAPP] 5.5 Reducing Procedure Calls(프로시저 호출 줄이기)
    • [CSAPP] 5.4 Eliminating Loop Inefficiencies(루프 비효율성 제거하기)
    • [CSAPP] 5.2 Expressing Program Performance(프로그램 성능의 표현)
    • [CSAPP] 5.1 Capabilities and Limitations of Optimizing Compilers(컴파일러 최적화의 능력과 한계)
    tgool
    tgool

    티스토리툴바