Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 621893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:55:32+00:00 2026-05-13T18:55:32+00:00

Considering a really huge file(maybe more than 4GB) on disk,I want to scan through

  • 0

Considering a really huge file(maybe more than 4GB) on disk,I want to scan through this file and calculate the times of a specific binary pattern occurs.

My thought is:

  1. Use memory-mapped file(CreateFileMap
    or boost mapped_file) to load the
    file to the virtual memory.

  2. For each 100MB mapped-memory,create one thread to scan and calculate the result.

Is this feasible?Are there any better method to do so?

Update:
Memory-mapped file would be a good choice,for scaning through a 1.6GB file could be handled within 11s.

thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T18:55:32+00:00Added an answer on May 13, 2026 at 6:55 pm

    Multithreading is only going to make this go slower unless you want to scan multiple files with each on a different hard drive. Otherwise you are just going to seek.

    I wrote a simple test function using memory mapped files, with a single thread a 1.4 Gb file took about 20 seconds to scan. With two threads, each taking half the file (even 1MB chunks to one thread, odd to the other), it took more than 80 seconds.

    • 1 thread: 20015 milliseconds
    • 2 threads: 83985 milliseconds

    That’s right, 2 threads was Four times slower than 1 thread!

    Here’s the code I used, this is the single threaded version, I used a 1 byte scan pattern, so the code to locate matches that straddle map boundaries is untested.

    HRESULT ScanForPattern(LPCTSTR pszFilename, LPBYTE pbPattern, UINT cbPattern, LONGLONG * pcFound)
    {
       HRESULT hr = S_OK;
    
       *pcFound = 0;
       if ( ! pbPattern || ! cbPattern)
          return E_INVALIDARG;
    
       //  Open the file
       //
       HANDLE hf = CreateFile(pszFilename,
                              GENERIC_READ,
                              FILE_SHARE_READ, NULL,
                              OPEN_EXISTING,
                              FILE_FLAG_SEQUENTIAL_SCAN,
                              NULL);
    
       if (INVALID_HANDLE_VALUE == hf)
          {
          hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
          // catch an open file that exists but is in use
          if (ERROR_SHARING_VIOLATION == GetLastError())
             hr = HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION);
          return hr;
          }
    
       // get the file length
       //
       ULARGE_INTEGER  uli;
       uli.LowPart = GetFileSize(hf, &uli.HighPart);
       LONGLONG cbFileSize = uli.QuadPart;
       if (0 == cbFileSize)
          {
          CloseHandle (hf);
          return S_OK;
          }
    
       const LONGLONG cbStride = 1 * 1024 * 1024; // 1 MB stride.
       LONGLONG cFound  = 0;
       LPBYTE   pbGap = (LPBYTE) malloc(cbPattern * 2);
    
       //  Create a mapping of the file.
       //
       HANDLE hmap = CreateFileMapping(hf, NULL, PAGE_READONLY, 0, 0, NULL);
       if (NULL != hmap)
          {
          for (LONGLONG ix = 0; ix < cbFileSize; ix += cbStride)
             {
             uli.QuadPart = ix;
             UINT cbMap = (UINT) min(cbFileSize - ix, cbStride);
             LPCBYTE pb = (LPCBYTE) MapViewOfFile(hmap, FILE_MAP_READ, uli.HighPart, uli.LowPart, cbMap);
             if ( ! pb)
                {
                hr = HRESULT_FROM_WIN32(GetLastError());
                break;
                }
             // handle pattern scanning over the gap.
             if (cbPattern > 1 && ix > 0)
                {
                CopyMemory(pbGap + cbPattern - 1, &pb[0], cbPattern - 1);
                for (UINT ii = 1; ii < cbPattern; ++ii)
                   {
                   if (pb[ii] == pbPattern[0] && 0 == memcmp(&pb[ii], pbPattern, cbPattern))
                      {
                      ++cFound; 
                      // advance by cbPattern-1 to avoid detecting overlapping patterns
                      }
                   }
                }
    
             for (UINT ii = 0; ii < cbMap - cbPattern + 1; ++ii)
                {
                if (pb[ii] == pbPattern[0] && 
                    ((cbPattern == 1) || 0 == memcmp(&pb[ii], pbPattern, cbPattern)))
                   {
                   ++cFound; 
                   // advance by cbPattern-1 to avoid detecting overlapping patterns
                   }
                }
             if (cbPattern > 1 && cbMap >= cbPattern)
                {
                // save end of the view in our gap buffer so we can detect map-straddling patterns
                CopyMemory(pbGap, &pb[cbMap - cbPattern + 1], cbPattern - 1);
                }
             UnmapViewOfFile(pb);
             }
    
          CloseHandle (hmap);
          }
       CloseHandle (hf);
    
       *pcFound = cFound;
       return hr;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, maybe this isn't so amazing considering I don't really understand how the debugger
Considering this part of a Java class, private List<Object> components = new ArrayList<Object>(); private
Considering that I have a Schema named SBST I want to find all empty
Considering it's a common issue, what could be the source of this error if
Considering this code with 3 differents function call semantics: void f(void){ puts(OK); } int
I'm considering writing my own delivery code using PowerShell and/or C#, maybe shelling to
I'm considering a personal learning project. Using .NET(preferably VB) I want to build a
I'm considering dumping boost as a dependency... atm the only thing that I really
Because I realized game rule logic should handle huge complexity, I'm considering using of
Yep. This question came up looking at Code Igniter but it is not really

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.