Solved

printf() radix conversions

Posted on 2003-10-29
7
622 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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:Kdo
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 125 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:Kdo
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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. …
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

919 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now