- UID
- 75402
注册时间2014-5-2
阅读权限30
最后登录1970-1-1
龙战于野
TA的每日心情 | 开心 2015-8-2 16:07 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
楼主 |
发表于 2015-4-15 20:55:43
|
显示全部楼层
测了一下,是可以对大数据文件进行操作的,请检查一下用法有没有错。还有我的代码在今天曾经修改过一次,请注意更新。
另外,关于我前面说的效率问题,还是用实际结果来表明吧:
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <time.h>
- #include "Sunday.h"
- using namespace std;
- int memfind(const char *mem, const char *str, int sizem, int sizes);
- int main()
- {
- //char Src[] = "fgjsderiodfda1546egdjkjdkfdadghh";
- //char Sub[] = "fda";
- /*char Src[] = {
- 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0x1C, 0x65, 0x1F, 0x65, 0x1F, 0x65, 0xD1, 0x6D, 0xF1, 0xB6,
- 0x5A, 0xDF, 0x16, 0xD5, 0xF1, 0xA6, 0x5F, 0x4E, 0x91, 0xDF, 0xB6, 0x51, 0xD6, 0xF5, 0x1A, 0x65,
- 0xF1, 0x65, 0xBD, 0xF1, 0x65, 0x1F, 0x65, 0x16, 0xB5, 0x1F, 0xB6, 0x51, 0x65, 0x1E, 0x91, 0xDF,
- 0x65, 0x89, 0x76, 0x51, 0x6F, 0x19, 0x84, 0xDF, 0x65, 0x1B, 0x6D, 0xF5, 0x1B, 0x6F, 0x8B, 0x4A,
- 0xE9, 0x6F, 0x1B, 0x6D, 0xC5, 0xB1, 0x98, 0x76, 0x51, 0xD6, 0x57, 0x49, 0x8E, 0xA5, 0x61, 0xB9,
- 0x81, 0x65, 0x1E, 0x41, 0xB6, 0x5F, 0x1D, 0x65, 0x1D, 0x64, 0x8D, 0x91, 0x65, 0xD1, 0x65, 0x6D
- };
- char Sub[] = {0x65,0x1F, 0x65};*/
- ifstream ifile("F:\\新番\\第十七课.rar", ios::binary);
- ifile.seekg(0, ios::end);
- unsigned int szSrcLen = ifile.tellg();
- ifile.seekg(0, ios::beg);
- char* Src = new char[szSrcLen+1];
- ifile.read(Src, szSrcLen);
- ifile.close();
- char Sub[] = {
- 0x76, 0x41, 0xDB, 0x0C, 0xBF, 0x3C, 0x44, 0xAB
- };
- unsigned int dwTimeNew, dwTimeOld;
- dwTimeNew = clock();
- cout<<FSunday::Sunday(Src, szSrcLen, Sub, sizeof(Sub))<<endl;
- dwTimeOld = clock();
- cout<<"<FSunday::Sunday,耗时"<<dwTimeOld - dwTimeNew<<"ms"<<endl;
- dwTimeNew = clock();
- cout<<FSunday::SundayF(Src, szSrcLen, Sub, sizeof(Sub))<<endl;
- dwTimeOld = clock();
- cout<<"<FSunday::SundayF,耗时"<<dwTimeOld - dwTimeNew<<"ms"<<endl;
- dwTimeNew = clock();
- cout<<FSunday::SundayB(Src, szSrcLen, Sub, sizeof(Sub))<<endl;
- dwTimeOld = clock();
- cout<<"<FSunday::SundayB,耗时"<<dwTimeOld - dwTimeNew<<"ms"<<endl;
- dwTimeNew = clock();
- cout<<FSunday::SundayW(Src, szSrcLen, Sub, sizeof(Sub))<<endl;
- dwTimeOld = clock();
- cout<<"<FSunday::SundayW,耗时"<<dwTimeOld - dwTimeNew<<"ms"<<endl;
- dwTimeNew = clock();
- cout<<FSunday::SundayDW(Src, szSrcLen, Sub, sizeof(Sub))<<endl;
- dwTimeOld = clock();
- cout<<"<FSunday::SundayDW,耗时"<<dwTimeOld - dwTimeNew<<"ms"<<endl;
- dwTimeNew = clock();
- cout<<memfind(Src, Sub, szSrcLen, sizeof(Sub))<<endl;
- dwTimeOld = clock();
- cout<<"memfind,耗时"<<dwTimeOld - dwTimeNew<<"ms"<<endl;
-
- cout << "Hello world!" << endl;
- delete []Src;
-
- system("pause");
- return 0;
- }
- int memfind(const char *mem, const char *str, int sizem, int sizes)
- {
- int da,i,j;
- if (sizes == 0) da = strlen(str);
- else da = sizes;
- for (i = 0; i < sizem; i++)
- {
- for (j = 0; j < da; j ++)
- {
- if (mem[i+j] != str[j])
- break;
- }
- if (j == da)
- return i;
- }
- return -1;
- }
复制代码 测试文件大小124MB,Sub位置112359584(0x6B278A0),应该算是够大的了,测试结果如下:
注意一下,这个文件只有100来MB大小,时间相差近3倍,如果再弄大点的,子字符串再长点的话,时间差将会变得更大。所以,改进这个算法还是有必要的。比如,子字符串扩展到32位的时候结果如下:
时间差在10倍以上!!!其余的就不多解析了
|
|