안녕지구 #developer #bompapa

소소한팁 - 수행 시간(elapsed time) 계산

|

알고리즘을 풀다 보면 어디에서 병목이 생기는지 알아야 할 때가 있습니다. 그때 구간별 수행 시간을 출력하면 쉽게 파악할 수 있습니다.

예시는 구조체를 초기화하는 두 가지 방법에 대해서 측정해보았습니다.

구현

#include <stdio.h>
#define MAXN 5000000

struct Node {
	int id;
	int time;
	int like;
	Node* next;
};

Node node_arr[MAXN];
Node node_arr2[MAXN];

// time 인터페이스 가져오기
#include <time.h>

int main() {
	
	/* Case 1*/////

	// 시작 시간
	clock_t t = clock();

	for (int i = 0; i < MAXN; i++) {
		node_arr[i].id = 0;
		node_arr[i].like = 0;
		node_arr[i].time = 0;
		node_arr[i].next = 0;
	}

	// 시간 계산
	t = clock() - t;
	printf("%f \n", ((double)t / CLOCKS_PER_SEC));



	/* Case 2 */////

	// 시작 시간
	clock_t t2 = clock();

	for (int i = 0; i < MAXN; i++) {
		node_arr2[i] = { 0, 0, 0, 0 };
	}

	t2 = clock() - t2;
	printf("%f \n", ((double)t2 / CLOCKS_PER_SEC));
}

출력

0.080000
0.118000

Comments