float转字符串使用sprintf太慢了,自己实现sprintf功能,用于把三个float转换成字符串格式,速度比sprintf快了20倍!运行结果如下图:
例程:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define RUN_COUNT 10000000 // 运行次数
/*实现sprintf功能,用于把三个float转换成字符串格式*/
/********************************************
* @brief 读现sprintf功能,用于把三个float转换成字符串格式*
* @param [in] num1 浮点数 num2 浮点数,num3 浮点数, [out] buff:字符串
* @return 字符串长度
* @Author lzb
* @Time 2024/07/16
*********************************************/
int my_sprintf(float num1, float num2, float num3, int num4, char buff[]);
int main()
{
printf("\n");
clock_t start, end;
double cpu_time_used;
float outData[10] = {-0.999, -0.164, 2.499};
char date[128] = {0};
int i = 0;
// 记录开始时间
start = clock();
memset(date, 0, sizeof(date));
for(i=0; i<RUN_COUNT; i++)
{
my_sprintf(outData[0], outData[1], outData[2],0, date);
}
// 记录结束时间
end = clock();
// 计算并打印运行时间
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("my_sprintf运行%d次使用时间: %f 秒\n", RUN_COUNT, cpu_time_used);
printf("date1: %s", date);
printf("\n\n");
// 记录开始时间
start = clock();
memset(date, 0, sizeof(date));
for(i=0; i<RUN_COUNT; i++)
{
sprintf(date, "%.3f %.3f %.3f\n", outData[0], outData[1], outData[2]);
}
// 记录结束时间
end = clock();
// 计算并打印运行时间
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("sprintf运行%d次使用时间: %f 秒\n", RUN_COUNT, cpu_time_used);
printf("date2: %s", date);
return 0;
}
int my_sprintf(float num1, float num2, float num3, int num4, char buff[])
{
short buffLen = 0;
int integerNum[3], DecimalNum[3];
integerNum[0] = num1, integerNum[1] = num2, integerNum[2] = num3;
DecimalNum[0] = ((float)((num1 - integerNum[0]) * 1000));
DecimalNum[1] = ((float)((num2 - integerNum[1]) * 1000));
DecimalNum[2] = ((float)((num3 - integerNum[2]) * 1000));
// printf("\r\ndata: %0.3f %0.3f %0.3f\n", num1, num2, num3);
// printf("integerNum: %d %d %d\n", integerNum[0], integerNum[1], integerNum[2]);
// printf("DecimalNum: %d %d %d\n", DecimalNum[0], DecimalNum[1], DecimalNum[2]);
if(integerNum[0] < 0 || DecimalNum[0]<0){
integerNum[0] *= -1;
DecimalNum[0] *= -1;
buff[buffLen++] = '-';
}
if(integerNum[0] >= 100){ // 3 位
buff[buffLen++] = integerNum[0] / 100 + '0';
buff[buffLen++] = (integerNum[0] % 100) / 10 + '0';
buff[buffLen++] = integerNum[0] % 10 + '0';
buff[buffLen++] = '.';
buff[buffLen++] = DecimalNum[0] / 100 + '0';
buff[buffLen++] = (DecimalNum[0] % 100) / 10 + '0';
buff[buffLen++] = DecimalNum[0] % 10 + '0';
}else if(integerNum[0] >= 10){ // 2 位
buff[buffLen++] = (integerNum[0] % 100) / 10 + '0';
buff[buffLen++] = integerNum[0] % 10 + '0';
buff[buffLen++] = '.';
buff[buffLen++] = DecimalNum[0] / 100 + '0';
buff[buffLen++] = (DecimalNum[0] % 100) / 10 + '0';
buff[buffLen++] = DecimalNum[0] % 10 + '0';
}else{
buff[buffLen++] = integerNum[0] % 10 + '0';
buff[buffLen++] = '.';
buff[buffLen++] = DecimalNum[0] / 100 + '0';
buff[buffLen++] = (DecimalNum[0] % 100) / 10 + '0';
buff[buffLen++] = DecimalNum[0] % 10 + '0';
}
buff[buffLen++] = ' ';
if(integerNum[1] < 0 || DecimalNum[1]<0){
integerNum[1] *= -1;
DecimalNum[1] *= -1;
buff[buffLen++] = '-';
}
if(integerNum[1] >= 100){ // 3 位
buff[buffLen++] = integerNum[1] / 100 + '0';
buff[buffLen++] = (integerNum[1] % 100) / 10 + '0';
buff[buffLen++] = integerNum[1] % 10 + '0';
buff[buffLen++] = '.';
buff[buffLen++] = DecimalNum[1] / 100 + '0';
buff[buffLen++] = (DecimalNum[1] % 100) / 10 + '0';
buff[buffLen++] = DecimalNum[1] % 10 + '0';
}else if(integerNum[1] >= 10){ // 2 位
buff[buffLen++] = (integerNum[1] % 100) / 10 + '0';
buff[buffLen++] = integerNum[1] % 10 + '0';
buff[buffLen++] = '.';
buff[buffLen++] = DecimalNum[1] / 100 + '0';
buff[buffLen++] = (DecimalNum[1] % 100) / 10 + '0';
buff[buffLen++] = DecimalNum[1] % 10 + '0';
}else{
buff[buffLen++] = integerNum[1] % 10 + '0';
buff[buffLen++] = '.';
buff[buffLen++] = DecimalNum[1] / 100 + '0';
buff[buffLen++] = (DecimalNum[1] % 100) / 10 + '0';
buff[buffLen++] = DecimalNum[1] % 10 + '0';
}
buff[buffLen++] = ' ';
if(integerNum[2] < 0 || DecimalNum[2]<0){
integerNum[2] *= -1;
DecimalNum[2] *= -1;
buff[buffLen++] = '-';
}
if(integerNum[2] >= 100){ // 3 位
buff[buffLen++] = integerNum[2] / 100 + '0';
buff[buffLen++] = (integerNum[2] % 100) / 10 + '0';
buff[buffLen++] = integerNum[2] % 10 + '0';
buff[buffLen++] = '.';
buff[buffLen++] = DecimalNum[2] / 100 + '0';
buff[buffLen++] = (DecimalNum[2] % 100) / 10 + '0';
buff[buffLen++] = DecimalNum[2] % 10 + '0';
}else if(integerNum[2] >= 10){ // 2 位
buff[buffLen++] = (integerNum[2] % 100) / 10 + '0';
buff[buffLen++] = integerNum[2] % 10 + '0';
buff[buffLen++] = '.';
buff[buffLen++] = DecimalNum[2] / 100 + '0';
buff[buffLen++] = (DecimalNum[2] % 100) / 10 + '0';
buff[buffLen++] = DecimalNum[2] % 10 + '0';
}else{
buff[buffLen++] = integerNum[2] % 10 + '0';
buff[buffLen++] = '.';
buff[buffLen++] = DecimalNum[2] / 100 + '0';
buff[buffLen++] = (DecimalNum[2] % 100) / 10 + '0';
buff[buffLen++] = DecimalNum[2] % 10 + '0';
}
if(num4 == 0){
buff[buffLen++] = '\n';
return buffLen;
}
buff[buffLen++] = ' ';
if(num4 < 0){
num4 *= -1;
buff[buffLen++] = '-';
}
if(num4 >= 10000){ // 5 位
buff[buffLen++] = num4 / 10000 + '0';
buff[buffLen++] = (num4 % 10000) / 1000 + '0';
buff[buffLen++] = (num4 % 1000) / 100 + '0';
buff[buffLen++] = (num4 % 100) / 10 + '0';
buff[buffLen++] = num4 % 10 + '0';
}else if(num4 >= 1000){ // 4 位
buff[buffLen++] = num4 / 1000 + '0';
buff[buffLen++] = (num4 % 1000) / 100 + '0';
buff[buffLen++] = (num4 % 100) / 10 + '0';
buff[buffLen++] = num4 % 10 + '0';
}else if(num4 >= 100){ // 3 位
buff[buffLen++] = num4 / 100 + '0';
buff[buffLen++] = (num4 % 100) / 10 + '0';
buff[buffLen++] = num4 % 10 + '0';
}else if(num4 >= 10){ // 2 位
buff[buffLen++] = num4 / 10 + '0';
buff[buffLen++] = num4 % 10 + '0';
}else{ // 1 位
buff[buffLen++] = num4 % 10 + '0';
}
buff[buffLen++] = '\n';
return buffLen;
}
标签:10,20,++,float,DecimalNum,sprintf,100,buffLen,buff
From: https://blog.csdn.net/ZakaryBin/article/details/140469027