Thread: Bit output

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    32

    Bit output

    Hello,

    I'm programming a set of functions for bit output. This will be buffered, suppose I have two pointers:

    Code:
    unsigned char *buffer, *buffer_start;
    The pointer buffer points to the current byte and buffer_start always points to the beginning of the dynamically allocated buffer.

    My question is:

    Should I introduce an integer variable buf_count that keeps the number of bytes ready in the buffer or should I calculate the number of bytes in the buffer by this formula: buffer - buffer_start? I'll need to know the number of bytes in the buffer in order to compare it with the constant BUFFER_SIZE and know when the buffer is full and ready to be written to a file.

    My main concern here is speed.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It would seem that you are checking every time, so either you do the subtraction buffer - buffer_start each time, or you have a separate variable and you add 1 each time. I don't know that there's going to be much difference between adding and subtracting in terms of time taken.

    (In olden times, comparisons against zero were supposed to be faster than comparisons against a number, so using a "countdown" approach might, emphasize might, in theory, emphasize theory, save a minuscule, emphasize minuscule, amount of time.)

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    How do you plan on updating buf_count as it's dynamic; by incrementing it?

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    32
    Quote Originally Posted by itCbitC View Post
    How do you plan on updating buf_count as it's dynamic; by incrementing it?
    Yes, incrementing it by one each time I increment the buffer pointer.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by kkk View Post
    Yes, incrementing it by one each time I increment the buffer pointer.
    It's hard to say since whatever you do can always be optimized by the compiler.
    buffer - buffer_start results need to be cast to the proper type before comparing to BUFFER_SIZE.
    buffer_count already is of the proper type but needs more steps for incrementing it each time.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    C++ containers like vector typically subtract the two pointers to get the size.
    Since they're designed for efficiency, it's probably a good choice.

    Quote Originally Posted by itCbitC View Post
    buffer - buffer_start results need to be cast to the proper type before comparing to BUFFER_SIZE.
    No it doesn't. subtracting two pointers gives a ptrdiff_t which is a basic signed integer type.
    Last edited by iMalc; 07-21-2011 at 01:21 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ overlapping output and adding extensions to output files
    By lordmorgul in forum Linux Programming
    Replies: 9
    Last Post: 05-11-2010, 08:26 AM
  2. How to edit output in struct and call for the output
    By andrewkho in forum C Programming
    Replies: 4
    Last Post: 03-16-2010, 10:28 PM
  3. terminal output not showing output properly
    By stanlvw in forum C Programming
    Replies: 13
    Last Post: 11-19-2007, 10:46 PM
  4. output a string to a standard output
    By sh4k3 in forum C Programming
    Replies: 3
    Last Post: 06-15-2007, 05:59 AM
  5. Replies: 3
    Last Post: 02-19-2003, 08:34 PM