参考:http://blog.csdn.net/oujunli/article/details/17563999
android 执行时间:单位ms
ms为单位
long startTime = System.currentTimeMillis();
long endTime = System.currentTimeMillis();
Log.e("-------------", "初始化数据时间:" + (endTime - startTime) + "ms");
以纳秒为单位
long startTime = System.nanoTime(); // 获取开始时间
// doThing(); // 测试的代码段
long endTime = System.nanoTime(); // 获取结束时间
Log.e("wsy","代码运行时间: " + (endTime - startTime) + "ms");
c++获取执行时间 单位ms
std::chrono::time_point<std::chrono::system_clock> t1 = std::chrono::system_clock::now();
std::chrono::time_point<std::chrono::system_clock> t2 = std::chrono::system_clock::now();
LOGE("cost time:%f ms\n",(float)std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count() / 1000);
#include <stdio.h>
#include <sys/time.h>
void main ()
{
struct timeval time;
gettimeofday(&time, NULL);
printf ( "\007The current date/time is: %lld\n", time.tv_sec * 1000 + time.tv_usec /1000);
}
在kernel里面,可以通过rtc,跟上层应用的时间对应起来,如下面的例子:
#include <linux/time.h>
#include <linux/rtc.h>
struct timespec time_start, time_end;
struct rtc_time tm_start, tm_end;
long time_nsec = 0;
getnstimeofday(&time_start);
rtc_time_to_tm(time_end.tv_sec, &tm_start);
printk(KERN_ERR "\n (%d-%02d-%02d %02d:%02d:%02d.%09lu UTC)\n",
tm_start.tm_year + 1900, tm_start.tm_mon + 1, tm_start.tm_mday,
tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec, time_start.tv_nsec);
.......
getnstimeofday(&time_end);
rtc_time_to_tm(time_end.tv_sec, &tm_end);
time_nsec = time_end.tv_nsec - time_start.tv_nsec;
printk(KERN_ERR "\n tid: %d, common: %s \n", current->pid, current->comm);
printk(KERN_ERR "\n end(%d-%02d-%02d %02d:%02d:%02d.%09lu UTC)\n",
tm_end.tm_year + 1900, tm_end.tm_mon + 1, tm_end.tm_mday,
tm_end.tm_hour, tm_end.tm_min, tm_end.tm_sec, time_end.tv_nsec);
printk(KERN_ERR "\n mdss_fb_commit_wq_handler end, time_nsec : %ld \n" , time_nsec);
2014/03/12更新:
Java得到当前的年月日,时分秒格式的时间
import java.text.SimpleDateFormat;
SimpleDateFormat mFormat = new java.text.SimpleDateFormat("yyyy:MM:dd HH:mm:ss:SSS");
String time = mFormat.format(System.currentTimeMillis());
2014/03/13更新:
Native得到当前的年月日,时分秒格式的时间
timeval tv;
gettimeofday(&tv, NULL);
int milli = tv.tv_usec / 1000;
char buffer [80];
strftime(buffer, 80, "%Y:%m:%d %H:%M:%S", localtime(&tv.tv_sec));
char currentTime[84] = "";
sprintf(currentTime, "%s.%d", buffer, milli);
ALOGD("time: %s \n", currentTime);
到现今为止,终于把Android Java、Native、Kernel的时间时间对应起来了,对做系统性能的人来说,这是多么重要的事情呀!