https://levelup.gitconnected.com/8-ways-to-measure-execution-time-in-c-c-48634458d0f9
참조는 위와 같으며, 혹시 page 가 삭제될 경우를 대비하여, 코드를 다시 복사 함
#include <stdio.h>
#include <chrono>
int main () {
double sum = 0;
double add = 1;
// Start measuring time
auto begin = std::chrono::high_resolution_clock::now();
int iterations = 1000*1000*1000;
for (int i=0; i<iterations; i++) {
sum += add;
add /= 2.0;
}
// Stop measuring time and calculate the elapsed time
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
printf("Result: %.20f\n", sum);
printf("Time measured: %.3f seconds.\n", elapsed.count() * 1e-9);
return 0;
}
#include <stdio.h>
#include <sys/time.h>
int main () {
double sum = 0;
double add = 1;
// Start measuring time
struct timeval begin, end;
gettimeofday(&begin, 0);
int iterations = 1000*1000*1000;
for (int i=0; i<iterations; i++) {
sum += add;
add /= 2.0;
}
// Stop measuring time and calculate the elapsed time
gettimeofday(&end, 0);
long seconds = end.tv_sec - begin.tv_sec;
long microseconds = end.tv_usec - begin.tv_usec;
double elapsed = seconds + microseconds*1e-6;
printf("Result: %.20f\n", sum);
printf("Time measured: %.3f seconds.\n", elapsed);
return 0;
}
#include <stdio.h>
#include <time.h>
int main () {
double sum = 0;
double add = 1;
// Start measuring time
time_t begin, end;
time(&begin);
int iterations = 1000*1000*1000;
for (int i=0; i<iterations; i++) {
sum += add;
add /= 2.0;
}
// Stop measuring time and calculate the elapsed time
time(&end);
time_t elapsed = end - begin;
printf("Result: %.20f\n", sum);
printf("Time measured: %ld seconds.\n", elapsed);
return 0;
}
#include <stdio.h>
#include <time.h>
int main () {
double sum = 0;
double add = 1;
// Start measuring time
clock_t start = clock();
int iterations = 1000*1000*1000;
for (int i=0; i<iterations; i++) {
sum += add;
add /= 2.0;
}
// Stop measuring time and calculate the elapsed time
clock_t end = clock();
double elapsed = double(end - start)/CLOCKS_PER_SEC;
printf("Result: %.20f\n", sum);
printf("Time measured: %.3f seconds.\n", elapsed);
return 0;
}
#include <stdio.h>
#include <time.h>
int main () {
double sum = 0;
double add = 1;
// Start measuring time
struct timespec begin, end;
clock_gettime(CLOCK_REALTIME, &begin);
int iterations = 1000*1000*1000;
for (int i=0; i<iterations; i++) {
sum += add;
add /= 2.0;
}
// Stop measuring time and calculate the elapsed time
clock_gettime(CLOCK_REALTIME, &end);
long seconds = end.tv_sec - begin.tv_sec;
long nanoseconds = end.tv_nsec - begin.tv_nsec;
double elapsed = seconds + nanoseconds*1e-9;
printf("Result: %.20f\n", sum);
printf("Time measured: %.3f seconds.\n", elapsed);
return 0;
}
#include <stdio.h>
#include <sysinfoapi.h>
int main () {
double sum = 0;
double add = 1;
// Start measuring time
long long int begin = GetTickCount64();
int iterations = 1000*1000*1000;
for (int i=0; i<iterations; i++) {
sum += add;
add /= 2.0;
}
// Stop measuring time and calculate the elapsed time
long long int end = GetTickCount64();
double elapsed = (end - begin)*1e-3;
printf("Result: %.20f\n", sum);
printf("Time measured: %.3f seconds.\n", elapsed);
return 0;
}
#include <stdio.h>
#include <processthreadsapi.h>
double get_cpu_time(){
FILETIME a,b,c,d;
if (GetProcessTimes(GetCurrentProcess(),&a,&b,&c,&d) != 0){
// Returns total user time.
// Can be tweaked to include kernel times as well.
return
(double)(d.dwLowDateTime |
((unsigned long long)d.dwHighDateTime << 32)) * 0.0000001;
}else{
// Handle error
return 0;
}
}
int main () {
double sum = 0;
double add = 1;
// Start measuring time
double begin = get_cpu_time();
int iterations = 1000*1000*1000;
for (int i=0; i<iterations; i++) {
sum += add;
add /= 2.0;
}
// Stop measuring time and calculate the elapsed time
double end = get_cpu_time();
double elapsed = (end - begin);
printf("Result: %.20f\n", sum);
printf("Time measured: %.3f seconds.\n", elapsed);
return 0;
}
'Linux_system' 카테고리의 다른 글
POSIX.1 이 뭐지? (0) | 2022.12.09 |
---|---|
netplan 고정 IP 설정시, 서브넷 마스크 작성법 (0) | 2022.12.09 |
우분투 히든파일(숨김파일) 보이기/감추기 (0) | 2022.12.09 |
c 에서 pthread 를 사용하여 shell script 실행 (0) | 2022.12.06 |
Multiplexed I/O - poll() (0) | 2022.09.18 |