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

A

anon.asdf

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
????



/******* 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';
puts(dest);
}

/********* 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';
}
 
A

anon.asdf

On Oct 11, 2:54 am, (e-mail address removed) <<<Albert - forgot to sign the
root post>>> wrote:
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]; \

<snip>

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

Regards,
Albert
 
E

Eric Sosman

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

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.)
 
G

Gene

On Oct 11, 2:54 am, (e-mail address removed) <<<Albert - forgot to sign theroot post>>> wrote:

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]; \

<snip>

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

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.
 
K

Keith Thompson

Gene said:
On Oct 11, 2:54 am, (e-mail address removed) <<<Albert - forgot to sign theroot post>>> wrote:

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]; \

<snip>

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

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.

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.)
 
A

anon.asdf

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!"

"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. -?
(See
Section 6.5.14; see alsohttp://www.c-faq.com/Question 20.13.)

I've had a look and fail to see how Section 6.5.14
http://c0x.coding-guidelines.com/6.5.14.html
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf#page=101

is relevant.

-Albert
 
E

Eric Sosman

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!"

"Yes" to the first part (use multiple putchar()'s) OR "yes" to the
second part (collect and use puts()) ???
[...]
(See
Section 6.5.14; see alsohttp://www.c-faq.com/Question 20.13.)

I've had a look and fail to see how Section 6.5.14
http://c0x.coding-guidelines.com/6.5.14.html
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf#page=101

is relevant.

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.
 
K

Kenneth Brody

"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 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:[email protected]>
 
B

Barry Schwarz

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
????

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
character.

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.
/******* 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];

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.
char *dest_ptr = dest;

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

/********* 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++ = ' ');

Why are you storing the output characters anywhere?
{
int i;
char source[29] = "asdfasdfasdfasdfsadfasdfasdf";
char *src_ptr = source;
char dest[29];

Also too small.
char *dest_ptr = dest;

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

Since you output individual characters and not a string, why bother?


Remove del for email
 
S

SM Ryan

(e-mail address removed) wrote:
# 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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top