Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/time.h>
- #include <stdio.h>
- #define N 10000000
- char buf[N*24]; // more than enough
- char *pp = buf;
- __attribute__ ((noinline))
- void myputchar(char ch) {
- *pp++ = ch;
- }
- long long current_timestamp() {
- struct timeval te;
- gettimeofday(&te, NULL); // get current time
- long long milliseconds = te.tv_sec*1000LL + te.tv_usec/1000; // caculate milliseconds
- // printf("milliseconds: %lld\n", milliseconds);
- return milliseconds;
- }
- ///////////////////////////////////////////////////////////////////
- //reference - brucehoult (small changes in order to speed it a bit)
- ///////////////////////////////////////////////////////////////////
- void itoc_ref_1(int n) {
- unsigned u = n;
- if (n < 0) {
- //myputchar('-');
- *pp++ = '-';
- u = -n;
- }
- unsigned d = u/10, ch = u - d*10 + '0';
- if (d != 0) itoc_ref_1(d);
- //myputchar(ch);
- *pp++ = ch;
- *pp = '\0';
- }
- ///////////////////////////////////////////////////////////////////
- //ref2 - brucehoult + splitted and modified by sasa (similar noted by hamster_nz)
- ///////////////////////////////////////////////////////////////////
- void itoc_ref_2_uint(unsigned int n) {
- unsigned int d = n/10;
- char ch = (char) (n - d*10) + '0';
- if (d != 0)
- {
- n = d;
- itoc_ref_2_uint(n);
- }
- //putchar(ch);
- *pp++ = ch;
- }
- void itoc_ref_2(int n) {
- if (n < 0)
- {
- //putchar('-');
- *pp++ = '-';
- n =-n;
- }
- itoc_ref_2_uint(n);
- *pp = '\0';
- }
- ///////////////////////////////////////////////////////////////////
- void itoc_NorthGuy_1(int n) {
- unsigned u, d;
- char ch, *p, buf[12];
- u = n;
- if (n < 0) {
- //putchar('-');
- *pp++ = '-';
- u = -n;
- }
- p = buf;
- *p++ = 0;
- while(1) {
- d = u/10;
- *p++ = u - d*10 + '0';
- if (u = d) continue;
- while (ch = *--p) *pp++=ch;
- *pp = '\0';
- return;
- }
- }
- ///////////////////////////////////////////////////////////////////
- char * itoc_sasa_1(int n, char *s) {
- unsigned int u, d;
- char *p1, *p2;
- char c;
- if (n < 0)
- {
- u = -n;
- *s++ = '-';
- } else
- {
- u = n;
- }
- p1 = s;
- do {
- d = u/10;
- *s++ = (char) (u - d*10) + '0';
- } while (u = d);
- *s = '\0';
- p2 = s-1;
- while (p1 < p2)
- {
- c = *p1;
- *p1++ = *p2;
- *p2-- = c;
- }
- return s;
- }
- int print_dec1(unsigned n, char*s, int l)
- {
- unsigned d = n/10;
- char ch = n - d*10 + '0';
- *s = ch;
- if (d == 0)
- return l;
- else
- return print_dec1(d, s + 1, l + 1);
- }
- void reverse(char *a, char *b)
- {
- if(a < b)
- {
- char t = *b;
- *b = *a;
- *a = t;
- reverse(a+1, b-1);
- }
- }
- char* print_dec(int n, char *s) {
- int len = 0;
- unsigned u = n;
- char *r = s;
- if (n < 0) {
- *(r++) = '-';
- u = -n;
- len = 1;
- }
- len += print_dec1(u, r, 0);
- *(s+len+1) = '\0';
- reverse(r, s+len);
- return s;
- }
- ///////////////////////////////////////////////////////////////////
- void process1_old ( void (*f)(int) )
- {
- char *p;
- printf("\n");
- pp = buf;
- p=pp; f(0); printf("%s\n", p);
- p=pp; f(123456); printf("%s\n", p);
- p=pp; f(-123456); printf("%s\n", p);
- p=pp; f( 2147483647); printf("%s\n", p);
- p=pp; f(-2147483648); printf("%s\n", p);
- }
- void process1_new ( char * (*f)(int, char *) )
- {
- char b[12], *p;
- printf("\n");
- p = b;
- f(0,p); printf("%s\n", p);
- f(123456,p); printf("%s\n", p);
- f(-123456,p); printf("%s\n", p);
- f( 2147483647,p); printf("%s\n", p);
- f(-2147483648,p); printf("%s\n", p);
- }
- void process2_old ( void (*f)(int), char name[])
- {
- long long ti;
- pp = buf; ti = current_timestamp();
- for (int i=-N; i<=N; ++i)
- f(i);
- ti = current_timestamp() - ti;
- printf("%s : %lld ms\n", name, ti);
- }
- void process2_new ( char * (*f)(int, char *), char name[])
- {
- long long ti;
- pp = buf; ti = current_timestamp();
- for (int i=-N; i<=N; ++i)
- pp = f(i,pp);
- ti = current_timestamp() - ti;
- printf("%s : %lld ms\n", name, ti);
- }
- int main(void) {
- printf("\n"); printf("itoc_ref_1 : "); process1_old(itoc_ref_1);
- printf("\n"); printf("itoc_ref_2 : "); process1_old(itoc_ref_2);
- printf("\n"); printf("itoc_NorthGuy_1 : "); process1_old(itoc_NorthGuy_1);
- printf("\n"); printf("itoc_sasa_1 : "); process1_new(itoc_sasa_1);
- printf("\n"); printf("print_dec : "); process1_new(print_dec);
- printf("\n");
- /* process2_old( itoc_ref_1, "Dummy test. Skip this result..." ); */
- /* printf("\n"); */
- /* ////////////////////////////////////////// */
- /* for (int i= 0; i<3; ++i) */
- /* process2_old( itoc_ref_1, "itoc_ref_1 " ); */
- /* ////////////////////////////////////////// */
- /* for (int i= 0; i<3; ++i) */
- /* process2_old( itoc_ref_2, "itoc_ref_2 " ); */
- /* ////////////////////////////////////////// */
- /* for (int i= 0; i<3; ++i) */
- /* process2_old( itoc_NorthGuy_1, "itoc_NorthGuy_1" ); */
- //////////////////////////////////////////
- for (int i= 0; i<3; ++i)
- process2_new( itoc_sasa_1, "itoc_sasa_1 " );
- //////////////////////////////////////////
- for (int i= 0; i<3; ++i)
- process2_new( print_dec, "print_dec " );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement