我要遍历硬盘扇区呀,但是太慢了

GQHSOFT 2006-10-20 10:23:38
我要遍历硬盘扇区找一个字符串,但是现在太慢了,一个20GB的分区要4个多小时,如何优化呀,我想的方法是做多线程,我有几个问题。就是如果我把硬盘一个扇区的内容作为字符串来比较。如和作才最佳呀。比如这个字符串 “dfasfas dfadsfsdaf dafsfs”中间空的是好多0x00,我怎么快速查找呀。要是把这个字符串赋值到一个CString,会被中断的。我应该如何比较才能不中断那?
...全文
203 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
GQHSOFT 2006-10-20
  • 打赏
  • 举报
回复
谢谢大家呀,我试试呀,要是好用就马上给分
DentistryDoctor 2006-10-20
  • 打赏
  • 举报
回复
多线程反而会影响速度。
应该一次读入一个Track(一般来说是63sectors per track)然后在内存中进行遍历,这样的IO效效率是最高。7200rpm的20G硬盘应该在18~20分钟内可以完成。
可以通过取硬盘的几何信息得到sectors per track是多少。


/* The code of interest is in the subroutine GetDriveGeometry. The
code in main shows how to interpret the results of the IOCTL call. */

#include <windows.h>
#include <winioctl.h>

BOOL GetDriveGeometry(DISK_GEOMETRY *pdg)
{
HANDLE hDevice; // handle to the drive to be examined
BOOL bResult; // results flag
DWORD junk; // discard results

hDevice = CreateFile("\\\\.\\PhysicalDrive0", // drive to open
0, // no access to the drive
FILE_SHARE_READ | // share mode
FILE_SHARE_WRITE,
NULL, // default security attributes
OPEN_EXISTING, // disposition
0, // file attributes
NULL); // do not copy file attributes

if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
{
return (FALSE);
}

bResult = DeviceIoControl(hDevice, // device to be queried
IOCTL_DISK_GET_DRIVE_GEOMETRY, // operation to perform
NULL, 0, // no input buffer
pdg, sizeof(*pdg), // output buffer
&junk, // # bytes returned
(LPOVERLAPPED) NULL); // synchronous I/O

CloseHandle(hDevice);

return (bResult);
}

int main(int argc, char *argv[])
{
DISK_GEOMETRY pdg; // disk drive geometry structure
BOOL bResult; // generic results flag
ULONGLONG DiskSize; // size of the drive, in bytes

bResult = GetDriveGeometry (&pdg);

if (bResult)
{
printf("Cylinders = %I64d\n", pdg.Cylinders);
printf("Tracks per cylinder = %ld\n", (ULONG) pdg.TracksPerCylinder);
printf("Sectors per track = %ld\n", (ULONG) pdg.SectorsPerTrack);
printf("Bytes per sector = %ld\n", (ULONG) pdg.BytesPerSector);

DiskSize = pdg.Cylinders.QuadPart * (ULONG)pdg.TracksPerCylinder *
(ULONG)pdg.SectorsPerTrack * (ULONG)pdg.BytesPerSector;
printf("Disk size = %I64d (Bytes) = %I64d (Mb)\n", DiskSize,
DiskSize / (1024 * 1024));
}
else
{
printf ("GetDriveGeometry failed. Error %ld.\n", GetLastError ());
}

return ((int)bResult);
}
This example does not work on Windows Me/98/95 for the following reasons:


The standard device input/output control codes are not available.
An application must specify a virtual device driver in the CreateFile function—not a specific device.
littleXX 2006-10-20
  • 打赏
  • 举报
回复
多线程确实不会提高什么效率,磁头移动,磁盘I/O时间占了大部分
尽量一次读的扇区大,提高查找效率,从文中来看,字符串搜索时间占用也是非常大的,有测试过80G的硬盘,我连续擦除7次也不会需要4个小时
zhoujie20153 2006-10-20
  • 打赏
  • 举报
回复
学习,
我也想找到一个最佳的办法
王国凡 2006-10-20
  • 打赏
  • 举报
回复
现在硬盘容易巨大, 慢属正常现象, 太快了, 硬盘就冒烟了 :D
baojian88888 2006-10-20
  • 打赏
  • 举报
回复
不用字符串,自己写二进制模式匹配查找函数
用MFC的CString反而会增加内存拷贝,效率更低
另外,硬盘扇区遍历确实很慢的,个人认为多线程不会提高什么效率
yjgx007 2006-10-20
  • 打赏
  • 举报
回复
用Unicode字符串查找!编译Unicode版本!

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧