Solved

printf() radix conversions

Posted on 2003-10-29
7
620 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

707 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

18 Experts available now in Live!

Get 1:1 Help Now