- #include <iostream>
- #ifdef WIN32
- #include <windows.h>
- #else
- #include <sys/time.h>
- #endif
- using std::cout;
- using std::endl;
- long GetMillisecond()
- {
- long lMillisecond = 0L;
- #ifdef WIN32
- SYSTEMTIME st; // System time structure
- GetSystemTime(&st); // Get system time
- lMillisecond = st.wMilliseconds; // Get millisecond
- #else
- struct timeval tv;
- gettimeofday(&tv, NULL); // NULL is only legal value
- lMillisecond = tv.tv_usec / 1000; // Get Millisecond;
- #endif
- return lMillisecond;
- }
- int main()
- {
- int i = 0; // Counter
- cout<<"Get Millisecond Example by JJ."<<endl;
- cout<<"----------------------------"<<endl;
- for (i = 0; i < 20; ++i)
- {
- cout<<GetMillisecond()<<endl;
- Sleep(100);
- }
- cout<<"----------------------------\nDone!"<<endl;
- return 0;
- }
转载于:https://www.cnblogs.com/black/p/5171611.html
本文介绍了一个跨平台的毫秒级计时方法,通过使用C++编程语言,在Windows和Unix-like系统上实现了精确的毫秒级时间获取。代码示例展示了如何根据不同操作系统调用相应的API来获取当前时间的毫秒数。

802

被折叠的 条评论
为什么被折叠?



