# 【转载】在C / C ++中测量执行时间的8种方法

2022/04/08 16:51

=====================================================

## 1.使用“ time” Linux命令

\$ time ./MyProgram
Result: 2.00000000000000000000

real 0m5.931s
user 0m5.926s
sys 0m0.005s

## 2.使用<chrono>

<chrono>库可以访问您机器中的几个不同的时钟，每个时钟都有不同的用途和特性。如果需要，您可以在此处获取有关每种时钟的更多详细信息。但是，除非您确实需要其他时钟，否则我建议只使用high_resolution_clock。该时钟使用的是最高分辨率的时钟，因此对大多数人来说可能就足够了。使用方法如下：

#include <stdio.h>
#include <chrono>

int main () {
double sum = 0;

// Start measuring time
auto begin = std::chrono::high_resolution_clock::now();

int iterations = 1000*1000*1000;
for (int i=0; i<iterations; i++) {
}

// 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;
}


## 3.使用<sys / time.h>和gettimeofday（）

#include <stdio.h>
#include <sys/time.h>

int main () {
double sum = 0;

// Start measuring time
struct timeval begin, end;
gettimeofday(&begin, 0);

int iterations = 1000*1000*1000;
for (int i=0; i<iterations; i++) {
}

// 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;
}


## 4.使用<time.h>和time（）

#include <stdio.h>
#include <time.h>

int main () {
double sum = 0;

// Start measuring time
time_t begin, end;
time(&begin);

int iterations = 1000*1000*1000;
for (int i=0; i<iterations; i++) {
}

// 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;
}


## 5.使用<time.h>和clock（）

#include <stdio.h>
#include <time.h>

int main () {
double sum = 0;

// Start measuring time
clock_t start = clock();

int iterations = 1000*1000*1000;
for (int i=0; i<iterations; i++) {
}

// 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;
}


## 6.使用<time.h>和clock_gettime（）

#include <time.h>

int main () {
double sum = 0;

// Start measuring time
struct timespec begin, end;
clock_gettime(CLOCK_REALTIME, &begin);

int iterations = 1000*1000*1000;
for (int i=0; i<iterations; i++) {
}

// 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;
}


## 7.使用<sysinfoapi.h>和GetTickCount64（）

#include <stdio.h>
#include <sysinfoapi.h>

int main () {
double sum = 0;

// Start measuring time
long long int begin = GetTickCount64();

int iterations = 1000*1000*1000;
for (int i=0; i<iterations; i++) {
}

// 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>

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;

// Start measuring time
double begin = get_cpu_time();

int iterations = 1000*1000*1000;
for (int i=0; i<iterations; i++) {
}

// 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;
}


0 评论
0 收藏
0