Thread: Fastest way to check if the bytes contained in an array are all x00

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    7

    Question Fastest way to check if the bytes contained in an array are all x00

    Hello to all the board

    I'll process the raw content of a stream which obviously will be loaded one chunk at a time into an buffer.

    In my first attempt, I need to check if every chunk content is x00 filled. If it is, I'll increase the blank chunks counter of 1.

    On your personal opinion, which is the fastest an less cycles consuming manner to achieve such result?

    I was wondering this: is it possible to make an instant XOR of all the buffer content to check if it does return 0 ?

    Or really, the only way is it to cycle through all the bytes and compare each one of them with 0?

    Thank you for hinting

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Make sure the buffer is aligned on a boundary which permits access via the longest native type of the machine (say unsigned long).

    unsigned long *p = (unsigned long*)buff;

    Now p[i] == 0 tests say 4 bytes at once in a single machine instruction.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 02-23-2013, 03:24 AM
  2. Problem with array of bytes
    By cioannou in forum C++ Programming
    Replies: 1
    Last Post: 02-22-2010, 06:01 AM
  3. Ansi C - check file size in bytes.
    By Ironic in forum C Programming
    Replies: 9
    Last Post: 04-02-2009, 11:54 AM
  4. concatenating 2 bytes of an array
    By davo666 in forum C Programming
    Replies: 1
    Last Post: 02-28-2009, 07:37 AM
  5. Fastest way to search the table ( or array )
    By prayami in forum C++ Programming
    Replies: 5
    Last Post: 03-17-2008, 04:56 PM