openAI兴起,于是拿着之前小组合作的项目(承认优化较差),交给AI试着帮忙优化下
一时兴起,做到哪里写到哪里
1.功能函数(TCP_SER_INIT)优化
原代码:
int TCP_SER_INIT(int *tcpsocket, const char *ip, const char *port)
{
int res, optval = 1;
// TCP通信结构体构建
struct sockaddr_in ser = {
.sin_family = AF_INET,
.sin_port = htons((short)atoi(port)),
.sin_addr.s_addr = inet_addr(ip),
};
*tcpsocket = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == *tcpsocket)
{
ERR_MSG("socket");
return -1;
}
// 允许端口快速重复使用
res = setsockopt(*tcpsocket, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
if (-1 == res)
{
ERR_MSG("setsockopt");
return -1;
}
res = bind(*tcpsocket, (struct sockaddr *)&ser, sizeof(ser));
if (-1 == res)
{
ERR_MSG("bind");
return -1;
}
res = listen(*tcpsocket, 128);
if (-1 == res)
{
ERR_MSG("listen");
return -1;
}
printf("等待客户端链接...\n");
return 0;
}
优化后代码:
int TCP_SER_INIT(int *tcpsocket, const char *ip, const char *port)
{
int optval = 1;
// TCP通信结构体构建
struct sockaddr_in ser = {
.sin_family = AF_INET,
.sin_port = htons((short)atoi(port)),
.sin_addr.s_addr = inet_addr(ip),
};
if ((*tcpsocket = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
ERR_MSG("socket");
return -1;
}
if (setsockopt(*tcpsocket, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) == -1)
{
ERR_MSG("setsockopt");
return -1;
}
if (bind(*tcpsocket, (struct sockaddr *)&ser, sizeof(ser)) == -1)
{
ERR_MSG("bind");
return -1;
}
if (listen(*tcpsocket, 128) == -1)
{
ERR_MSG("listen");
return -1;
}
printf("等待客户端链接...\n");
return 0;
}
反思:
优化前后的差别: 优化前,主要出现在每一次操作之后都要判断操作是否成功,返回相应的值; 优化后,通过将操作结果判断和返回值放在操作一起,简化了代码,使之更加简洁易读。
2.手动控制线程代码优化
原代码:
void *pthread_manual(void *arg)
{
while (1)
{
int which;
msg_t *sendmsg = (msg_t *)arg;
if (sendmsg->type & LED1_ON)
{
which = 1;
ioctl(fd_led1, IO_LED_ON, &which);
}
if (sendmsg->type & LED1_OFF)
{
which = 1;
ioctl(fd_led1, IO_LED_OFF, &which);
}
if (sendmsg->type & LED2_ON)
{
which = 2;
ioctl(fd_led2, IO_LED_ON, &which);
}
if (sendmsg->type & LED2_OFF)
{
which = 2;
ioctl(fd_led2, IO_LED_OFF, &which);
}
if (sendmsg->type & LED3_ON)
{
which = 3;
ioctl(fd_led3, IO_LED_ON, &which);
}
if (sendmsg->type & LED3_OFF)
{
which = 3;
ioctl(fd_led3, IO_LED_OFF, &which);
}
if (sendmsg->type & BUZZER_ON)
{
ioctl(fd_buzzer, IO_BUZZER_ON);
}
if (sendmsg->type & BUZZER_OFF)
{
ioctl(fd_buzzer, IO_BUZZER_OFF);
}
if (sendmsg->type & FAN_ON)
{
ioctl(fd_fan, IO_FAN_ON);
}
if (sendmsg->type & FAN_OFF)
{
ioctl(fd_fan, IO_FAN_OFF);
}
if (sendmsg->type & MOTOR_ON)
{
ioctl(fd_motor, IO_MOTOR_ON);
}
if (sendmsg->type & MOTOR_OFF)
{
ioctl(fd_motor, IO_MOTOR_OFF);
}
}
}
优化后代码:
void *pthread_manual(void *arg)
{
while (1)
{
msg_t *sendmsg = (msg_t *)arg;
int device[3] = {1, 2, 3};
if (sendmsg->type & LED1_ON)
{
ioctl(fd_led1, IO_LED_ON, &device[0]);
}
if (sendmsg->type & LED1_OFF)
{
ioctl(fd_led1, IO_LED_OFF, &device[0]);
}
if (sendmsg->type & LED2_ON)
{
ioctl(fd_led2, IO_LED_ON, &device[1]);
}
if (sendmsg->type & LED2_OFF)
{
ioctl(fd_led2, IO_LED_OFF, &device[1]);
}
if (sendmsg->type & LED3_ON)
{
ioctl(fd_led3, IO_LED_ON, &device[2]);
}
if (sendmsg->type & LED3_OFF)
{
ioctl(fd_led3, IO_LED_OFF, &device[2]);
}
if (sendmsg->type & BUZZER_ON)
{
ioctl(fd_buzzer, IO_BUZZER_ON);
}
if (sendmsg->type & BUZZER_OFF)
{
ioctl(fd_buzzer, IO_BUZZER_OFF);
}
if (sendmsg->type & FAN_ON)
{
ioctl(fd_fan, IO_FAN_ON);
}
if (sendmsg->type & FAN_OFF)
{
ioctl(fd_fan, IO_FAN_OFF);
}
if (sendmsg->type & MOTOR_ON)
{
ioctl(fd_motor, IO_MOTOR_ON);
}
if (sendmsg->type & MOTOR_OFF)
{
ioctl(fd_motor, IO_MOTOR_OFF);
}
}
}
反思:
优化前:
上面的代码中,在给LED1,LED2,LED3,Buzzer,FAN,MOTOR的控制语句中,which变量的值都是写死的,如果需要控制的设备添加,这样的写法会比较复杂且容易出错。
优化后:
可以将控制的设备的which值存储在一个数组中,循环处理每个设备,这样只要添加新的设备,只需要在数组中添加新的which值即可,比优化前更加简单,也更容易维护。