读速测试

    技术2026-04-21  4

    测试结果:

    1

    Path: /Program/ReadSpeed/data.txt

    Size: 2097152(Byte)

    API:              61

    MFC:             182

    C Language: 845

    2

    Path: /ResidentFlash/ReadSpeed/data.txt

    Size: 2097152(Byte)

    API:              312

    MFC:             2341

    C Language: 1388

     

     

    //API

    DWORD SamsungAPI_ReadTest(CString szFileName){ //Test data const DWORD READ_BUF_SIZE = 4096*4; unsigned char *sReadBuf; sReadBuf = new unsigned char[(int)READ_BUF_SIZE]; memset(sReadBuf, 0x00, (int)READ_BUF_SIZE);

     //----------------    read   -------------------------------- HANDLE hFile; WIN32_FIND_DATA wfd; DWORD dwReadTicks = 0;  hFile = CreateFile(szFileName,GENERIC_READ,0,NULL,  OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING,NULL); if (hFile == INVALID_HANDLE_VALUE)  {  return dwReadTicks; } FindFirstFile(szFileName,&wfd);//大于4G的文件才用到高位 DWORD nSize = wfd.nFileSizeLow; DWORD dwRead    = 0; long CountTick = 0; while(nSize>0) {  CountTick = GetTickCount();  // Start!  ReadFile(hFile, sReadBuf, (int)READ_BUF_SIZE, &dwRead, NULL); // To move the file pointer is not needed for performance.     dwReadTicks += GetTickCount() - CountTick; // End!  nSize -= dwRead; }; CloseHandle(hFile);  delete []sReadBuf;  return dwReadTicks;}

     

     

    // MFC

    long MFC_ReadTest(CString szPath){ long lStartTime    = 0, lReadTimeTotal    = 0; const int READ_TIMES = 1; for (int m = 0; m < READ_TIMES; m++) //多读几次 {  long lTotalLen = 0;

      //读出数据  CFile file;  file.Open(szPath, CFile::modeRead);  if (!file)  {   AfxMessageBox(_T("读取文件失败!"));   return lReadTimeTotal;  }  const int READ_BUF_SIZE = 4096*4;  char sReadBuf[READ_BUF_SIZE];

      //读时测试  lStartTime = GetTickCount();  int iReadLen = file.Read(sReadBuf, READ_BUF_SIZE);  lReadTimeTotal += GetTickCount() - lStartTime;

      while (iReadLen > 0)  {   lTotalLen += iReadLen;   //看是否还有剩余数据   lStartTime = GetTickCount();   iReadLen = file.Read(sReadBuf, READ_BUF_SIZE);   lReadTimeTotal += GetTickCount() - lStartTime;  }

      file.Close(); } return lReadTimeTotal;}

     

     

    //C

    DWORD CLanguage_ReadTest(char szPath[MAX_PATH]){ FILE *f = fopen(szPath,"r");  DWORD dwReadTicks = 0; if(f) {  const DWORD READ_BUF_SIZE = 4096*4;  char *sReadBuf = new char[(int)READ_BUF_SIZE];  memset(sReadBuf, 0, (int)READ_BUF_SIZE);  long CountTick = 0;  CountTick = GetTickCount();  while( fgets(sReadBuf,READ_BUF_SIZE,f))//按行读取  {   dwReadTicks += GetTickCount() - CountTick;    CountTick = GetTickCount();  }  delete []sReadBuf;   fclose(f);  } return dwReadTicks;}

    最新回复(0)