NDK时间测量
在NDK中测量时间,有四种方法。
LINUX系统方法
gettimeofday
以秒和微秒的形式返回自从Epoch(1970-01-01 00:00:00 +0000 (UTC))时间以来,系统已经经过了多少时间。这个函数会受到系统的时间跳变的影响,比如系统管理员重新设置了系统时间。clock_gettime则不受这个的影响(使用特定的时钟时)。
从POSIX.1-2008开始,这个函数被标记为弃用,并推荐使用clock_gettime
#include <sys/time.h>
//时间通过tv返回
//tz参数已经被废弃,必须设置为NULL
//若获取失败,则函数返回-1,否则函数返回0
int gettimeofday(struct timeval *tv, struct timezone *tz);
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
//使用示例:
struct timeval t1, t2;
gettimeofday(&t1, NULL); //begin
//do something...
gettimeofday(&t2, NULL); //end
int sec = t2.tv_sec - t1.tv_sec; //秒
int usec = t2.tv_usec - t1.tv_usec; //微秒
double ms = sec * 1000.0 + usec / 1000.0; //毫秒
printf("time:%f ms", ms);
clock_gettime
以秒和纳秒的形式返回自从Epoch以来经过了多少时间。
https://linux.die.net/man/2/clock_gettime
#include <time.h>
//时间通过tp返回
//clk_id指定使用的clock id
//若获取成功,函数返回0,失败返回-1
int clock_gettime(clockid_t clk_id, struct timespec *tp);
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
//使用
#include <time.h>
struct timespec time1 = {0, 0};
struct timespec time2 = {0, 0};
clock_gettime(CLOCK_REALTIME, &time1);
//do somethings...
clock_gettime(CLOCK_REALTIME, &time2);
cout << "time passed is: " <<
(time2.tv_sec - time1.tv_sec)*1000 +
(time2.tv_nsec - time1.tv_nsec)/1000000 << "ms" << endl;
各种时钟:clockid_t
- CLOCK_REALTIME:测量系统真实的时间,比如wall-clock,会受系统管理员调整系统时间的影响
- CLOCK_REALTIME_COARSE :CLOCK_REALTIME的快速但低精度版本
- CLOCK_MONOTONIC:从某个不确定的点开始计时的单调递增的时间,比如开机,不受系统管理调整时间等的时间跳变的影响,但是如果adjtime使得时间增长,那么会受影响,也会受NTP的影响。
- CLOCK_MONOTONIC_COARSE:CLOCK_MONOTONIC的快速低精度版本
- CLOCK_MONOTONIC_RAW:与CLOCK_MONOTONIC类似,但是是基于硬件的时间,且不受adjtime和NTP的影响。
- CLOCK_BOOTTIME:和CLOCK_MONOTONIC类似,但是会包括suspended时间,即suspended也在计时。
- CLOCK_PROCESS_CPUTIME_ID:高精度的统计进程的CPU时间。
- CLOCK_THREAD_CPUTIME_ID:统计线程的CPU时间

time
获取自从Eroph以来,以秒为单位的时间。
#include <time.h>
//返回值:以秒为单位的时间
//t:若非NULL,则与返回值同义
time_t time(time_t *t);
语言提供
clock
C和C++都提供这个函数
//C
//Defined in header <time.h>
clock_t clock(void);
//C++
//Defined in header <ctime>
std::clock_t clock();
这个函数返回程序一个大概的CPU耗时,至于起点则不一定和程序的开始点一致,和具体的实现相关。所以,这个函数单一的调用返回值没有意义,只有两次调用之间的差值才有意义。
这个函数的计时可能比wall clock快或者慢。当本程序与另外的程序共享CPU时,那么就会比wall clock慢;当本程序在多线程执行时,那么就会比wall clock快,因为,比如每一秒内,本程序实际上耗费了多个(多线程)一秒的CPU时间.
//c++版本
#include <iostream>
#include <iomanip>
#include <chrono>
#include <ctime>
#include <thread>
// the function f() does some time-consuming work
void f()
{
volatile double d = 0;
for(int n=0; n<10000; ++n)
for(int m=0; m<10000; ++m)
d += d*n*m;
}
int main()
{
std::clock_t c_start = std::clock();
auto t_start = std::chrono::high_resolution_clock::now();
std::thread t1(f);
std::thread t2(f); // f() is called on two threads
t1.join();
t2.join();
std::clock_t c_end = std::clock();
auto t_end = std::chrono::high_resolution_clock::now();
std::cout << std::fixed << std::setprecision(2) << "CPU time used: "
<< 1000.0 * (c_end-c_start) / CLOCKS_PER_SEC << " ms\n"
<< "Wall clock time passed: "
<< std::chrono::duration<double, std::milli>(t_end-t_start).count()
<< " ms\n";
}
//输出:
CPU time used: 1590.00 ms //因为本程序多线程,所以比wall clock快
Wall clock time passed: 808.23 ms
std::chrono::steady_clock
返回一个单调递增的时间,和CLOCK_MONOTONIC类似,最适合用来测量时间间隔
#include <iostream>
#include <vector>
#include <numeric>
#include <chrono>
volatile int sink;
int main()
{
for (auto size = 1ull; size < 1000000000ull; size *= 100) {
// record start time
auto start = std::chrono::steady_clock::now();
// do some work
std::vector<int> v(size, 42);
sink = std::accumulate(v.begin(), v.end(), 0u); // make sure it's a side effect
// record end time
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> diff = end-start;
std::cout << "Time to fill and iterate a vector of "
<< size << " ints : " << diff.count() << " s\n";
}
}
//out:
Time to fill and iterate a vector of 1 ints : 2.43e-07 s
Time to fill and iterate a vector of 100 ints : 4.1e-07 s
Time to fill and iterate a vector of 10000 ints : 2.519e-05 s
Time to fill and iterate a vector of 1000000 ints : 0.00207669 s
Time to fill and iterate a vector of 100000000 ints : 0.423087 s
//简单使用示例:
#include <chrono>
#include <ctime> //CLOCKS_PER_SEC定义
auto start = std::chrono::steady_clock::now();
auto end = std::chrono::steady_clock::now();
printf("time: %f ms", 1000.0 * (c_end-c_start) / CLOCKS_PER_SEC); //打印毫秒
std::chrono::high_resolution_clock::now
只有C++有,并且从C++11开始. 使用见上例
#include <chrono>
static std::chrono::time_point<std::chrono::high_resolution_clock> now() noexcept;
//since C++11
std::chrono::system_clock
获取系统时间。
打印时间 std::ctime
#include <ctime>
#include <iostream>
int main()
{
std::time_t result = std::time(nullptr);
std::cout << std::ctime(&result);
}
//Output:
Tue Dec 27 17:21:29 2011
NDK时间测量的更多相关文章
- 玩转X-CTR100 l STM32F4 l 定时器时间测量
我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ] 本文介绍X-CTR100控制器 使用处理器内部硬件定 ...
- python时间测量
使用自定义装饰器测量时间 def test_time(func): def inner(*args, **kw): t1 = datetime.datetime.now() print('开始时间:' ...
- 对java程序员来说时间格式永远让人挠头来看Java Date Time 教程-时间测量
在Java中,用System.currentTimeMillis()来测量时间最方便. 你要做的是在某些操作之前获取到时间,然后在这些操作之后你想要测量时间,算出时间差.下面是一个例子: long s ...
- 【转载】Linux 进程调度时间测量
测试Context Switch time(进程上下文切换时间) -------------------------------------------------- 创建两个进程(实时进程) ...
- 【安富莱专题教程第7期】终极调试组件Event Recorder,各种Link通吃,支持时间和功耗测量,printf打印,RTX5及中间件调试
说明:1.继前面的专题教程推出SEGGER的RTT,JScope,Micrium的uC/Probe之后,再出一期终极调试方案Event Recoder,之所以叫终极解决方案,是因为所有Link通吃. ...
- iOS 时间的处理
做App避免不了要和时间打交道,关于时间的处理,里面有不少门道,远不是一行API调用,获取当前系统时间这么简单.我们需要了解与时间相关的各种API之间的差别,再因场景而异去设计相应的机制. 时间的形式 ...
- Boost学习笔记(二) 时间与日期
timer库概述 timer库包含三个组件:分别是计时器类timer.progress_timer和进度指示类progress_display timer 主要作用是计时,精确度是毫秒级.下面是一个简 ...
- 真实世界:使用WCF扩展记录服务调用时间
WCF 可扩展性 WCF 提供了许多扩展点供开发人员自定义运行时行为. WCF 在 Channel Layer 之上还提供了一个高级运行时,主要是针对应用程序开发人员.在 WCF 文档中,它常被称为服 ...
- (笔记)Linux内核学习(八)之定时器和时间管理
一 内核中的时间观念 内核在硬件的帮助下计算和管理时间.硬件为内核提供一个系统定时器用以计算流逝的时间.系 统定时器以某种频率自行触发,产生时钟中断,进入内核时钟中断处理程序中进行处理. 墙上时间和系 ...
随机推荐
- springboot集成quartz定时任务课动态执行
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</ ...
- Spring autowire自动装配 ByType和ByName
不使用自动装配前使用的是类的引用: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=& ...
- HDFS第一次课堂测试
起初在linux上想使用Map/Reduce来完成操作,发现需要导入的jar包过多,大概在6点左右写完程序却跑不起来,一直在找jar包,直接被卡死在这里. 从教室回来之后,发现好多人都是在window ...
- 浅谈HTTP Keep-Alive
背景是一次线上故障 项目类型vue ssr 与server的数据交互用的http内网域名方案 在5月发生了一次线上CPU100%的问题,直接导致了NodeServer 500. 最终解决办法是: 1. ...
- Python的 is 和 == 弄懂了吗?
在Python中一切都是对象. Python中对象包含的三个基本要素,分别是: id(身份标识) type(数据类型) value(值) 对象之间比较是否相等可以用 == ,也可以用 is . is ...
- MapReduce实现ReduceSideJoin操作
本文转载于:http://blog.csdn.net/xyilu/article/details/8996204 一.准备两张表以及对应的数据 (1)m_ys_lab_jointest_a(以下简称表 ...
- ionic3项目 出现 No provider for ApplicationInitStatus!
出现No provider for ApplicationInitStatus!原因是在app.module.ts文件忘记导入BrowserModule导致的,打开app.module.ts文件添加以 ...
- [Web安全之实战] 跨站脚本攻击XSS
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章Points: 1. 认识XSS 2. ...
- Long类型时间如何转换成视频时长?
数据库中存放的视频时长是一个Long类型的毫秒/秒时间,现在需要把这个时间转换成标准的视频时长格式,在我看来这应该是一个很常用的转化有一个很常用的转换方法工具才对,可是我百度找了许久,没有一个简单直观 ...
- Ubuntu16.04环境下搭建基于三台主机的mysql galera cluster集群(实测有效)
(注意: (1)文中红色字体部分不一定需要操作 (2)由于word文档编辑的原因,实际操作时部分命令需要手动输入!!直接复制粘贴会提示错误!! ) 一 搭建环境: 1 Ubuntu16.04版本(系 ...