c++控制台输出速度fmt库或者printf?

直接上结论:

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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值