SoFunction
Updated on 2025-03-02

Summary of related methods for calculating function timestamps on Android programming

This article describes the relevant methods of Android programming to calculate the timestamp of function. Share it for your reference, as follows:

For performance-based people, it is more important to know where the time is spent. You can get the system time before and after the function, and calculate the time stamp to get the time of each function.

existJAVACan be passed()get:

long start_time = ();
(canvas);
long end_time = ();
long spend_time = end_time - start_time;
(TAG,": spend_time = " + spend_time);

existnativeIn the code, the execution time of the function can be obtained by the following method:

#include <>
#include <sys/>
void main ()
{
  struct timeval time;
  gettimeofday(&time, NULL);
  printf ( "\007The current date/time is: %lld\n", time.tv_sec * 1000 + time.tv_usec /1000);
}

existkernelIn it, you can use rtc to correspond to the time of the upper layer application, as in the following example:

#include <linux/>
#include <linux/>
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);

Of course, if you follow the process from Java to native to kernel, you may find that the user space is more time-consuming, but there is no time-consuming in kernel. This is because there is process scheduling. I have encountered such a problem recently. A function of user space takes 30ms, but it does not take time in the kernel, because when returning from kernel to user space, process scheduling is performed, and the thread block of user space at this time will cause such a situation. I hope to pay attention.

Java gets the current time in the year, month, day, hour, minute and second format

import ;
SimpleDateFormat mFormat = new ("yyyy:MM:dd HH:mm:ss:SSS");
String time = (());

Native gets the current time in the year, month, day, hour, minute and second format

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);

Until now, the time and time of Android Java, Native, and Kernel have finally been matched. How important this is for those who do system performance!

PS: This site also provides a Unix timestamp conversion tool, which includes operation methods for timestamps in various common languages, for your reference:

Unix timestamp conversion tool:
http://tools./code/unixtime

For more information about Android related content, please check out the topic of this site:Android date and time operation skills summary》、《Android development introduction and advanced tutorial》、《Android multimedia operation skills summary (audio, video, recording, etc.)》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.