C-language function clock, more accurate than the second timer

Source: Internet
Author: User

In the process of using cocos2d-x, want to calculate time difference, such as Touchbegin and touchend click Time difference, need to get the current times, this time is usually used to obtain, return unit is time_t on the iOS platform looked down, That is, long, which is the number of seconds starting from January 1, 1970. But this time is not very accurate, can only be accurate to the second.


If you want to be in milliseconds, or microseconds, there are functions like GetTickCount (Windows platform).

Google a bit, found a C function: clock_t clock (void);

The function returns a clock_t type, which is also a long, and how much time does it know how to represent the return value.

Here is a macro, clocks_per_sec is obviously a second how many clocks meaning, then we have to get the current in seconds to do the time to do the unit, the code is as follows:

float curtimesec = (float) click ()/(float) clocks_per_sec;


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.