서론
이 장은 C/Python을 통해 시간 측정하는 방법에 대해 설명한다.
목차
Python
time.time() : 프로그램 전체의 실행시간 측정(sleep을 측정)
절대적인 시간을 기준으로 측정된다. (i.e. “real-world time” - type of time we’re used to)
.단위는 second(초)
import time
result = 0
start = time.time()
time.sleep(3)
end = time.time()
print(f"Time: {end-start:.5f}sec")
time.perf_counter() : 프로그램 전체의 실행시간 측정(sleep을 측정)
상대적인 시간을 측정한다. (No defined relationship to real-world time - should only be used to measure time intervals)
millisecond(ms)로 사용하고싶은 경우 (end-start)*1000 를 사용한다.
단위는 second(초)
start = time.perf_counter()
time.sleep(3)
end = time.perf_counter()
print(f"Time: {(end-start):.5f}s")
time.process_time() : 프로그램이 실제 처리된 시간으로 코어 효율성을 비교하는데 사용
(sleep을 측정하지 않음)
단위는 second(초)
start = time.process_time()
time.sleep(5)
end = time.process_time()
print(f"Time: {end-start:.5f}") # 0.00004sec
C
clock_t start = clock(): CPU의 클럭수로 시간을 측정(sleep을 측정하지 않는다.)
단위는 millisecond(ms)이며 second(초)로 변환하고 싶다면 CLOCKS_PER_SEC 로 나눠주면 된다.
#include<time.h>
#include <stdio.h>
#include <unistd.h>
int main() {
clock_t start, end;
start = clock();
sleep(1);
end = clock();
printf("Time: %lfsec\n",double((end-start)/CLOCKS_PER_SEC)); //0.00000sec
return 0;
}
time(): 현재 캘린더의 시간을 Return한다.
위 함수를 사용하면 프로그램 전체의 실행시간 측정이 가능하다(sleep 측정)
단위는 second(초)
int main() {
time_t start, end;
time(&start);
sleep(2);
time(&end);
printf("Time = %5fsec\n", (float)(end - start));
return 0;
}
[reference]
https://80000coding.oopy.io/bab5ff29-4c69-4e16-8786-62f6d267ba20
Comment