版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、#include <iostream.h>#include <windows.h>/三種模式void filter_nobuffer(char* source,char* sink,void (*func)(char* addr);void filter_sequen(char* source,char* sink,void (*func)(char* addr);void filter_overlp(char* source,char* sink,void (*func)(char* addr);/五個不同功能的操作void f1(char* addr);void f
2、2(char* addr);void f3(char* addr);void f4(char* addr);void f5(char* addr);#define BUFFER_SIZE 1024 /定義緩沖區(qū)的大小,這里設(shè)為字節(jié)char * buffer; /這里的緩沖區(qū)被定義成char型void main() /分配緩沖區(qū) buffer = new charBUFFER_SIZE; /用于記錄執(zhí)行filter函數(shù)的開始時間 DWORD tick; /用于求三種模式各自的平均用時 DWORD nobuffer_average_time=0; DWORD sequen_average_time
3、=0; DWORD overlp_average_time=0; /采用無緩存模式調(diào)用filter函數(shù)10次 cout<<"無文件高速緩存模式正在運行"<<endl; DWORD nobuffer_start_time=GetTickCount(); tick = nobuffer_start_time; filter_nobuffer("source.txt","nobuffer_1.txt",f1); cout<<"nobuffer 0-1: "<<GetTick
4、Count() - tick<<" ms."<<endl; tick = GetTickCount(); filter_nobuffer("nobuffer_1.txt","nobuffer_2.txt",f2); cout<<"nobuffer 1-2: "<<GetTickCount() - tick<<" ms."<<endl; tick = GetTickCount(); filter_nobuffer("
5、nobuffer_2.txt","nobuffer_3.txt",f3); cout<<"nobuffer 2-3: "<<GetTickCount() - tick<<" ms."<<endl; tick = GetTickCount(); filter_nobuffer("nobuffer_3.txt","nobuffer_4.txt",f4); cout<<"nobuffer 3-4: "<&l
6、t;GetTickCount() - tick<<" ms."<<endl; tick = GetTickCount(); filter_nobuffer("nobuffer_4.txt","nobuffer_5.txt",f5); cout<<"nobuffer 4-5: "<<GetTickCount() - tick<<" ms."<<endl; tick = GetTickCount(); filter_nobuff
7、er("nobuffer_5.txt","nobuffer_6.txt",f1); cout<<"nobuffer 5-6: "<<GetTickCount() - tick<<" ms."<<endl; tick = GetTickCount(); filter_nobuffer("nobuffer_6.txt","nobuffer_7.txt",f2); cout<<"nobuffer 6-7: &qu
8、ot;<<GetTickCount() - tick<<" ms."<<endl; tick = GetTickCount(); filter_nobuffer("nobuffer_7.txt","nobuffer_8.txt",f3); cout<<"nobuffer 7-8: "<<GetTickCount() - tick<<" ms."<<endl; tick = GetTickCount(); filt
9、er_nobuffer("nobuffer_8.txt","nobuffer_9.txt",f4); cout<<"nobuffer 8-9: "<<GetTickCount() - tick<<" ms."<<endl; tick = GetTickCount(); filter_nobuffer("nobuffer_9.txt","nobuffer_10.txt",f5); DWORD nobuffer_end_time=G
10、etTickCount(); cout<<"nobuffer 9-10: "<<nobuffer_end_time - tick<<" ms."<<endl<<endl; /采用高速緩存模式調(diào)用filter函數(shù)10次 cout<<"使用文件高速緩存模式正在運行"<<endl; DWORD sequen_start_time=GetTickCount(); tick = sequen_start_time; filter_sequen("sou
11、rce.txt","sequen_1.txt",f1); cout<<"sequen 0-1: "<<GetTickCount() - tick<<" ms."<<endl; tick = GetTickCount(); filter_sequen("sequen_1.txt","sequen_2.txt",f2); cout<<"sequen 1-2: "<<GetTickCount() -
12、tick<<" ms."<<endl; tick = GetTickCount(); filter_sequen("sequen_2.txt","sequen_3.txt",f3); cout<<"sequen 2-3: "<<GetTickCount() - tick<<" ms."<<endl; tick = GetTickCount(); filter_sequen("sequen_3.txt",&
13、quot;sequen_4.txt",f4); cout<<"sequen 3-4: "<<GetTickCount() - tick<<" ms."<<endl; tick = GetTickCount(); filter_sequen("sequen_4.txt","sequen_5.txt",f5); cout<<"sequen 4-5: "<<GetTickCount() - tick<<&qu
14、ot; ms."<<endl; tick = GetTickCount(); filter_sequen("sequen_5.txt","sequen_6.txt",f1); cout<<"sequen 5-6: "<<GetTickCount() - tick<<" ms."<<endl; tick = GetTickCount(); filter_sequen("sequen_6.txt","sequen_7.t
15、xt",f2); cout<<"sequen 6-7: "<<GetTickCount() - tick<<" ms."<<endl; tick = GetTickCount(); filter_sequen("sequen_7.txt","sequen_8.txt",f3); cout<<"sequen 7-8: "<<GetTickCount() - tick<<" ms."&l
16、t;<endl; tick = GetTickCount(); filter_sequen("sequen_8.txt","sequen_9.txt",f4); cout<<"sequen 8-9: "<<GetTickCount() - tick<<" ms."<<endl; tick = GetTickCount(); filter_sequen("sequen_9.txt","sequen_10.txt",f5);
17、DWORD sequen_end_time=GetTickCount(); cout<<"sequen 9-10: "<<sequen_end_time - tick<<" ms."<<endl<<endl; /采用異步模式調(diào)用filter函數(shù)10次 cout<<"異步傳輸模式正在運行"<<endl; DWORD overlp_start_time=GetTickCount(); tick = overlp_start_time; filter_ove
18、rlp("source.txt","overlp_1.txt",f1); cout<<"overlp 0-1: "<<GetTickCount() - tick<<" ms."<<endl; tick = GetTickCount(); filter_overlp("overlp_1.txt","overlp_2.txt",f2); cout<<"overlp 1-2: "<<GetT
19、ickCount() - tick<<" ms."<<endl; tick = GetTickCount(); filter_overlp("overlp_2.txt","overlp_3.txt",f3); cout<<"overlp 2-3: "<<GetTickCount() - tick<<" ms."<<endl; tick = GetTickCount(); filter_overlp("overlp_
20、3.txt","overlp_4.txt",f4); cout<<"overlp 3-4: "<<GetTickCount() - tick<<" ms."<<endl; tick = GetTickCount(); filter_overlp("overlp_4.txt","overlp_5.txt",f5); cout<<"overlp 4-5: "<<GetTickCount() - ti
21、ck<<" ms."<<endl; tick = GetTickCount(); filter_overlp("overlp_5.txt","overlp_6.txt",f1); cout<<"overlp 5-6: "<<GetTickCount() - tick<<" ms."<<endl; tick = GetTickCount(); filter_overlp("overlp_6.txt",&qu
22、ot;overlp_7.txt",f2); cout<<"overlp 6-7: "<<GetTickCount() - tick<<" ms."<<endl; tick = GetTickCount(); filter_overlp("overlp_7.txt","overlp_8.txt",f3); cout<<"overlp 7-8: "<<GetTickCount() - tick<<"
23、; ms."<<endl; tick = GetTickCount(); filter_overlp("overlp_8.txt","overlp_9.txt",f4); cout<<"overlp 8-9: "<<GetTickCount() - tick<<" ms."<<endl; tick = GetTickCount(); filter_overlp("overlp_9.txt","overlp_10.tx
24、t",f5); DWORD overlp_end_time=GetTickCount(); cout<<"overlp 9-10: "<<overlp_end_time - tick<<" ms."<<endl<<endl; /輸出三種模式下的平均時間以做對比 cout<<"三種模式的平均用時如下:"<<endl; cout<<"·無文件高速緩存模式平均用時 :" <<(nobuffer_
25、end_time-nobuffer_start_time)/10<<" ms."<<endl; cout<<"·使用文件高速緩存模式平均用時 :" <<(sequen_end_time-sequen_start_time)/10<<" ms."<<endl; cout<<"·異步傳輸模式平均用時 :" <<(overlp_end_time-overlp_start_time)/10<<&q
26、uot; ms."<<endl<<endl; return;/對文件內(nèi)容進行的5種操作(可以任意定義),本程序僅用了五個很簡單的操作/f1 +1/f2 -1/f3 *1/f4 >>/f5 <<void f1(char* addr) *addr = (unsigned char)*addr + 1; void f2(char* addr) *addr = (unsigned char)*addr - 1; void f3(char* addr) *addr = (unsigned char)*addr * 1; void f4(char*
27、 addr) *addr = (unsigned char)*addr >> 1;void f5(char* addr) *addr = (unsigned char)*addr << 1;/沒有文件高速緩存的filter函數(shù)void filter_nobuffer(char* source, char* sink, void (*func) (char* addr) HANDLE handle_src,handle_dst; /定義源文件與目標文件的句柄 BOOL cycle; /用以判斷是否滿一個緩沖區(qū) DWORD NumberOfBytesRead,NumberO
28、fBytesWrite,index; /讀的字節(jié)數(shù)、寫的字節(jié)數(shù) /打開源文件handle_src=CreateFile(source,GENERIC_READ,NULL,NULL,OPEN_EXISTING,FILE_FLAG_NO_BUFFERING,NULL);/創(chuàng)建目標文件handle_dst = CreateFile(sink,GENERIC_WRITE,NULL,NULL,CREATE_ALWAYS,NULL,NULL); /如果打開或創(chuàng)建失敗,則報錯 if( handle_src = INVALID_HANDLE_VALUE | handle_dst = INVALID_HANDL
29、E_VALUE) cout<<"CreateFile Invocation Error!"<<endl; exit(1); cycle = TRUE; /用cycle判斷文件什么時候讀完 while(cycle) /從源文件讀數(shù)據(jù)送入緩沖區(qū) if(ReadFile(handle_src,buffer,BUFFER_SIZE,&NumberOfBytesRead,NULL) = FALSE) cout<<"ReadFile Error!"<<endl; exit(1); /當讀不滿一個緩沖區(qū)時,說明達
30、到文件末尾,結(jié)束循環(huán) if(NumberOfBytesRead < BUFFER_SIZE) cycle = FALSE; /對文件內(nèi)容進行的操作 for(index = 0;index < NumberOfBytesRead;index+) func(&bufferindex); /將緩沖區(qū)中的數(shù)據(jù)寫入目標文件if(WriteFile(handle_dst,buffer,NumberOfBytesRead,&NumberOfBytesWrite,NULL) = FALSE) cout<<"WriteFile Error!"<&
31、lt;endl; exit(1); /關(guān)閉文件句柄 CloseHandle(handle_src); CloseHandle(handle_dst);void filter_sequen(char* source, char* sink, void (*func) (char* addr) HANDLE handle_src,handle_dst; /定義源文件與目標文件的句柄 BOOL cycle; /用以判斷是否滿一個緩沖區(qū) DWORD NumberOfBytesRead,NumberOfBytesWrite,index; /讀的字節(jié)數(shù)、寫的字節(jié)數(shù) /CreateFile函數(shù)設(shè)置參數(shù)FIL
32、E_FLAG_SEQUENTIAL_SCAN:使用文件高速緩存 /打開源文件handle_src=CreateFile(source,GENERIC_READ,NULL,NULL,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,NULL); /創(chuàng)建目標文件handle_dst=CreateFile(sink,GENERIC_WRITE,NULL,NULL,CREATE_ALWAYS,FILE_FLAG_SEQUENTIAL_SCAN,NULL); /如果打開或創(chuàng)建失敗,則報錯 if( handle_src = INVALID_HANDLE_VALUE | han
33、dle_dst = INVALID_HANDLE_VALUE) cout<<"CreateFile Invocation Error!"<<endl; exit(1); cycle = TRUE; /用cycle判斷文件什么時候讀完 while(cycle) /從源文件讀數(shù)據(jù)送入緩沖區(qū) if(ReadFile(handle_src,buffer,BUFFER_SIZE,&NumberOfBytesRead,NULL)=FALSE) cout<<"ReadFile Error!"<<endl; exi
34、t(1); /當讀不滿一個緩沖區(qū)時,說明達到文件末尾,結(jié)束循環(huán) if(NumberOfBytesRead < BUFFER_SIZE) cycle = FALSE; /對文件內(nèi)容進行的操作 for(index = 0;index < NumberOfBytesRead;index+) func(&bufferindex); /將緩沖區(qū)中的數(shù)據(jù)寫入目標文件 if(WriteFile(handle_dst,buffer,NumberOfBytesRead,&NumberOfBytesWrite, NULL) = FALSE) cout<<"Writ
35、eFile Error!"<<endl; exit(1); /關(guān)閉文件句柄 CloseHandle(handle_src); CloseHandle(handle_dst);void filter_overlp(char* source, char* sink, void (*func) (char* addr) HANDLE handle_src,handle_dst; /定義源文件與目標文件的句柄 BOOL cycle; /用以判斷是否滿一個緩沖區(qū) /讀的字節(jié)數(shù)、寫的字節(jié)數(shù)、GetLastError函數(shù)的返回值 DWORD NumberOfBytesRead,NumberOfBytesWrite,index,dwError; OVERLAPPED overlapped; /overlapped 結(jié)構(gòu) /打開源文件 handle_src=CreateFile(source,GENERIC_READ,NULL,NULL,OPEN_EXISTING, FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED,NULL); /創(chuàng)建目標文件 handle_dst=CreateFile(sink,GENERIC_WRITE,NULL,N
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五版油氣田鉆井技術(shù)服務(wù)質(zhì)量承包合同3篇
- 2025年度環(huán)保型廠房設(shè)計與施工總承包合同3篇
- 二零二四年在線教育平臺軟件全國代理銷售合同模板2篇
- 2025年度全國范圍內(nèi)土地測繪技術(shù)服務(wù)合同范文3篇
- 2024版液化天然氣交易協(xié)議全文下載版B版
- 2024版運輸行業(yè)職員勞動協(xié)議樣本
- 2024年地基買賣合同附帶地基檢測及質(zhì)量認證3篇
- 2025年大棚農(nóng)業(yè)綠色生產(chǎn)技術(shù)引進合同3篇
- 2025年度綠色建筑:知識產(chǎn)權(quán)許可與環(huán)保建材合同3篇
- 2025年智慧能源物業(yè)工程承包及節(jié)能服務(wù)合同3篇
- 2024版塑料購銷合同范本買賣
- 【高一上】【期末話收獲 家校話未來】期末家長會
- JJF 2184-2025電子計價秤型式評價大綱(試行)
- GB/T 44890-2024行政許可工作規(guī)范
- 有毒有害氣體崗位操作規(guī)程(3篇)
- 兒童常見呼吸系統(tǒng)疾病免疫調(diào)節(jié)劑合理使用專家共識2024(全文)
- 2025屆山東省德州市物理高三第一學(xué)期期末調(diào)研模擬試題含解析
- 《華潤集團全面預(yù)算管理案例研究》
- 二年級下冊加減混合豎式練習360題附答案
- 異地就醫(yī)備案個人承諾書
- 蘇教版五年級數(shù)學(xué)下冊解方程五種類型50題
評論
0/150
提交評論