Advertisement
Guest User

printdec.c

a guest
Sep 9th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.41 KB | None | 0 0
  1. #include <sys/time.h>
  2. #include <stdio.h>
  3.  
  4. #define N 10000000
  5.  
  6. char buf[N*24]; // more than enough
  7. char *pp = buf;
  8.  
  9. __attribute__ ((noinline))
  10. void myputchar(char ch) {
  11.   *pp++ = ch;
  12. }
  13.  
  14. long long current_timestamp() {
  15.     struct timeval te;
  16.     gettimeofday(&te, NULL); // get current time
  17.     long long milliseconds = te.tv_sec*1000LL + te.tv_usec/1000; // caculate milliseconds
  18.     // printf("milliseconds: %lld\n", milliseconds);
  19.     return milliseconds;
  20. }
  21.  
  22. ///////////////////////////////////////////////////////////////////
  23. //reference - brucehoult (small changes in order to speed it a bit)
  24. ///////////////////////////////////////////////////////////////////
  25.  
  26. void itoc_ref_1(int n) {
  27.   unsigned u = n;
  28.   if (n < 0) {
  29.     //myputchar('-');
  30.     *pp++ = '-';
  31.     u = -n;
  32.   }
  33.   unsigned d = u/10, ch = u - d*10 + '0';
  34.  
  35.   if (d != 0) itoc_ref_1(d);
  36.   //myputchar(ch);
  37.   *pp++ = ch;
  38.  
  39.   *pp = '\0';
  40. }
  41.  
  42. ///////////////////////////////////////////////////////////////////
  43. //ref2 - brucehoult + splitted and modified by sasa (similar noted by hamster_nz)
  44. ///////////////////////////////////////////////////////////////////
  45.  
  46. void itoc_ref_2_uint(unsigned int n) {
  47.  
  48.     unsigned int d = n/10;
  49.     char ch = (char) (n - d*10) + '0';
  50.  
  51.     if (d != 0)
  52.     {
  53.      n =  d;  
  54.      itoc_ref_2_uint(n);
  55.     }
  56.  
  57.     //putchar(ch);
  58.     *pp++ = ch;
  59. }
  60.  
  61. void itoc_ref_2(int n) {
  62.  
  63.   if (n < 0)
  64.   {
  65.      //putchar('-');
  66.      *pp++ = '-';
  67.      n =-n;
  68.   }
  69.  
  70.   itoc_ref_2_uint(n);  
  71.  
  72.   *pp = '\0';
  73. }
  74.  
  75. ///////////////////////////////////////////////////////////////////
  76.  
  77. void itoc_NorthGuy_1(int n) {
  78.   unsigned u, d;
  79.   char ch, *p, buf[12];
  80.  
  81.   u = n;
  82.   if (n < 0) {
  83.     //putchar('-');
  84.     *pp++ = '-';
  85.     u = -n;
  86.   }
  87.  
  88.   p = buf;
  89.   *p++ = 0;
  90.  
  91.   while(1) {
  92.     d = u/10;
  93.     *p++ = u - d*10 + '0';
  94.     if (u = d) continue;
  95.     while (ch = *--p) *pp++=ch;
  96.  
  97.     *pp = '\0';
  98.     return;
  99.   }
  100. }
  101.  
  102. ///////////////////////////////////////////////////////////////////
  103.  
  104. char * itoc_sasa_1(int n, char *s) {
  105.  
  106.   unsigned int u, d;
  107.  
  108.   char *p1, *p2;
  109.   char c;
  110.  
  111.   if (n < 0)
  112.   {
  113.     u = -n;
  114.     *s++ = '-';
  115.   } else
  116.   {
  117.     u = n;
  118.   }
  119.  
  120.   p1 = s;
  121.  
  122.   do {
  123.  
  124.     d = u/10;
  125.     *s++ = (char) (u - d*10) + '0';
  126.  
  127.   } while (u = d);
  128.  
  129.   *s = '\0';
  130.  
  131.   p2 = s-1;
  132.  
  133.   while (p1 < p2)
  134.   {
  135.     c = *p1;
  136.     *p1++ = *p2;
  137.     *p2-- = c;
  138.   }
  139.  
  140.   return s;  
  141.  
  142. }
  143.  
  144. int print_dec1(unsigned n, char*s, int l)
  145. {
  146.     unsigned d = n/10;
  147.     char ch = n - d*10 + '0';
  148.     *s = ch;
  149.    
  150.     if (d == 0)
  151.         return l;
  152.     else
  153.         return print_dec1(d, s + 1, l + 1);
  154. }
  155.  
  156. void reverse(char *a, char *b)
  157. {
  158.     if(a < b)
  159.     {
  160.         char t = *b;
  161.         *b = *a;
  162.         *a = t;
  163.         reverse(a+1, b-1);
  164.     }
  165. }
  166.  
  167. char* print_dec(int n, char *s) {
  168.     int       len = 0;
  169.     unsigned  u   = n;
  170.     char     *r   = s;
  171.    
  172.     if (n < 0) {
  173.         *(r++) = '-';
  174.         u = -n;
  175.         len = 1;
  176.     }    
  177.    
  178.     len += print_dec1(u, r, 0);
  179.     *(s+len+1) = '\0';
  180.    
  181.     reverse(r, s+len);
  182.     return s;
  183. }
  184.  
  185. ///////////////////////////////////////////////////////////////////
  186.  
  187. void process1_old ( void (*f)(int) )
  188. {
  189.   char *p;
  190.  
  191.   printf("\n");
  192.  
  193.   pp = buf;
  194.  
  195.   p=pp;  f(0);           printf("%s\n", p);
  196.   p=pp;  f(123456);      printf("%s\n", p);
  197.   p=pp;  f(-123456);     printf("%s\n", p);
  198.   p=pp;  f( 2147483647); printf("%s\n", p);
  199.   p=pp;  f(-2147483648); printf("%s\n", p);
  200.  
  201. }
  202.  
  203. void process1_new ( char * (*f)(int, char *) )
  204. {
  205.   char b[12], *p;
  206.  
  207.   printf("\n");
  208.  
  209.   p = b;
  210.  
  211.   f(0,p);           printf("%s\n", p);
  212.   f(123456,p);      printf("%s\n", p);
  213.   f(-123456,p);     printf("%s\n", p);
  214.   f( 2147483647,p); printf("%s\n", p);
  215.   f(-2147483648,p); printf("%s\n", p);
  216. }
  217.  
  218. void process2_old ( void (*f)(int), char name[])
  219. {
  220.   long long ti;
  221.  
  222.   pp = buf; ti = current_timestamp();
  223.  
  224.   for (int i=-N; i<=N; ++i)
  225.     f(i);
  226.  
  227.   ti = current_timestamp() - ti;
  228.   printf("%s : %lld ms\n", name, ti);
  229. }
  230.  
  231. void process2_new ( char * (*f)(int, char *), char name[])
  232. {
  233.   long long ti;
  234.  
  235.   pp = buf; ti = current_timestamp();
  236.  
  237.   for (int i=-N; i<=N; ++i)
  238.     pp = f(i,pp);
  239.  
  240.   ti = current_timestamp() - ti;
  241.   printf("%s : %lld ms\n", name, ti);
  242. }
  243.  
  244. int main(void) {
  245.  
  246.   printf("\n"); printf("itoc_ref_1      : "); process1_old(itoc_ref_1);
  247.   printf("\n"); printf("itoc_ref_2      : "); process1_old(itoc_ref_2);
  248.   printf("\n"); printf("itoc_NorthGuy_1 : "); process1_old(itoc_NorthGuy_1);
  249.   printf("\n"); printf("itoc_sasa_1     : "); process1_new(itoc_sasa_1);
  250.   printf("\n"); printf("print_dec       : "); process1_new(print_dec);
  251.  
  252.   printf("\n");
  253.  
  254.   /* process2_old( itoc_ref_1, "Dummy test. Skip this result..." ); */
  255.   /* printf("\n"); */
  256.  
  257.   /* ////////////////////////////////////////// */
  258.   /* for (int i= 0; i<3; ++i) */
  259.   /*   process2_old( itoc_ref_1, "itoc_ref_1     " ); */
  260.  
  261.   /* ////////////////////////////////////////// */
  262.   /* for (int i= 0; i<3; ++i) */
  263.   /*   process2_old( itoc_ref_2, "itoc_ref_2     " ); */
  264.  
  265.   /* ////////////////////////////////////////// */
  266.   /* for (int i= 0; i<3; ++i) */
  267.   /*   process2_old( itoc_NorthGuy_1, "itoc_NorthGuy_1" ); */
  268.  
  269.   //////////////////////////////////////////
  270.   for (int i= 0; i<3; ++i)
  271.     process2_new( itoc_sasa_1, "itoc_sasa_1    " );
  272.  
  273.   //////////////////////////////////////////
  274.   for (int i= 0; i<3; ++i)
  275.     process2_new( print_dec, "print_dec    " );
  276.  
  277.  
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement