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 7967251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:38:47+00:00 2026-06-04T06:38:47+00:00

I need to iterate through a set of bytes, searching for a 4 byte

  • 0

I need to iterate through a set of bytes, searching for a 4 byte value (all 4 bytes are the same). The length of the data is variable and these bytes can be anywhere inside the data; I’m looking for the first instance. I’m trying to find the fastest possible implementation because this logic runs in a critical part of my code.

This will only ever run on x86 & x64, under Windows.

typedef unsigned char Byte;
typedef Byte* BytePtr;
typedef unsigned int UInt32;
typedef UInt32* UInt32Ptr;

const Byte MARKER_BYTE = 0xAA;
const UInt32 MARKER = 0xAAAAAAAA;

UInt32 nDataLength = ...;
BytePtr pData = ...;
BytePtr pEnd = pData + nDataLength - sizeof ( UInt32 );

// Option 1 -------------------------------------------
while ( pData < pEnd )
{
    if ( *( (UInt32Ptr) pData ) == MARKER )
    {
        ... // Do something here
        break;
    }

    pData++;
}

// Option 2 -------------------------------------------
while ( pData < pEnd )
{
    if ( ( *pData == MARKER_BYTE ) && ( *( (UInt32Ptr) pData ) == MARKER ) )
    {
        ... // Do something here
        break;
    }

    pData++;
}

I think Option 2 is faster but I’m not sure if my reasoning is correct.

Option 1 first reads 4 bytes from memory, checks it against the 4-byte constant and if not found, it steps onto the next byte and starts over. The next 4-byte ready from memory is going to overlap 3 bytes already read so the same bytes need to be fetched again. Most bytes before my 4-byte marker would be read twice.

Option 2 only reads 1 byte at a time and if that single byte is a match, it reads the full 4-byte value from that address. This way, all bytes are read only once and only the 4 matching bytes are read twice.

Is my reasoning correct or am I overlooking something?

And before someone brings it up, yes, I really do need to perform this kind of optimization. 🙂

Edit: note, that this code will only ever run on Intel / AMD based computers. I don’t care if other architectures would fail to run this, as long as normal x86 / x64 computers (desktops / servers) run this without problems or performance penalties.

Edit 2: compiler is VC++ 2008, if that helps.

  • 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-06-04T06:38:49+00:00Added an answer on June 4, 2026 at 6:38 am

    You might also try the Boyer-Moore approach.

    pData = start + 3;
    int i;
    
    while(pData < pEnd) {
        for(i = 0; i < 4; ++i) {
            if (*(pData-i) != MARKER_BYTE) {
                pData += 4-i;
                break;
            }
        }
        if (i == 4) {
            /* do something here with (pData-3) */
            break;
        }
    }
    

    If you’re lucky, that tests only every fourth byte until you found a match.

    Whether that’s faster or slower than testing every single byte is anybody’s guess for short patterns as this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a property within a class, that I need to iterate through all
I need to iterate through SortedMap's entry set (which is a SortedSet) backwards. The
I need to iterate through a number of MySQL queries and save them in
Say I have components that I need to iterate through, for example From and
I have a deserialized xml c# objet. I need to iterate through the oject
I have a simple Dictionary(of String, Object) that I need to iterate through and
I am working on a project where I need to recursively iterate through the
I need the JavaScript code to iterate through the filled attributes in an HTML
As I iterate through a DataTable object, I need to check each of its
I need to go through a set and remove elements that meet a predefined

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.