Actually, I took a look with the profiler. I got 2 L2 cache misses.
The rest was L2 cache hits and retired instructions (whatever that means).
Nevertheless, it was faster.
For the sake of it, my current code:
Code:// ---------------------------------------------------------------------------------------------- // Prog_name: comma_sep.c / ver 0.5 // A routine for formatting integer numbers with thousand separators. // Created with Pelles C for Windows 6.00.4 //----------------------------------------------------------------------------------------------- // This version takes into account the sign and the possibility to have different // separator like space, comma, point, and so on. // Moveover this version creates a function that can be tested for performance. // Added the choice for right and left alignment. // In this version Global variables have been removed. //----------------------------------------------------------------------------------------------- // Date: 04 july 2010 // Author: frktons @ cprogramming forum. //----------------------------------------------------------------------------------------------- #include <stdio.h> #include <time.h> #include <math.h> #include <string.h> //----------------------------------------------------------------------------------------------- // Function prototype for int_format(). Takes the number to format, // the address of the string to fill, some switches and thousand separator. // Returns nothing. //----------------------------------------------------------------------------------------------- inline int GetNumLength(int num) { int len_str = 0; int temp_num = num; while (temp_num /= 10) len_str++; return len_str + 1; } void int_format(int num, char * buffer, bool sign, char sep); //----------------------------------------------------------------------------------------------- char digits_table[999999][8]; int main() { for (int i = 0; i < 999999; i++) { for (int j = 0; j < 8; j++) { digits_table[i][0] = ','; digits_table[i][1] = '0' + char(i / 100000); digits_table[i][2] = '0' + char(i / 10000 % 10); digits_table[i][3] = '0' + char(i / 1000 % 10); digits_table[i][4] = ','; digits_table[i][5] = '0' + char(i / 100 % 10); digits_table[i][6] = '0' + char(i / 10 % 10); digits_table[i][7] = '0' + char(i % 10); } } //----------------------------------------------------------------------------------------------- // Local variables. //----------------------------------------------------------------------------------------------- bool sign = true; // if the number has to be displayed with sign // from the function to reflect the state char sep = ','; // here I choose the separator char buffer[30] = {0}; // string array for the formatted number //char alignment = ' '; // choice to [R]ight-align or [L]eft-align the number int num = -1234567890; // test number int cycles = 0; //int len_str = GetNumLength(num); int x; // generic integer counter time_t init_time = 0; // initial time time_t end_time = 0; // end time //alignment = 'L'; // the formatted number will be Left-aligned int times = 500000000; // for testing performance set this number for repetitions printf("\n The value of num is: %d\n",num); time(&init_time); printf("\n init_time = %d \n",init_time); for (cycles = 0; cycles < times; cycles++) { memset(buffer, ' ', sizeof(buffer)); int_format(num, /*len_str, */buffer, sign, sep/*, alignment*/); } // end for printf("\n The formatted value of num is: %s", buffer); printf("\n\n"); time(&end_time); printf(" end_time = %d \n",end_time); printf("\n\n The routine test has taken about %d seconds\n", end_time - init_time); memset(buffer, ' ', sizeof(buffer)); int_format(times, buffer, sign, sep); printf("\n to perform %s cycles of the formatting function\n", buffer); return 0; } //-------------------------------------------------------------------------------------------------------------------- // Function int_format() //-------------------------------------------------------------------------------------------------------------------- __forceinline void int_format(int num, /*int length_of_num, */char *buffer, bool sign, /*bool neg, */char sep/* char alignment*/){ int remain = 0; // integer variable to store the remainder bool neg = false; const int sizeof_buffer = 30; if (num == 0) { strcpy(buffer, "0"); return; } if (num < 0) { neg = true; buffer[0] = '-'; num = -num; // transform number to positive if negative } else { if (sign) buffer[0] = '+'; } int x; for (x = sizeof_buffer - 1; num; x -= 8) { remain = num % 1000000; num /= 1000000; memcpy(&buffer[x - 8], digits_table[remain], 8); } int copy_to = 0; if (neg || sign) copy_to++; memmove(&buffer[copy_to], &buffer[x + 1], sizeof_buffer - x - 1); buffer[sizeof_buffer - x - 1] = '\0'; return; }