?
Solved

printf() radix conversions

Posted on 2003-10-29
7
Medium Priority
?
653 Views
Last Modified: 2013-12-14
What algorithm does printf use to convert integers to radix 16? printf must evaluate its input string, find the variable in its va_list, convert it into hex, and format it somewhere along the way. I would have thought that I could write my own conversion that would be faster as I don't need the format string and the va stuff. However, I keep finding that printf outperforms my algorithms by roughly .2 seconds when outputing 9000+ shorts. What is printf doing? Is it possible that the compiler's author(s) used asm code to get the speed or am I missing something?

Exceter
0
Comment
Question by:Exceter
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 7

Expert Comment

by:MrNed
ID: 9647828
Such a common function would be optimised to the hilt. Its most likely written in asm. But the conversion isnt the only thing that can cause the 200ms difference - theres function calling overhead aswell. What does your code look like?
0
 
LVL 6

Expert Comment

by:Ajar
ID: 9647918
such library functions are  optimized by the compiler not only by the asm implementation but also by
using the special facilities provided by a particular cpu hardware architecture .

0
 
LVL 15

Expert Comment

by:efn
ID: 9648078
> What is printf doing?

Assuming you really mean "How is it doing it?", that depends on your particular library implementation.  It's impossible to answer for all library implementations in general.  It may be possible to trace into the disassembly of the printf function to see how it formats hexadecimal strings.  If you specify what compiler you are using, some wizardly expert here may be able to give you a specific answer.

> Is it possible that the compiler's author(s) used asm code to get the speed or am I missing something?

Both are quite possible.  ;-)

--efn
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 23

Expert Comment

by:brettmjohnson
ID: 9648732
I tried a half dozen different ways to implement it.  With compiler optimization -O3,
this was the most efficient implementation:

static inline char * tohex32c(char * buff, unsigned int n)
{
  static char hexd[] = "0123456789ABCDEF";
  if (buff) {
    *buff++ = hexd[ (n >> 28) & 15 ];
    *buff++ = hexd[ (n >> 24) & 15 ];
    *buff++ = hexd[ (n >> 20) & 15 ];
    *buff++ = hexd[ (n >> 16) & 15 ];
    *buff++ = hexd[ (n >> 12) & 15 ];
    *buff++ = hexd[ (n >> 8) & 15 ];
    *buff++ = hexd[ (n >> 4) & 15 ];
    *buff++ = hexd[ n & 15 ];
    *buff = '\0';
  }
  return buff;
}

With no optimization, this macro version was faster:

static inline char * tohex32c(char * buff, unsigned int n)
{
  static char hexd[] = "0123456789ABCDEF";
  if (buff) {
    *buff++ = hexd[ (n >> 28) & 15 ];
    *buff++ = hexd[ (n >> 24) & 15 ];
    *buff++ = hexd[ (n >> 20) & 15 ];
    *buff++ = hexd[ (n >> 16) & 15 ];
    *buff++ = hexd[ (n >> 12) & 15 ];
    *buff++ = hexd[ (n >> 8) & 15 ];
    *buff++ = hexd[ (n >> 4) & 15 ];
    *buff++ = hexd[ n & 15 ];
    *buff = '\0';
  }
  return buff;
}

0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 9650355

Hi Brett,

Depending upon the underlying instruction set, picking a character from within a string could be fairly expensive.  Can you retry your tests with the array defined as:

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


Thanks,
Kent
0
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 500 total points
ID: 9652020
Kdo,

I figured using a array of ints for the hex digits would give about the same results as
using an array of char for the hex digits.  Maybe slightly better (for word aligned access).
Maybe slightly worse (more data, more instructions).  As it turns out, it is more than 2X
slower.  It surprised the heck out of me.

My test runs 100,000,000 iterations of each loop on a 600Mhz G3 using gcc 3.3 with -O3

char hexd[]  : 1133 msecs
  int hexd[]  : 2770 msecs

Here is the complete test:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>

static char * tohex8(char * buff, unsigned char n)
{
  if (buff) {
    unsigned char c;
    *buff++ = ((c = (n >> 4) & 15) < 10) ? c + '0' : c + ('A' - 10);
    *buff++ = ((c = n & 15) < 10) ? c + '0' : c + ('A' - 10);
    *buff = '\0';
  }
  return buff;
}


static char * tohex16(char * buff, unsigned short n)
{
  if (buff) {
    unsigned char c;
    *buff++ = ((c = (n >> 12) & 15) < 10) ? c + '0' : c + ('A' - 10);
    *buff++ = ((c = (n >> 8) & 15) < 10) ? c + '0' : c + ('A' - 10);
    *buff++ = ((c = (n >> 4) & 15) < 10) ? c + '0' : c + ('A' - 10);
    *buff++ = ((c = n & 15) < 10) ? c + '0' : c + ('A' - 10);
    *buff = '\0';
  }
  return buff;
}

static char * tohex32a(char * buff, unsigned int n)
{
  if (buff) {
    unsigned char c;
    int i;
    for ( i = 7; i >= 0; --i, n >>= 4)
      buff[i] = ((c = n & 15) < 10) ? c + '0' : c + ('A' - 10);
    buff[8] = '\0';
  }
  return buff+8;
}

static char * tohex32b(char * buff, unsigned int n)
{
  static char hexd[] = "0123456789ABCDEF";
  if (buff) {
    int i;
    for ( i = 7; i >= 0; --i, n >>= 4)
      buff[i] = hexd[ n & 15 ];
    buff[8] = '\0';
  }
  return buff+8;
}

static inline char * tohex32c(char * buff, unsigned int n)
{
  static char hexd[] = "0123456789ABCDEF";
  if (buff) {
    *buff++ = hexd[ (n >> 28) & 15 ];
    *buff++ = hexd[ (n >> 24) & 15 ];
    *buff++ = hexd[ (n >> 20) & 15 ];
    *buff++ = hexd[ (n >> 16) & 15 ];
    *buff++ = hexd[ (n >> 12) & 15 ];
    *buff++ = hexd[ (n >> 8) & 15 ];
    *buff++ = hexd[ (n >> 4) & 15 ];
    *buff++ = hexd[ n & 15 ];
    *buff = '\0';
  }
  return buff;
}

static char * tohex32d(char * buff, unsigned int n)
{
  if (buff) {
    unsigned char c;
    *buff++ = ((c = (n >> 28) & 15) < 10) ? c + '0' : c + ('A' - 10);
    *buff++ = ((c = (n >> 24) & 15) < 10) ? c + '0' : c + ('A' - 10);
    *buff++ = ((c = (n >> 20) & 15) < 10) ? c + '0' : c + ('A' - 10);
    *buff++ = ((c = (n >> 16) & 15) < 10) ? c + '0' : c + ('A' - 10);
    *buff++ = ((c = (n >> 12) & 15) < 10) ? c + '0' : c + ('A' - 10);
    *buff++ = ((c = (n >> 8) & 15) < 10) ? c + '0' : c + ('A' - 10);
    *buff++ = ((c = (n >> 4) & 15) < 10) ? c + '0' : c + ('A' - 10);
    *buff++ = ((c = n & 15) < 10) ? c + '0' : c + ('A' - 10);
    *buff = '\0';
  }
  return buff;
}


static inline char * tohex32e(char * buff, unsigned int n)
{
  if (buff) {
    *buff++ = hexd[ (n >> 28) & 15 ];
    *buff++ = hexd[ (n >> 24) & 15 ];
    *buff++ = hexd[ (n >> 20) & 15 ];
    *buff++ = hexd[ (n >> 16) & 15 ];
    *buff++ = hexd[ (n >> 12) & 15 ];
    *buff++ = hexd[ (n >> 8) & 15 ];
    *buff++ = hexd[ (n >> 4) & 15 ];
    *buff++ = hexd[ n & 15 ];
    *buff = '\0';
  }
  return buff;
}

static char hexd[] = "0123456789ABCDEF";
#define tohex32f( bp,  n) \
  if (buff) { \
    *bp++ = hexd[ (n >> 28) & 15 ]; \
    *bp++ = hexd[ (n >> 24) & 15 ]; \
    *bp++ = hexd[ (n >> 20) & 15 ]; \
    *bp++ = hexd[ (n >> 16) & 15 ]; \
    *bp++ = hexd[ (n >> 12) & 15 ]; \
    *bp++ = hexd[ (n >> 8) & 15 ]; \
    *bp++ = hexd[ (n >> 4) & 15 ]; \
    *bp++ = hexd[ n & 15 ]; \
    *bp = '\0'; \
  }

static inline char * tohex32g(char * buff, unsigned int n)
{
  static int hexd[] =  { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  if (buff) {
    *buff++ = hexd[ (n >> 28) & 15 ];
    *buff++ = hexd[ (n >> 24) & 15 ];
    *buff++ = hexd[ (n >> 20) & 15 ];
    *buff++ = hexd[ (n >> 16) & 15 ];
    *buff++ = hexd[ (n >> 12) & 15 ];
    *buff++ = hexd[ (n >> 8) & 15 ];
    *buff++ = hexd[ (n >> 4) & 15 ];
    *buff++ = hexd[ n & 15 ];
    *buff = '\0';
  }
  return buff;
}


int main (int argc, char **argv)
{
  long i;
  struct timeval start, stop;
  struct timezone zone;

  char buff[100];
  char * bufp = buff;

  gettimeofday(&start, &zone);
  for (i = 0; i < 100000000L; i++)
    tohex32a(buff, i);
  gettimeofday(&stop, &zone);
  printf ("tohex32a : %ld msecs\n", (stop.tv_sec*1000L + stop.tv_usec/1000L) - (start.tv_sec*1000L + start.tv_usec/1000L));

  gettimeofday(&start, &zone);
  for (i = 0; i < 100000000L; i++)
    tohex32b(buff, i);
  gettimeofday(&stop, &zone);
  printf ("tohex32b : %ld msecs\n", (stop.tv_sec*1000L + stop.tv_usec/1000L) - (start.tv_sec*1000L + start.tv_usec/1000L));

  gettimeofday(&start, &zone);
  for (i = 0; i < 100000000L; i++)
    tohex32c(buff, i);
  gettimeofday(&stop, &zone);
  printf ("tohex32c : %ld msecs\n", (stop.tv_sec*1000L + stop.tv_usec/1000L) - (start.tv_sec*1000L + start.tv_usec/1000L));

  gettimeofday(&start, &zone);
  for (i = 0; i < 100000000L; i++)
    tohex32d(buff, i);
  gettimeofday(&stop, &zone);
  printf ("tohex32d : %ld msecs\n", (stop.tv_sec*1000L + stop.tv_usec/1000L) - (start.tv_sec*1000L + start.tv_usec/1000L));

 gettimeofday(&start, &zone);
  for (i = 0; i < 100000000L; i++)
     tohex32e(buff, i);
  gettimeofday(&stop, &zone);
  printf ("tohex32e : %ld msecs\n", (stop.tv_sec*1000L + stop.tv_usec/1000L) - (start.tv_sec*1000L + start.tv_usec/1000L));

  gettimeofday(&start, &zone);
  for (i = 0; i < 100000000L; i++) {
    bufp = buff;
    tohex32f(bufp, i);
  }
  gettimeofday(&stop, &zone);
  printf ("tohex32f : %ld msecs\n", (stop.tv_sec*1000L + stop.tv_usec/1000L) - (start.tv_sec*1000L + start.tv_usec/1000L));

  gettimeofday(&start, &zone);
  for (i = 0; i < 100000000L; i++)
    tohex32g(buff, i);
  gettimeofday(&stop, &zone);
  printf ("tohex32g : %ld msecs\n", (stop.tv_sec*1000L + stop.tv_usec/1000L) - (start.tv_sec*1000L + start.tv_usec/1000L));

  return 0;
}



0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 9652140

Wow!  That is a surprise.  I would have expected at least comparable execution times due to caching.

The things you learn just by showing up.   :)


Thanks Brett,
Kent
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month15 days, 15 hours left to enroll

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question