方法一:
时间函数,单位 毫秒
#include <time.h>
#include <sys/time.h>
#include <iostream>
long get_time()
{
struct timeval t_start;
gettimeofday(&t_start, NULL);
long end = ((long)t_start.tv_sec) * 1000 + (long)t_start.tv_usec / 1000;;
return end;
}
int main()
{
long lStart = get_time();
long lEnd = get_time();
std::cout << "cost time = " << lEnd - lStart << "ms" << std::endl;
}
方法二(我比较喜欢的方法):
auto t_start = std::chrono::high_resolution_clock::now();
hr = Function();
auto t_end = std::chrono::high_resolution_clock::now();
ms = std::chrono::duration<float, std::milli>(t_end - t_start).count();
std::cout << "[ " << " " << " ] " << ms << " ms." << std::endl;
方法三,C#端记录程序运行时间
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
System.Threading.Thread.Sleep(time_cap);
sw.Stop();
//这个是实例运行的毫秒数
//sw.ElapsedMilliseconds
TimeSpan ts2 = sw.Elapsed;
Console.WriteLine("example2 time {0}", ts2.TotalMilliseconds);