目录
简述
1. 通过写注释来实现接口补全
1.1 示例:编写接口注释
1.2 DeepSeek自动补全
1.3 验证结果(可行)
2. 通过注释优化代码
2.1 提示词
2.2 优化之前
2.3 DeepSeek优化后
2.4 代码解释
2.5 验证(差强人意)
2.6 进一步优化
2.7 优化方案(分优先级实现)
2.7.1 分批次更新 + 事件循环释放
2.7.2 定时器合并更新请求
3. 总结
在软件开发的过程中,自动化编程可以显著提高开发效率、减少重复性劳动,并且降低错误发生的概率。DeepSeek作为一款强大的智能助手,能够根据自然语言描述生成高质量的代码。今天,我们将通过几个实例,展示如何利用DeepSeek进行自动化编程,尤其是在C++开发中如何利用它进行接口补全、类功能补全,以及项目架构设计。
这里主要是介绍接口的补全与优化。
接口是程序中的核心部分。通常,我们需要为接口函数编写详细的注释,以便其他开发人员理解其用途和功能。DeepSeek能够通过接口的注释自动补全相关代码。
我需要一个遍历目录下所有文件,并展示到控件中的接口。
我们需要完成的部分:
//要求:在Qt Widgets环境下,实现以下接口
//接口描述:遍历目录下所有的文件,不包括隐藏文件。
//输入:参数 dir 表示目录
//输出:将完整的文件路径显示到 ui->textBrowser控件(QTextBrowser)中
void MainWindow::listAllFiles(QString dir)
{
}
提示词要点:说明我的需求,接口描述、输入参数、输出形态。
将上述注释和接口名称复制到DeepSeek中,DeepSeek会根据注释生成完整的接口代码实现。
这一次思考过程有点漫长:
代码实现:
#include
#include
void MainWindow::listAllFiles(QString dir)
{
QDir directory(dir);
if (!directory.exists()) {
qDebug() << "Directory does not exist:" << dir;
return;
}
QDirIterator it(dir, QDir::Files | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
while (it.hasNext()) {
QString filePath = it.next();
ui->textBrowser->append(filePath);
}
}
DeepSeek根据接口的注释自动生成了遍历目录下所有文件的接口。
刚才生成的接口在一般情况下可用,但是当输入参数为空 或者 目录下文件数量非常多时,会引起UI卡顿,可以通过DeepSeek进一步优化。
优化以下代码。1. dir为空时会卡死,2. 当文件数多时,会引起UI卡顿。
将 注释 和 代码部分 复制到DeepSeek中:
//要求:优化以下代码。1. dir为空时会卡死,2. 当文件数多时,会引起UI卡顿。
void MainWindow::listAllFiles(QString dir)
{
QDir directory(dir);
if (!directory.exists()) {
qDebug() << "Directory does not exist:" << dir;
return;
}
QDirIterator it(dir, QDir::Files | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
while (it.hasNext()) {
QString filePath = it.next();
ui->textBrowser->append(filePath);
}
}
DeepSeek优化策略:
#ifndef FILELISTER_H
#define FILELISTER_H
#include
#include
#include
#include
#include
#include
// 工作线程类
class FileLister : public QObject
{
Q_OBJECT
public:
explicit FileLister(QString dir, QObject *parent = nullptr) : QObject(parent), m_dir(dir) {}
signals:
// 信号:用于将文件路径列表发送到主线程
void filePathsReady(QStringList paths);
public slots:
// 槽函数:执行文件列举任务
void listFiles() {
if (m_dir.isEmpty()) {
qDebug() << "Directory is empty.";
emit filePathsReady(QStringList());
return;
}
QDir directory(m_dir);
if (!directory.exists()) {
qDebug() << "Directory does not exist:" << m_dir;
emit filePathsReady(QStringList());
return;
}
QStringList filePaths;
QDirIterator it(m_dir, QDir::Files | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
while (it.hasNext()) {
filePaths.append(it.next());
}
// 发送文件路径列表到主线程
emit filePathsReady(filePaths);
}
private:
QString m_dir;
};
#endif // FILELISTER_H
应用部分:
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "filelister.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::listAllFiles(QString dir)
{
// 创建工作线程
QThread* thread = new QThread;
FileLister* fileLister = new FileLister(dir);
fileLister->moveToThread(thread);
// 连接信号和槽
connect(thread, &QThread::started, fileLister, &FileLister::listFiles);
connect(fileLister, &FileLister::filePathsReady, this, &MainWindow::handleFilePaths);
connect(fileLister, &FileLister::filePathsReady, thread, &QThread::quit);
connect(fileLister, &FileLister::filePathsReady, fileLister, &FileLister::deleteLater);
connect(thread, &QThread::finished, thread, &QThread::deleteLater);
// 启动线程
thread->start();
}
void MainWindow::handleFilePaths(const QStringList& paths)
{
// 批量更新 UI
for (const QString& path : paths) {
ui->textBrowser->append(path);
}
}
void MainWindow::on_pushButton_clicked()
{
QString dir = ui->lineEdit->text();
if (!dir.isEmpty()) {
listAllFiles(dir);
}
}
FileLister 类:
MainWindow::listAllFiles 函数:
MainWindow::handleFilePaths 函数:
当需要列出的文件数量非常多时,UI还是会卡顿。这暴露了一些问题,DeepSeek推理模型为了达到大致上的效果,有些地方会存在欺骗的情况。(也有可能是我的提示词不够细致,没关系,可以进一步优化)
提示词:
刚才实现的代码有一点问题,结果更新频繁的时候,ui->textBrowser->append(path); 这里会卡住主界面
DeepSeek给出的原因分析:
针对 UI 更新频繁导致主界面卡顿的问题,结合历史代码和搜索结果中的优化思路,提供以下解决方案:
简单有效,通过分段释放事件循环避免长时间阻塞。(推荐这种处理方式)
void MainWindow::handleFilePaths(const QStringList& paths)
{
const int batchSize = 100; // 每批处理100条
for (int i = 0; i < paths.size(); ++i) {
ui->textBrowser->append(paths[i]);
// 每处理100条释放事件循环
if (i % batchSize == 0) {
QCoreApplication::processEvents();
}
}
}
// 在 MainWindow 类中添加
private:
QStringList m_pendingPaths;
QTimer m_updateTimer;
// 初始化时连接
MainWindow::MainWindow(...)
{
connect(&m_updateTimer, &QTimer::timeout, this, [this]() {
if (!m_pendingPaths.isEmpty()) {
ui->textBrowser->append(m_pendingPaths.join("\n"));
m_pendingPaths.clear();
}
});
m_updateTimer.start(200); // 200ms触发一次
}
void MainWindow::handleFilePaths(const QStringList& paths)
{
m_pendingPaths.append(paths);
}
通过DeepSeek,我们可以极大地提高编程效率,尤其是在C++开发中,它能够根据注释自动补全接口、以及优化代码。借助AI的力量,我们可以更快速地构建高质量的代码,专注于更具创意和挑战性的任务。