C/C ++ obtains the current system time in milliseconds.

Source: Internet
Author: User
Tags time in milliseconds

Previous code to get the current system time.

You can obtain the system time by checking the time () on the network. If you find any problem, you can only obtain the system time in seconds.

Then I found the following article:

========================================================== ========================================================== ============================================

From http://blog.csdn.net/jefry_xdz/article/details/8072097

 

I am working on the underlying Android ndk test code. In many cases, I need to obtain the current system's Millisecond Time to accurately analyze the data. The following code is a test on the Android system, therefore, this function code is feasible in the Linux system. I did not test it as to whether VC is feasible or not (it should not work), because sleep in VC and Linux C is quite different. I will share the current system time in milliseconds for C/C ++.

  • It is easy to get the current system in milliseconds using Java:
[Java]
View plaincopyprint?
  1. Public Static VoidMain (string [] ARGs ){
  2. System. Out. println ("Java program:" + system. currenttimemillis ());
  3. }
public static void main(String[] args){      System.out.println("java program :" + System.currentTimeMillis());   }
  • C/C ++ is a little more complex. It requires the help of timeval. For the detailed description of timeval, you only need to Google it to understand it. I will not introduce it any more:
[CPP]
View plaincopyprint?
  1. # Include <stdio. h>
  2. # Include <sys/time. h>
  3. LongGetcurrenttime ()
  4. {
  5. StructTimeval TV;
  6. Gettimeofday (& TV, null );
  7. ReturnTV. TV _sec * 1000 + TV. TV _usec/1000;
  8. }
  9. IntMain ()
  10. {
  11. Printf ("C/C ++ program: % LD \ n", getcurrenttime ());
  12. Return0;
  13. }
#include <stdio.h>  #include <sys/time.h>    long getCurrentTime()  {     struct timeval tv;     gettimeofday(&tv,NULL);     return tv.tv_sec * 1000 + tv.tv_usec / 1000;  }    int main()  {      printf("c/c++ program:%ld\n",getCurrentTime());      return 0;  }

 

Test results:

C/C ++ program: 1350354127212

Java program: 1350354129299

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.