哪位大哥帮我看看这个程序该如何优化运行速度?
貌似中间字节高低位排序并写入文件的操作执行起来效率级低啊,一秒钟才处理几十K,为什么这么慢啊?
// strip.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>#include <fstream>#include <string>#include <cstdlib>inline void exch(char ex[],long size){ char c; switch (size) { case 4: { c=ex[0];ex[0]=ex[3];ex[3]=c; c=ex[1];ex[1]=ex[2];ex[2]=c; break; } case 3: { ex[0]=ex[3]; c=ex[1];ex[1]=ex[2];ex[2]=c; break; } case 2: { ex[0]=ex[3]; ex[1]=ex[2]; break; } case 1: { ex[0]=ex[3]; break; } }}int _tmain(int argc, _TCHAR* argv[]){ using namespace std; ifstream fin; fstream fout; ofstream fout2; fin.open("test.dat", ios_base::in |ios_base::binary);//打开数据文件 fout2.open("testout.dat",ios_base::out|ios_base::binary); if (fin.is_open()) { long m;//EB905717末(第m字节) long n;//数据长度 long p;//读指针位置 long end;//一帧的压缩数据末(第end字节) long size;//重新排序时的数组长度 char ex[4]; char head[4]={'\xEB','\x90','\x57','\x17'}; while (1) { for (fin.read(ex,4);memcmp(ex,head,4);) { fin.seekg(-3,ios::cur); fin.read(ex,4); if(fin.eof())break;//读指针到达文件尾时跳出 }//读取4个字节到ex[4],如果ex[4]不为0xEB905717,则读指针位置倒退3个字节 if(fin.eof())break; m=fin.tellg();//EB905717末(第m字节) fin.seekg(8,ios::cur); p=fin.tellg(); fin.read(ex,4);//把压缩数据长度写入ex p=fin.tellg(); exch (ex,4); n=*((long *)ex);//数据长度转为long型 end=m+12+n; p=fin.tellg(); while((end-p)>=4){//读指针(p=tellg)距离数据尾(m+13+n)大于等于4字节,size=4,向后读取size(4)个字节,进行数组长度为size(4)的字节顺序排列,并在fileout写入size(4)个字节 size=4; fin.read(ex,4); if(fin.eof())break; p=fin.tellg(); exch (ex,4); fout2.write(ex,4); } if( ((end-p)<4) & ((end-p)>0) ){//读指针(p=tellg)距离数据尾(m+13+n)少于4字节,size=n%4,向后读取4个字节,进行数组长度为size的字节顺序排列,并在fileout写入size个字节 size=n%4; fin.read(ex,4); if(fin.eof())break; p=fin.tellg(); exch (ex,size); fout2.write(ex,size); } } } fin.close(); fout2.close(); return 0;}