直接上结论:
1)打印到屏幕,时间差不错,100次循环,大概需要75毫秒;
2)格式化到字符串,还是snprintf到原始字节数组快,时间
分别是1.22ms
0.25ms
Timer timer;
timer.start();
string str;
for (int i=0; i<100; i++)
{
str = fmt::format("it is a {0}\n", "test");
//str = fmt::format("it is x:{}, y:{}, z:{}", 116.1, 39.101, 500);
fmt::print(fg(fmt::color::yellow), "it is a {0}\n", "test");
fmt::print(fg(fmt::color::yellow), "it is x:{}, y:{}, z:{}\n", 116.1, 39.101, 500);
}
double msecs = timer.stop_delta<Timer::ms>();
cout << msecs << endl;
timer.start();
char buffer[100];
for (int i = 0; i < 100; i++)
{
printf( "it is a %s \n", "test");
printf("it is x:%.03f, y:%.03f, z:%.0f\n", 116.1, 39.101, 500.0);
//snprintf((char *)buffer, 100, "it is a %s \n", "test");
//snprintf((char *)buffer, 100, "it is x:%f, y:%f, z:%f", 116.1, 39.101, 500.0);
//str = buffer;
}
msecs = timer.stop_delta<Timer::ms>();
cout << msecs << endl;