我的磁盘遍历算法为什么没有微软的的快,我不明白速度慢在那里?

Siney Siney 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
先看我的整个遍历模块

//遍历磁盘
//Aweay 2003-2-10
#ifndef BROWSEDIR
#define BROWSEDIR
#include <vcl.h>
#include <windows.h>
#include <Classes.hpp>

#define BD_FIND_DIR 0
#define BD_FIND_FILE 1
#define BD_FIND_COMPLETE 2 //查询完毕
#define BD_FIND_ABORT 3 //查询中断

//Error define
#define BD_ERR_OK 0
#define BD_ERR_NOFILTER 1
#define BD_ERR_NOCALLBACK 2
#define BD_ERR_NOTARGET 3

//Thread
#define BD_EXIT_NORMAL 0
#define BD_EXIT_ABORT 1

typedef void __fastcall (__closure *BDCallBack)(String path,int type); //回调函数类型

class TBrowseDir : public TThread //遍历整个磁盘的类
{
protected:
void __fastcall Execute();
private:
TStringList* lstDir; //用于保存临时目录
TStringList* lstFilter; //保存多种查找目标
TStringList* lstTarg;
BDCallBack lpFunc; //当找到目标时将调用这个函数指针指向的函数
String PathString(String pn); //返回带\\结尾的文件夹名称
int SearchDir(String dir);
WIN32_FIND_DATA filedata;
String tmpFn;
int tmpType;
void __fastcall CallBack();
void OnFind(String fn, int n);
int AddSubDir(String strParent);
public:
TBrowseDir(bool CreateSuspended);
__fastcall virtual ~TBrowseDir(void);
void SetCallBack(BDCallBack lpF);
void AddFilter(String strFil);
int Find();
void AddFilte(TStringList* lstF);
void AddTarget(String dir);
};

TBrowseDir::TBrowseDir(bool CreateSuspended)
:TThread(CreateSuspended)
{
//TODO: Add your source code here
lstDir=new TStringList();
lstFilter=new TStringList();
lstTarg=new TStringList();
lpFunc=NULL;
}

__fastcall TBrowseDir::~TBrowseDir()
{
//TODO: Add your source code here
delete lstDir;
delete lstFilter;
delete lstTarg;
}
/*设置回调函数,函数的类型为BDCallBack,原形如下
typedef void __fastcall (__closure *BDCallBack)(String path,int type)
当找到文件或文件夹或查找完毕将调用这个函数。
其中:
path 根据type不同表示找到的文件或文件夹,如果type返回BD_FIND_COMPLETE,则path为空
type 可以取值
BD_FIND_FILE 找到文件
BD_FIND_DIR 找到文件夹
BD_FIND_COMPLETE 查找完毕
*/
void TBrowseDir::SetCallBack(BDCallBack lpF) //设置回调函数
{
//TODO: Add your source code here
lpFunc=lpF;
}

void TBrowseDir::AddFilter(String strFil) //添加一种查找类型,例如*.txt
{
//TODO: Add your source code here
lstFilter->Add(strFil);
}

/*
用于遍历磁盘的主函数
dir表示要遍历的磁盘目录,比如"c:\\"
调用Find函数之前需要调用AddFilter函数和SetCallBack函数
*/
int TBrowseDir::Find() //查找/遍历文件夹
{
//TODO: Add your source code here
if(lstFilter->Count<=0)
return BD_ERR_NOFILTER;
if(lpFunc==NULL)
return BD_ERR_NOCALLBACK;
if(lstTarg->Count<=0)
return BD_ERR_NOTARGET;

memset(&filedata,0,sizeof(filedata));
Resume();
return BD_ERR_OK;
}

String TBrowseDir::PathString(String pn)
{
//TODO: Add your source code here
if(pn.SubString(pn.Length(),1)=="\\")
return pn;
else
return pn+String("\\");
}

int TBrowseDir::SearchDir(String dir)
{
//TODO: Add your source code here
HANDLE re;
for(int n=0;n<lstFilter->Count;n++)
{
String findit=lstFilter->Strings[n];
re=FindFirstFile(findit.c_str(),&filedata);
if(re!=INVALID_HANDLE_VALUE)
{
if(strcmp(filedata.cFileName,"..")!=0
&& strcmp(filedata.cFileName,".")!=0) //如果时文件夹并且不是上一层文件夹和自身
{
if((filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) !=0)
lstDir->Add(dir+String(filedata.cFileName));
else //找到了文件
OnFind(dir+filedata.cFileName,BD_FIND_FILE);
}
while(FindNextFile(re,&filedata))
{
if(Terminated) //处理用户终止
return BD_EXIT_ABORT;

if(strcmp(filedata.cFileName,"..")!=0
&& strcmp(filedata.cFileName,".")!=0)
{
if((filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) !=0)
lstDir->Add(dir+String(filedata.cFileName));
else
OnFind(dir+String(filedata.cFileName),BD_FIND_FILE);
}
}
}
FindClose(re); //释放
}
return BD_EXIT_NORMAL;
}

void __fastcall TBrowseDir::Execute()
{
//---- Place thread code here ----
for(int m=0;m<lstTarg->Count;m++)
{
String strDir=PathString(lstTarg->Strings[m]);
OnFind(strDir,BD_FIND_DIR);
AddSubDir(strDir);//添加子文件夹
SetCurrentDirectory(strDir.c_str());
int re=SearchDir(strDir);
if(re==BD_EXIT_ABORT)
{
OnFind("",BD_FIND_ABORT);
return;
}
int n=lstDir->Count;
while(n>0) //如果n>0证明还有文件夹没有扫描
{
String dn=PathString(lstDir->Strings[0]);
OnFind(dn,BD_FIND_DIR);
re=AddSubDir(dn);
if(re==BD_EXIT_ABORT)
{
OnFind("",BD_FIND_ABORT);
return;
}
SetCurrentDirectory(dn.c_str()); //设置当前工作目录
lstDir->Delete(0);
re=SearchDir(dn);
if(re==BD_EXIT_ABORT)
{
OnFind("",BD_FIND_ABORT);
return;
}
n=lstDir->Count;
}
}
OnFind("",BD_FIND_COMPLETE);
}

void __fastcall TBrowseDir::CallBack() //调用回调函数
{
//TODO: Add your source code here
lpFunc(tmpFn,tmpType);
}

void TBrowseDir::OnFind(String fn, int n)
{
//TODO: Add your source code here
tmpFn=fn;
tmpType=n;
Synchronize(CallBack); //VCL多线程阻塞
}
void TBrowseDir::AddFilte(TStringList* lstF) //重载方法
{
//TODO: Add your source code here
lstFilter->AddStrings(lstF);
}

int TBrowseDir::AddSubDir(String strParent) //添加strParent下的子目录
{
//TODO: Add your source code here
TSearchRec fileinfo;
memset(&fileinfo,0,sizeof(fileinfo));
int re=FindFirst(strParent+"*.*",faDirectory,fileinfo);
if(re==0)
{
if((fileinfo.Attr & faDirectory)!=0
&& fileinfo.Name!="."
&& fileinfo.Name!="..")
lstDir->Add(strParent+fileinfo.Name);
while(FindNext(fileinfo)==0)
{
if(Terminated)
return BD_EXIT_ABORT;
if((fileinfo.Attr & faDirectory)!=0
&& fileinfo.Name!="."
&& fileinfo.Name!="..")
lstDir->Add(strParent+fileinfo.Name);
}
}
FindClose(fileinfo);
return BD_EXIT_NORMAL;
}

void TBrowseDir::AddTarget(String dir)
{
//TODO: Add your source code here
lstTarg->Add(dir);
}
#endif


用我以上的算法遍历一个10g的硬盘(及指定*.*搜索)需要用50秒,而windows自带的查找文件功能查找*.*文件只需要20秒钟的时间???

为了提高速度,我没有采用递归算法,而且尽量使用API函数,可是还是有这么大的差距。

望高手指教。。。
...全文

相关推荐

彩绘兔子迎婴派对邀请卡矢量

彩绘兔子迎婴派对邀请卡矢量适用于迎婴派对邀请卡设计的AI格式素材。

weixin_38638647

通信行业基于MongoDB的安全合规与性能优化:5G时序数据处理、可查询加密及窗口函数应用

内容概要:本文围绕MongoDB在通信行业的深度应用,系统讲解了可查询加密、时间序列集合、窗口函数与性能诊断四大核心技术。通过详实的代码案例,展示了如何利用可查询加密实现敏感数据合规存储与查询,使用时序集合高效处理5G网络KPI等高频写入场景,借助窗口函数进行经营指标的趋势分析,并结合explain()与查询分析器完成性能调优。文章还涵盖分片一致性检查、索引优化等运维实践,全面覆盖通信系统对安全、性能与扩展性的严苛要求。; 适合人群:具备一定数据库基础,从事通信行业研发、架构或运维工作1-3年的技术人员,尤其是参与5G网络监控、客户数据管理或大数据平台建设的相关人员。; 使用场景及目标:① 实现用户手机号、身份证等敏感信息的加密存储与等值查询,满足安全合规要求;② 构建高吞吐、低延迟的5G基站KPI时序数据存储与分析系统;③ 在数据库层直接完成滑动平均、累计求和等经营分析,减少应用层计算负担;④ 诊断慢查询、验证索引有效性,保障系统高性能运行。; 阅读建议:此资源强调理论与实战结合,建议读者在本地或测试环境部署MongoDB 7.0+版本,动手复现文中代码,重点关注schemaMap配置、时序集合参数选择与窗口函数语法细节,并结合explain执行计划理解底层优化机制,从而真正掌握通信级数据架构的设计精髓。

数智顾问

国央企创新负责人如何借助平台实现技术成果的高价值转化与资源精准配置?.docx

科易网基于AI模型+行业大数据,在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,推出科技创新领域的AI+工具与数智化解决方案,推动科技创新与产业创新智能化发展。

BIZKEEN

产业园区运营负责人如何在平台建设中科学配置创新资源,提升园区内技术对接效率?.docx

产业园区运营负责人如何在平台建设中科学配置创新资源,提升园区内技术对接效率?

BIZKEEN

产业园区运营负责人如何提升园区内技术供需匹配的精准度?.docx

科易网基于AI模型+行业大数据,在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,推出科技创新领域的AI+工具与数智化解决方案,推动科技创新与产业创新智能化发展。

BIZKEEN
评论 7
收藏 已收藏
分享
返回顶部
Siney
Windows SDK/API 1,222 成员 8,135 内容