"multiple putchar()'s" versus "collect chars in char-array and use puts()" - speed/efficiency

Discussion in 'C Programming' started by anon.asdf, Oct 11, 2007.

  1. anon.asdf

    anon.asdf Guest

    In terms of efficieny:

    Is it better to use multiple putchar()'s after one another as one gets
    to new char's


    is it better to collect the characters to a char-array first, and then
    use puts() to print to screen

    /******* ExampleA **********/
    /**** collect chars and then call puts ****/

    char symbols[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    'A', 'B', 'C', 'D', 'E', 'F'};

    #define uchar2hex(char_ptr, str_ptr) \
    *str_ptr++ = symbols[((unsigned char)*char_ptr) >> 4]; \
    *str_ptr++ = symbols[((unsigned char)*char_ptr++) & 0xF]; \
    *str_ptr++ = ' ';

    int i;
    char source[29] = "asdfasdfasdfasdfsadfasdfasdf";
    char *src_ptr = source;
    char dest[29];
    char *dest_ptr = dest;

    for (i = 0; i < 29; i++) {
    uchar2hex(src_ptr, dest_ptr);
    *dest_ptr = '\0';

    /********* ExampleB *************/
    /***** putchar() as each new char is encountered ******/

    char symbols[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    'A', 'B', 'C', 'D', 'E', 'F'};

    #define uchar2hex2(char_ptr, str_ptr) \
    putchar(*str_ptr++ = symbols[((unsigned char)*char_ptr) >> 4]); \
    putchar(*str_ptr++ = symbols[((unsigned char)*char_ptr++) & 0xF]); \
    putchar(*str_ptr++ = ' ');

    int i;
    char source[29] = "asdfasdfasdfasdfsadfasdfasdf";
    char *src_ptr = source;
    char dest[29];
    char *dest_ptr = dest;

    for (i = 0; i < 29; i++) {
    uchar2hex2(src_ptr, dest_ptr);
    *dest_ptr = '\0';
    anon.asdf, Oct 11, 2007
    1. Advertisements

  2. anon.asdf

    anon.asdf Guest

    On Oct 11, 2:54 am, <<<Albert - forgot to sign the
    root post>>> wrote:

    Is there a faster way of converting a 8-bit char to its 2-digit hex

    anon.asdf, Oct 11, 2007
    1. Advertisements

  3. anon.asdf

    Eric Sosman Guest

    The C Standard says next to nothing about efficiency, even
    though "efficiency" is one of the principal reasons people turn
    to C nowadays. However, in regard to the question you ask the
    Standard, unusually, offers an unequivocal answer: "Yes!" (See
    Section 6.5.14; see also http://www.c-faq.com/ Question 20.13.)
    Eric Sosman, Oct 11, 2007
  4. anon.asdf

    Gene Guest

    Well, you can do it without the global table. Though this probably
    won't be faster, it ought not to be slower.

    #define uchar2hex(P, S) do { \
    *(P)++ = "0123456789ABCDEF"[(int)(unsigned char)*(S)]; \
    *(P)++ = "0123456789ABCDEF"[(int)(unsigned char)*(S)++ & 0xF]; }
    while (0)

    Using a 256x2 table would probably be slightly faster.

    char hex[][2] = { { '0', '0' }, ... };

    #define uchar2hex(P, S) do { \
    memcpy(P, hex[(int)(unsigned char)*(S)++], 2);
    (P) += 2; } while (0)

    especially if your compiler open codes the memcpy.
    Gene, Oct 11, 2007
  5. The "do { ... } while (0)" trick is an improvement over the original
    macro definition, since it allows it to be used in any statement
    context. But since the macro definition consists entirely of
    expressions, it's better to just define it as a single expression.

    Re-writing the original macro (without changing what it does) yields:

    #define uchar2hex(char_ptr, str_ptr) \
    ( *(str_ptr)++ = symbols[((unsigned char)*(char_ptr)) >> 4], \
    *(str_ptr)++ = symbols[((unsigned char)*(char_ptr)++) & 0xF], \
    *(str_ptr)++ = ' ' )

    (The last line was lost in a quoting mishap.)
    Keith Thompson, Oct 11, 2007
  6. anon.asdf

    anon.asdf Guest

    "Yes" to the first part (use multiple putchar()'s) OR "yes" to the
    second part (collect and use puts()) ???

    I suspect you mean "yes" to the 2nd part, since there puts() works at
    a low level and can hand multiple chunks to the driver routine which
    outputs the text; whereas putchar() only hands one char at a time to
    the driver - slowing thing down. -?
    I've had a look and fail to see how Section 6.5.14

    is relevant.

    anon.asdf, Oct 11, 2007
  7. anon.asdf

    Eric Sosman Guest

    It describes the result of the logical OR operator.
    In your case, the two propositions are opposites, so
    their OR is true, hence "Yes!"

    Once again, I commend Question 20.13 of the FAQ to
    your attention.
    Eric Sosman, Oct 11, 2007
  8. I suspect he meant "yes" to the entire statement. (As in, "yes,
    either it will be more efficient to do the former, or it will be
    more efficient to do the latter".) It was a way of saying there
    is no firm "using this method will be more efficient in all
    situations". In some cases, one will be "better", while in other
    cases, that same one is "worse".
    6.5.14 describes the logical OR operator. The statement "A or B" is
    true if either A or B is true. (You asked "is this better OR is that
    better?" Since one of those must be true, "A or B" must be true.)

    His reference to 6.5.14 confirms my original interpretation.

    Consider that, to many programmers, "do you know what time it is"
    requires a boolean response.

    Consider that, to a computer, "give me a list of people who live in
    New York and New Jersey" is likely to return a very short (if not
    empty) list.

    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Oct 11, 2007
  9. It depends on how your system implements I/O. The various byte output
    functions are required to behave "as if" fputc was called for each

    If puts and putchar both actually do call fputc for each character and
    if putchar is a macro, then

    The putchar approach requires n calls to fputc.

    The puts approach requires the same n calls + one call to puts
    + the effort to build the array.

    putchar appears more efficient.

    If puts does its own I/O in a single block and if function calls are
    "expensive", then puts appears more efficient.

    Unless you are doing a ton of I/O, it would be better(tm) to use the
    approach that is more natural to whatever process you are performing.
    The execution cost will be insignificant compared to future
    maintenance costs when the program is updated.
    This is the wrong size for dest. Each input character requires three
    output characters. Once i gets to 10, you will overflow this array
    and enter the realm of undefined behavior.
    Why are you storing the output characters anywhere?
    Also too small.
    Since you output individual characters and not a string, why bother?

    Remove del for email
    Barry Schwarz, Oct 13, 2007
  10. anon.asdf

    SM Ryan Guest

    # In terms of efficieny:
    # Is it better to use multiple putchar()'s after one another as one gets
    # to new char's
    # OR
    # is it better to collect the characters to a char-array first, and then
    # use puts() to print to screen
    # ????

    Whatever simplifies your code is best.
    SM Ryan, Oct 20, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.