执行时间: 给定任务的执行时间或CPU时间定义为系统执行该任务所花费的时间,也就是程序运行的时间。 有多种方法可以测量程序的执行时间,在本文中,我将讨论5种不同的测量方法 测量程序的执行时间。
- 使用
time()
C+C++中的函数 .
时间() 函数的作用是:返回从纪元(1970年1月1日)开始的时间,单位为秒。 头文件: “时间,h” 原型/语法: 时间(time_t*tloc); 返回值: 成功时,返回历元后的时间值(秒),错误时返回-1。
下面的程序演示了如何使用
time()
作用#include <bits/stdc++.h>
using
namespace
std;
// A sample function whose time taken to
// be measured
void
fun()
{
for
(
int
i=0; i<10; i++)
{
}
}
int
main()
{
/* Time function returns the time since the
Epoch(jan 1 1970). Returned time is in seconds. */
time_t
start, end;
/* You can call it like this : start = time(NULL);
in both the way start contain total time in seconds
since the Epoch. */
time
(&start);
// unsync the I/O of C and C++.
ios_base::sync_with_stdio(
false
);
fun();
// Recording end time.
time
(&end);
// Calculating total time taken by the program.
double
time_taken =
double
(end - start);
cout <<
"Time taken by program is : "
<< fixed
<< time_taken << setprecision(5);
cout <<
" sec "
<< endl;
return
0;
}
输出:Time taken by program is : 0.000000 sec
- 使用
clock()
在C++C++中的功能。
时钟(): clock()返回自程序启动以来经过的时钟滴答数。 头文件: “时间,h” 原型/语法: 时钟(无效); 返回值: 成功时,返回的值是时钟使用的CPU时间;要获得使用的秒数,请除以每秒时钟数。在出现错误时,返回-1。
下面的程序演示了如何使用
clock()
作用你也可以看到 这#include <bits/stdc++.h>
using
namespace
std;
// A sample function whose time taken to
// be measured
void
fun()
{
for
(
int
i=0; i<10; i++)
{
}
}
int
main()
{
/* clock_t clock(void) returns the number of clock ticks
elapsed since the program was launched.To get the number
of seconds used by the CPU, you will need to divide by
CLOCKS_PER_SEC.where CLOCKS_PER_SEC is 1000000 on typical
32 bit system. */
clock_t
start, end;
/* Recording the starting clock tick.*/
start =
clock
();
fun();
// Recording the end clock tick.
end =
clock
();
// Calculating total time taken by the program.
double
time_taken =
double
(end - start) /
double
(CLOCKS_PER_SEC);
cout <<
"Time taken by program is : "
<< fixed
<< time_taken << setprecision(5);
cout <<
" sec "
<< endl;
return
0;
}
输出:Time taken by program is : 0.000001 sec
- 使用
gettimeofday()
在C++C++中的功能。
gettimeofday(): 函数gettimeofday()可以获取时间和时区。 头文件: “sys/time.h”。 原型/语法: int gettimeofday(结构时间值*tv,结构时区*tz); tv参数是一个struct timeval,它给出了自 纪元 结构时间值{ 时间电视秒秒 suseconds_u t tv_u usec;//微秒 }; 返回值: 成功返回0,失败返回-1。
下面的程序演示了如何使用
gettimeofday()
作用#include <bits/stdc++.h>
#include <sys/time.h>
using
namespace
std;
// A sample function whose time taken to
// be measured
void
fun()
{
for
(
int
i=0; i<10; i++)
{
}
}
int
main()
{
/* The function gettimeofday() can get the time as
well as timezone.
int gettimeofday(struct timeval *tv, struct timezone *tz);
The tv argument is a struct timeval and gives the
number of seconds and micro seconds since the Epoch.
struct timeval {
time_t tv_sec; // seconds
suseconds_t tv_usec; // microseconds
}; */
struct
timeval start, end;
// start timer.
gettimeofday(&start, NULL);
// unsync the I/O of C and C++.
ios_base::sync_with_stdio(
false
);
fun();
// stop timer.
gettimeofday(&end, NULL);
// Calculating total time taken by the program.
double
time_taken;
time_taken = (end.tv_sec - start.tv_sec) * 1e6;
time_taken = (time_taken + (end.tv_usec -
start.tv_usec)) * 1e-6;
cout <<
"Time taken by program is : "
<< fixed
<< time_taken << setprecision(6);
cout <<
" sec"
<< endl;
return
0;
}
输出:Time taken by program is : 0.000029 sec
- 使用
clock_gettime()
在C++C++中的功能。
clock_gettime(): 函数的作用是:获取时钟id指定的时钟的当前时间,并将其放入tp指向的缓冲区。 头文件: “时间,h”。 原型/语法: int clock_gettime(clockid_t clock_id,struct timespec*tp); tp参数指向至少包含以下成员的结构: 结构timespec{ 时间-电视-秒//秒 长电视//纳秒 }; 返回值: 成功返回0,失败返回-1。 时钟id: 时钟id=时钟实时, 时钟\u进程\u CPUTIME\u ID、时钟\u单调…等等。 实时时钟: 测量实际时间的时钟(如挂钟)。 时钟\u进程\u CPU时间\u ID: 来自CPU的高分辨率每进程计时器。 时钟是单调的: 不受系统日期更改(例如NTP守护进程)影响的高分辨率计时器。
下面的程序演示了如何使用
clock_gettime()
作用#include <bits/stdc++.h>
#include <sys/time.h>
using
namespace
std;
// A sample function whose time taken to
// be measured
void
fun()
{
for
(
int
i=0; i<10; i++)
{
}
}
int
main()
{
/* int clock_gettime( clockid_t clock_id, struct
timespec *tp ); The clock_gettime() function gets
the current time of the clock specified by clock_id,
and puts it into the buffer pointed to by tp.tp
parameter points to a structure containing
atleast the following members:
struct timespec {
time_t tv_sec; // seconds
long tv_nsec; // nanoseconds
};
clock id = CLOCK_REALTIME, CLOCK_PROCESS_CPUTIME_ID,
CLOCK_MONOTONIC ...etc
CLOCK_REALTIME : clock that measures real (i.e., wall-clock) time.
CLOCK_PROCESS_CPUTIME_ID : High-resolution per-process timer
from the CPU.
CLOCK_MONOTONIC : High resolution timer that is unaffected
by system date changes (e.g. NTP daemons). */
struct
timespec start, end;
// start timer.
// clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
// clock_gettime(CLOCK_REALTIME, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
// unsync the I/O of C and C++.
ios_base::sync_with_stdio(
false
);
fun();
// stop timer.
// clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
// clock_gettime(CLOCK_REALTIME, &end);
clock_gettime(CLOCK_MONOTONIC, &end);
// Calculating total time taken by the program.
double
time_taken;
time_taken = (end.tv_sec - start.tv_sec) * 1e9;
time_taken = (time_taken + (end.tv_nsec - start.tv_nsec)) * 1e-9;
cout <<
"Time taken by program is : "
<< fixed
<< time_taken << setprecision(9);
cout <<
" sec"
<< endl;
return
0;
}
输出:Time taken by program is : 0.000028 sec
- 使用
chrono::high_resolution_clock
在C++中。
时钟: Chrono库用于处理日期和时间。该库旨在处理不同系统上的计时器和时钟可能不同的事实,从而随着时间的推移提高精度。chrono是头的名称,也是子名称空间的名称,此头中的所有元素都不是直接定义在std名称空间下(与大多数标准库一样),而是定义在std::chrono名称空间下。
下面的程序演示了如何使用
high_resolution_clock
作用有关chrono库的详细信息,请参见 这 和 这#include <bits/stdc++.h>
#include <chrono>
using
namespace
std;
// A sample function whose time taken to
// be measured
void
fun()
{
for
(
int
i=0; i<10; i++)
{
}
}
int
main()
{
auto
start = chrono::high_resolution_clock::now();
// unsync the I/O of C and C++.
ios_base::sync_with_stdio(
false
);
fun();
auto
end = chrono::high_resolution_clock::now();
// Calculating total time taken by the program.
double
time_taken =
chrono::duration_cast<chrono::nanoseconds>(end - start).count();
time_taken *= 1e-9;
cout <<
"Time taken by program is : "
<< fixed
<< time_taken << setprecision(9);
cout <<
" sec"
<< endl;
return
0;
}
输出:Time taken by program is : 0.000024 sec