Help me, a friend of mine wrote a program in C# and I wrote the sameprogram in C..His is faster than

Discussion in 'C Programming' started by c, Dec 12, 2007.

  1. c

    c Guest

    Hi every one,

    Me and my Cousin were talking about C and C#, I love C and he loves
    C#..and were talking C is ...blah blah...C# is Blah Blah ...etc

    and then we decided to write a program that will calculate the
    factorial of 10, 10 millions time and print the reusult in a file with
    the name log.txt..

    I wrote something like this

    #include <stdio.h>
    unsigned int fib (int n);



    int main()
    {
    FILE *fp;
    unsigned int loop =1 ;
    if ( (fp = fopen( "log.txt", "a" )) != NULL )
    for (loop; loop <= 10000000 ; loop++)
    {

    fprintf(fp,"%u\n",fib(10));
    }
    fclose (fp);
    return 0;
    }



    unsigned int fib (int n)
    { if (n != 1 )
    return n * fib(n-1);
    else
    return 1;
    }


    and he did the something in C#


    and then we all have the same laptop..DELL Inspiron 6000.

    I ran my program, I took 18 seconds to get done..his program took 7
    seconds..Wow

    and then I asked him to run my program in his laptop..it's all the
    same ..but I wanted to...I ran it...gave me the same time..

    How come ..?!

    Next day, I tried some Optimization

    and developed the loop and wrote something like this

    for (loop; loop <= 1000000 ; loop++)
    {
    fprintf(fp,"%u\n %u\n %u\n %u\n %u\n
    ",fib(10),fib(10),fib(10),fib(10),fib(10));
    fprintf(fp,"%u\n %u\n %u\n %u\n %u\n
    ",fib(10),fib(10),fib(10),fib(10),fib(10));
    }

    But his program still faster than mine..

    then, I tried the program under Slackware 12....it took 3.8 Seconds to
    get done..Wow, I won the Challenge..

    anyway, he want me to beat him under windows XP...Please guys help me
    out..
     
    c, Dec 12, 2007
    #1
    1. Advertising

  2. In article <>,
    c <> wrote:

    >unsigned int fib (int n)
    > { if (n != 1 )
    > return n * fib(n-1);
    > else
    > return 1;
    > }


    I think "fact" would be a better name for this function.

    >and then we all have the same laptop..DELL Inspiron 6000.
    >
    >I ran my program, I took 18 seconds to get done


    What compiler did you use? What optimisation settings? You can't
    tell much about the relative advantages of the languages just from a
    random figure like that.

    >and developed the loop and wrote something like this
    >
    > for (loop; loop <= 1000000 ; loop++)
    > {
    > fprintf(fp,"%u\n %u\n %u\n %u\n %u\n
    >",fib(10),fib(10),fib(10),fib(10),fib(10));
    > fprintf(fp,"%u\n %u\n %u\n %u\n %u\n
    >",fib(10),fib(10),fib(10),fib(10),fib(10));
    > }


    This is likely to be a pointless change. The loop overhead is small
    compared with time taken for the factorial function.

    Most likely your program would be significantly faster if you used
    a loop to calculate the factorial rather than recursion.

    -- Richard
    --
    :wq
     
    Richard Tobin, Dec 12, 2007
    #2
    1. Advertising

  3. c

    c Guest

    Re: Help me, a friend of mine wrote a program in C# and I wrote thesame program in C..His is faster than mine on the same machine...How Come ?

    On Dec 13, 2:35 am, (Richard Tobin) wrote:
    > In article <>,
    >
    > c <> wrote:
    > >unsigned int fib (int n)
    > > { if (n != 1 )
    > > return n * fib(n-1);
    > > else
    > > return 1;
    > > }

    >
    > I think "fact" would be a better name for this function.
    >
    > >and then we all have the same laptop..DELL Inspiron 6000.

    >
    > >I ran my program, I took 18 seconds to get done

    >
    > What compiler did you use? What optimisation settings? You can't
    > tell much about the relative advantages of the languages just from a
    > random figure like that.
    >
    > >and developed the loop and wrote something like this

    >
    > > for (loop; loop <= 1000000 ; loop++)
    > > {
    > > fprintf(fp,"%u\n %u\n %u\n %u\n %u\n
    > >",fib(10),fib(10),fib(10),fib(10),fib(10));
    > > fprintf(fp,"%u\n %u\n %u\n %u\n %u\n
    > >",fib(10),fib(10),fib(10),fib(10),fib(10));
    > > }

    >
    > This is likely to be a pointless change. The loop overhead is small
    > compared with time taken for the factorial function.
    >
    > Most likely your program would be significantly faster if you used
    > a loop to calculate the factorial rather than recursion.
    >
    > -- Richard
    > --
    > :wq


    I tried Tiny C Compiler, and with Turbo C...
    we both (me and my cousin) used recursion in our programs.


    Than you .
     
    c, Dec 12, 2007
    #3
  4. c

    c Guest

    Re: Help me, a friend of mine wrote a program in C# and I wrote thesame program in C..His is faster than mine on the same machine...How Come ?

    On Dec 13, 2:41 am, c <> wrote:
    > On Dec 13, 2:35 am, (Richard Tobin) wrote:
    >
    >
    >
    > > In article <>,

    >
    > > c <> wrote:
    > > >unsigned int fib (int n)
    > > > { if (n != 1 )
    > > > return n * fib(n-1);
    > > > else
    > > > return 1;
    > > > }

    >
    > > I think "fact" would be a better name for this function.

    >
    > > >and then we all have the same laptop..DELL Inspiron 6000.

    >
    > > >I ran my program, I took 18 seconds to get done

    >
    > > What compiler did you use? What optimisation settings? You can't
    > > tell much about the relative advantages of the languages just from a
    > > random figure like that.

    >
    > > >and developed the loop and wrote something like this

    >
    > > > for (loop; loop <= 1000000 ; loop++)
    > > > {
    > > > fprintf(fp,"%u\n %u\n %u\n %u\n %u\n
    > > >",fib(10),fib(10),fib(10),fib(10),fib(10));
    > > > fprintf(fp,"%u\n %u\n %u\n %u\n %u\n
    > > >",fib(10),fib(10),fib(10),fib(10),fib(10));
    > > > }

    >
    > > This is likely to be a pointless change. The loop overhead is small
    > > compared with time taken for the factorial function.

    >
    > > Most likely your program would be significantly faster if you used
    > > a loop to calculate the factorial rather than recursion.

    >
    > > -- Richard
    > > --
    > > :wq

    >
    > I tried Tiny C Compiler, and with Turbo C...
    > we both (me and my cousin) used recursion in our programs.
    >
    > Than you .


    One more thing, Why under Slackware with GCC..my program goes faster ?
    I test it with time

    [slackware] time ./fact
     
    c, Dec 12, 2007
    #4
  5. c

    Guest

    Re: Help me, a friend of mine wrote a program in C# and I wrote thesame program in C..His is faster than mine on the same machine...How Come ?

    On Dec 12, 11:45 pm, c <> wrote:
    > On Dec 13, 2:41 am, c <> wrote:
    >
    >
    >
    > > On Dec 13, 2:35 am, (Richard Tobin) wrote:

    >
    > > > In article <>,

    >
    > > > c <> wrote:
    > > > >unsigned int fib (int n)
    > > > > { if (n != 1 )
    > > > > return n * fib(n-1);
    > > > > else
    > > > > return 1;
    > > > > }

    >
    > > > I think "fact" would be a better name for this function.

    >
    > > > >and then we all have the same laptop..DELL Inspiron 6000.

    >
    > > > >I ran my program, I took 18 seconds to get done

    >
    > > > What compiler did you use? What optimisation settings? You can't
    > > > tell much about the relative advantages of the languages just from a
    > > > random figure like that.

    >
    > > > >and developed the loop and wrote something like this

    >
    > > > > for (loop; loop <= 1000000 ; loop++)
    > > > > {
    > > > > fprintf(fp,"%u\n %u\n %u\n %u\n %u\n
    > > > >",fib(10),fib(10),fib(10),fib(10),fib(10));
    > > > > fprintf(fp,"%u\n %u\n %u\n %u\n %u\n
    > > > >",fib(10),fib(10),fib(10),fib(10),fib(10));
    > > > > }

    >
    > > > This is likely to be a pointless change. The loop overhead is small
    > > > compared with time taken for the factorial function.

    >
    > > > Most likely your program would be significantly faster if you used
    > > > a loop to calculate the factorial rather than recursion.

    >
    > > > -- Richard
    > > > --
    > > > :wq

    >
    > > I tried Tiny C Compiler, and with Turbo C...
    > > we both (me and my cousin) used recursion in our programs.

    >
    > > Than you .

    >
    > One more thing, Why under Slackware with GCC..my program goes faster ?
    > I test it with time
    >
    > [slackware] time ./fact


    The answer to your question is 42.
     
    , Dec 12, 2007
    #5
  6. c

    user923005 Guest

    Re: Help me, a friend of mine wrote a program in C# and I wrote thesame program in C..His is faster than mine on the same machine...How Come ?

    On Dec 12, 3:23 pm, c <> wrote:
    > Hi every one,
    >
    > Me and my Cousin were talking about C and C#, I love C and he loves
    > C#..and were talking C is ...blah blah...C# is Blah Blah ...etc
    >
    > and then we decided to write a program that will calculate the
    > factorial of 10, 10 millions time and print the reusult in a file with
    > the name log.txt..
    >
    > I wrote something like this
    >
    > #include <stdio.h>
    > unsigned int fib (int n);
    >
    > int main()
    > {
    > FILE *fp;
    > unsigned int loop =1 ;
    > if ( (fp = fopen( "log.txt", "a" )) != NULL )
    > for (loop; loop <= 10000000 ; loop++)
    > {
    >
    > fprintf(fp,"%u\n",fib(10));
    > }
    > fclose (fp);
    > return 0;
    >
    > }
    >
    > unsigned int fib (int n)
    > { if (n != 1 )
    > return n * fib(n-1);
    > else
    > return 1;
    > }
    >
    > and he did the something in C#
    >
    > and then we all have the same laptop..DELL Inspiron 6000.
    >
    > I ran my program, I took 18 seconds to get done..his program took 7
    > seconds..Wow
    >
    > and then I asked him to run my program in his laptop..it's all the
    > same ..but I wanted to...I ran it...gave me the same time..
    >
    > How come ..?!
    >
    > Next day, I tried some Optimization
    >
    > and developed the loop and wrote something like this
    >
    > for (loop; loop <= 1000000 ; loop++)
    > {
    > fprintf(fp,"%u\n %u\n %u\n %u\n %u\n
    > ",fib(10),fib(10),fib(10),fib(10),fib(10));
    > fprintf(fp,"%u\n %u\n %u\n %u\n %u\n
    > ",fib(10),fib(10),fib(10),fib(10),fib(10));
    > }
    >
    > But his program still faster than mine..
    >
    > then, I tried the program under Slackware 12....it took 3.8 Seconds to
    > get done..Wow, I won the Challenge..
    >
    > anyway, he want me to beat him under windows XP...Please guys help me
    > out..


    The recursive factorial function will be a lot slower than an
    iterative one.
    But the lion's share of the time is going to be in writing out the
    text file.
    It occupies 160 MB on my machine. It appears that your friend has a
    faster disk than you do.
    This might be marginally faster:

    #include <stdio.h>

    unsigned fact(unsigned n)
    {
    unsigned result = 1;
    while (n-- > 1)
    result *= n;
    return result;
    }

    int main()
    {
    FILE *fp;
    unsigned loop;
    if ((fp = fopen("log.txt", "a")) != NULL) {
    setvbuf(fp, NULL, _IOFBF, 16000);
    for (loop=1; loop <= 10000000; loop++) {
    fprintf(fp, "%u\n", fact(10));
    }
    fclose(fp);
    }
    return 0;
    }

    This program executes in less than one second on my machine (showing
    that the time is almost exclusively I/O):

    #include <stdio.h>

    unsigned fact(unsigned n)
    {
    unsigned result = 1;
    while (n-- > 1)
    result *= n;
    return result;
    }

    int main()
    {
    FILE *fp;
    unsigned loop;
    double sum = 0;
    if ((fp = fopen("log.txt", "a")) != NULL) {
    setvbuf(fp, NULL, _IOFBF, 16000);
    for (loop=1; loop <= 10000000; loop++) {
    sum += fact(10);
    }
    printf("sum was %f\n", sum);
    fclose(fp);
    }
    return 0;
    }
    /*
    C:\tmp>foo
    sum was 3628800000000.000000
    */
     
    user923005, Dec 12, 2007
    #6
  7. c

    c Guest

    Re: Help me, a friend of mine wrote a program in C# and I wrote thesame program in C..His is faster than mine on the same machine...How Come ?

    On Dec 13, 2:54 am, user923005 <> wrote:
    > On Dec 12, 3:23 pm, c <> wrote:
    >
    >
    >
    > > Hi every one,

    >
    > > Me and my Cousin were talking about C and C#, I love C and he loves
    > > C#..and were talking C is ...blah blah...C# is Blah Blah ...etc

    >
    > > and then we decided to write a program that will calculate the
    > > factorial of 10, 10 millions time and print the reusult in a file with
    > > the name log.txt..

    >
    > > I wrote something like this

    >
    > > #include <stdio.h>
    > > unsigned int fib (int n);

    >
    > > int main()
    > > {
    > > FILE *fp;
    > > unsigned int loop =1 ;
    > > if ( (fp = fopen( "log.txt", "a" )) != NULL )
    > > for (loop; loop <= 10000000 ; loop++)
    > > {

    >
    > > fprintf(fp,"%u\n",fib(10));
    > > }
    > > fclose (fp);
    > > return 0;

    >
    > > }

    >
    > > unsigned int fib (int n)
    > > { if (n != 1 )
    > > return n * fib(n-1);
    > > else
    > > return 1;
    > > }

    >
    > > and he did the something in C#

    >
    > > and then we all have the same laptop..DELL Inspiron 6000.

    >
    > > I ran my program, I took 18 seconds to get done..his program took 7
    > > seconds..Wow

    >
    > > and then I asked him to run my program in his laptop..it's all the
    > > same ..but I wanted to...I ran it...gave me the same time..

    >
    > > How come ..?!

    >
    > > Next day, I tried some Optimization

    >
    > > and developed the loop and wrote something like this

    >
    > > for (loop; loop <= 1000000 ; loop++)
    > > {
    > > fprintf(fp,"%u\n %u\n %u\n %u\n %u\n
    > > ",fib(10),fib(10),fib(10),fib(10),fib(10));
    > > fprintf(fp,"%u\n %u\n %u\n %u\n %u\n
    > > ",fib(10),fib(10),fib(10),fib(10),fib(10));
    > > }

    >
    > > But his program still faster than mine..

    >
    > > then, I tried the program under Slackware 12....it took 3.8 Seconds to
    > > get done..Wow, I won the Challenge..

    >
    > > anyway, he want me to beat him under windows XP...Please guys help me
    > > out..

    >
    > The recursive factorial function will be a lot slower than an
    > iterative one.
    > But the lion's share of the time is going to be in writing out the
    > text file.
    > It occupies 160 MB on my machine. It appears that your friend has a
    > faster disk than you do.
    > This might be marginally faster:
    >
    > #include <stdio.h>
    >
    > unsigned fact(unsigned n)
    > {
    > unsigned result = 1;
    > while (n-- > 1)
    > result *= n;
    > return result;
    >
    > }
    >
    > int main()
    > {
    > FILE *fp;
    > unsigned loop;
    > if ((fp = fopen("log.txt", "a")) != NULL) {
    > setvbuf(fp, NULL, _IOFBF, 16000);
    > for (loop=1; loop <= 10000000; loop++) {
    > fprintf(fp, "%u\n", fact(10));
    > }
    > fclose(fp);
    > }
    > return 0;
    >
    > }
    >
    > This program executes in less than one second on my machine (showing
    > that the time is almost exclusively I/O):
    >
    > #include <stdio.h>
    >
    > unsigned fact(unsigned n)
    > {
    > unsigned result = 1;
    > while (n-- > 1)
    > result *= n;
    > return result;
    >
    > }
    >
    > int main()
    > {
    > FILE *fp;
    > unsigned loop;
    > double sum = 0;
    > if ((fp = fopen("log.txt", "a")) != NULL) {
    > setvbuf(fp, NULL, _IOFBF, 16000);
    > for (loop=1; loop <= 10000000; loop++) {
    > sum += fact(10);
    > }
    > printf("sum was %f\n", sum);
    > fclose(fp);
    > }
    > return 0;}
    >
    > /*
    > C:\tmp>foo
    > sum was 3628800000000.000000
    > */


    Thanks sir for you reply..
    you mentioned "It appears that your friend has a
    faster disk than you do"
    We both have the same laptop..same model..anyway, I tested my program
    in his laptop just in case..
    anyway, I compiled the code you posted..its save a 0-byte text file on
    machine..
    I will try with another compiler..I'll get back to you..
     
    c, Dec 13, 2007
    #7
  8. c

    c Guest

    Re: Help me, a friend of mine wrote a program in C# and I wrote thesame program in C..His is faster than mine on the same machine...How Come ?

    On Dec 13, 2:51 am, wrote:
    > On Dec 12, 11:45 pm, c <> wrote:
    >
    >
    >
    > > On Dec 13, 2:41 am, c <> wrote:

    >
    > > > On Dec 13, 2:35 am, (Richard Tobin) wrote:

    >
    > > > > In article <>,

    >
    > > > > c <> wrote:
    > > > > >unsigned int fib (int n)
    > > > > > { if (n != 1 )
    > > > > > return n * fib(n-1);
    > > > > > else
    > > > > > return 1;
    > > > > > }

    >
    > > > > I think "fact" would be a better name for this function.

    >
    > > > > >and then we all have the same laptop..DELL Inspiron 6000.

    >
    > > > > >I ran my program, I took 18 seconds to get done

    >
    > > > > What compiler did you use? What optimisation settings? You can't
    > > > > tell much about the relative advantages of the languages just from a
    > > > > random figure like that.

    >
    > > > > >and developed the loop and wrote something like this

    >
    > > > > > for (loop; loop <= 1000000 ; loop++)
    > > > > > {
    > > > > > fprintf(fp,"%u\n %u\n %u\n %u\n %u\n
    > > > > >",fib(10),fib(10),fib(10),fib(10),fib(10));
    > > > > > fprintf(fp,"%u\n %u\n %u\n %u\n %u\n
    > > > > >",fib(10),fib(10),fib(10),fib(10),fib(10));
    > > > > > }

    >
    > > > > This is likely to be a pointless change. The loop overhead is small
    > > > > compared with time taken for the factorial function.

    >
    > > > > Most likely your program would be significantly faster if you used
    > > > > a loop to calculate the factorial rather than recursion.

    >
    > > > > -- Richard
    > > > > --
    > > > > :wq

    >
    > > > I tried Tiny C Compiler, and with Turbo C...
    > > > we both (me and my cousin) used recursion in our programs.

    >
    > > > Than you .

    >
    > > One more thing, Why under Slackware with GCC..my program goes faster ?
    > > I test it with time

    >
    > > [slackware] time ./fact

    >
    > The answer to your question is 42.


    What do you mean by 42..please give me more details..
     
    c, Dec 13, 2007
    #8
  9. c

    pete Guest

    c wrote:
    >
    > Hi every one,
    >
    > Me and my Cousin were talking about C and C#, I love C and he loves
    > C#..and were talking C is ...blah blah...C# is Blah Blah ...etc
    >
    > and then we decided to write a program that will calculate the
    > factorial of 10, 10 millions time and print the reusult in a file with
    > the name log.txt..
    >
    > I wrote something like this
    >
    > #include <stdio.h>
    > unsigned int fib (int n);
    >
    > int main()
    > {
    > FILE *fp;
    > unsigned int loop =1 ;
    > if ( (fp = fopen( "log.txt", "a" )) != NULL )
    > for (loop; loop <= 10000000 ; loop++)
    > {
    >
    > fprintf(fp,"%u\n",fib(10));
    > }
    > fclose (fp);
    > return 0;
    > }
    >
    > unsigned int fib (int n)
    > { if (n != 1 )
    > return n * fib(n-1);
    > else
    > return 1;
    > }
    >
    > and he did the something in C#
    >
    > and then we all have the same laptop..DELL Inspiron 6000.
    >
    > I ran my program, I took 18 seconds to get done..his program took 7
    > seconds..Wow
    >
    > and then I asked him to run my program in his laptop..it's all the
    > same ..but I wanted to...I ran it...gave me the same time..
    >
    > How come ..?!
    >
    > Next day, I tried some Optimization
    >
    > and developed the loop and wrote something like this
    >
    > for (loop; loop <= 1000000 ; loop++)
    > {
    > fprintf(fp,"%u\n %u\n %u\n %u\n %u\n
    > ",fib(10),fib(10),fib(10),fib(10),fib(10));
    > fprintf(fp,"%u\n %u\n %u\n %u\n %u\n
    > ",fib(10),fib(10),fib(10),fib(10),fib(10));
    > }
    >
    > But his program still faster than mine..
    >
    > then, I tried the program under Slackware 12....it took 3.8 Seconds to
    > get done..Wow, I won the Challenge..
    >
    > anyway, he want me to beat him under windows XP...Please guys help me
    > out..


    My first guess is that fprintf is your slowest dog.
    The second most obvious improvement would be to
    remove recursion from your fib function.

    Your program runs in 19 cursor blinks on my machine.
    My program, new.c, runs in 11 cursor blinks.

    /* BEGIN new.c */

    #include <stdio.h>
    #include <limits.h>

    long unsigned fib(int n);
    void lutoa(long unsigned n, char *s);

    int main(void)
    {
    FILE *fp;
    long unsigned loop = 10000000;
    char lutoa_buff[(sizeof(long) * CHAR_BIT ) / 3 + 1];

    if ((fp = fopen("log.txt", "w")) != NULL) {
    do {
    lutoa(fib(10), lutoa_buff);
    fputs(lutoa_buff, fp);
    putc('\n', fp);
    } while (--loop != 0);
    fclose (fp);
    }
    return 0;
    }

    long unsigned fib(int n)
    {
    long unsigned r = 1;

    while (n > 1) {
    r *= n--;
    }
    return r;
    }

    void lutoa(long unsigned n, char *s)
    {
    long unsigned tenth;
    char *p, swap;

    p = s;
    tenth = n;
    do {
    tenth /= 10;
    *p++ = (char)(n - 10 * tenth + '0');
    n = tenth;
    } while (tenth != 0);
    *p-- = '\0';
    while (p > s) {
    swap = *s;
    *s++ = *p;
    *p-- = swap;
    }
    }

    /* END new.c */

    --
    pete
     
    pete, Dec 13, 2007
    #9
  10. c

    c Guest

    Re: Help me, a friend of mine wrote a program in C# and I wrote thesame program in C..His is faster than mine on the same machine...How Come ?

    On Dec 13, 3:07 am, pete <> wrote:
    > c wrote:
    >
    > > Hi every one,

    >
    > > Me and my Cousin were talking about C and C#, I love C and he loves
    > > C#..and were talking C is ...blah blah...C# is Blah Blah ...etc

    >
    > > and then we decided to write a program that will calculate the
    > > factorial of 10, 10 millions time and print the reusult in a file with
    > > the name log.txt..

    >
    > > I wrote something like this

    >
    > > #include <stdio.h>
    > > unsigned int fib (int n);

    >
    > > int main()
    > > {
    > > FILE *fp;
    > > unsigned int loop =1 ;
    > > if ( (fp = fopen( "log.txt", "a" )) != NULL )
    > > for (loop; loop <= 10000000 ; loop++)
    > > {

    >
    > > fprintf(fp,"%u\n",fib(10));
    > > }
    > > fclose (fp);
    > > return 0;
    > > }

    >
    > > unsigned int fib (int n)
    > > { if (n != 1 )
    > > return n * fib(n-1);
    > > else
    > > return 1;
    > > }

    >
    > > and he did the something in C#

    >
    > > and then we all have the same laptop..DELL Inspiron 6000.

    >
    > > I ran my program, I took 18 seconds to get done..his program took 7
    > > seconds..Wow

    >
    > > and then I asked him to run my program in his laptop..it's all the
    > > same ..but I wanted to...I ran it...gave me the same time..

    >
    > > How come ..?!

    >
    > > Next day, I tried some Optimization

    >
    > > and developed the loop and wrote something like this

    >
    > > for (loop; loop <= 1000000 ; loop++)
    > > {
    > > fprintf(fp,"%u\n %u\n %u\n %u\n %u\n
    > > ",fib(10),fib(10),fib(10),fib(10),fib(10));
    > > fprintf(fp,"%u\n %u\n %u\n %u\n %u\n
    > > ",fib(10),fib(10),fib(10),fib(10),fib(10));
    > > }

    >
    > > But his program still faster than mine..

    >
    > > then, I tried the program under Slackware 12....it took 3.8 Seconds to
    > > get done..Wow, I won the Challenge..

    >
    > > anyway, he want me to beat him under windows XP...Please guys help me
    > > out..

    >
    > My first guess is that fprintf is your slowest dog.
    > The second most obvious improvement would be to
    > remove recursion from your fib function.
    >
    > Your program runs in 19 cursor blinks on my machine.
    > My program, new.c, runs in 11 cursor blinks.
    >
    > /* BEGIN new.c */
    >
    > #include <stdio.h>
    > #include <limits.h>
    >
    > long unsigned fib(int n);
    > void lutoa(long unsigned n, char *s);
    >
    > int main(void)
    > {
    > FILE *fp;
    > long unsigned loop = 10000000;
    > char lutoa_buff[(sizeof(long) * CHAR_BIT ) / 3 + 1];
    >
    > if ((fp = fopen("log.txt", "w")) != NULL) {
    > do {
    > lutoa(fib(10), lutoa_buff);
    > fputs(lutoa_buff, fp);
    > putc('\n', fp);
    > } while (--loop != 0);
    > fclose (fp);
    > }
    > return 0;
    >
    > }
    >
    > long unsigned fib(int n)
    > {
    > long unsigned r = 1;
    >
    > while (n > 1) {
    > r *= n--;
    > }
    > return r;
    >
    > }
    >
    > void lutoa(long unsigned n, char *s)
    > {
    > long unsigned tenth;
    > char *p, swap;
    >
    > p = s;
    > tenth = n;
    > do {
    > tenth /= 10;
    > *p++ = (char)(n - 10 * tenth + '0');
    > n = tenth;
    > } while (tenth != 0);
    > *p-- = '\0';
    > while (p > s) {
    > swap = *s;
    > *s++ = *p;
    > *p-- = swap;
    > }
    >
    > }
    >
    > /* END new.c */
    >
    > --
    > pete


    thanks pete...
    But I gotta use recursion..It's not fair..My friend uses recursion in
    his program, the program that he wrote in C#..So I have to :)
    otherwise, I am cheating.

    Thank you very much.
     
    c, Dec 13, 2007
    #10
  11. c

    pete Guest

    Re: Help me, a friend of mine wrote a program in C# and I wrote thesame program in C..His is faster than mine on the same machine...How Come ?

    c wrote:
    >
    > On Dec 13, 3:07 am, pete <> wrote:


    > > My first guess is that fprintf is your slowest dog.
    > > The second most obvious improvement would be to
    > > remove recursion from your fib function.
    > >
    > > Your program runs in 19 cursor blinks on my machine.
    > > My program, new.c, runs in 11 cursor blinks.


    > thanks pete...
    > But I gotta use recursion..It's not fair..My friend uses recursion in
    > his program, the program that he wrote in C#..So I have to :)
    > otherwise, I am cheating.


    I put the recursion back in and timed it again.
    It's still just as fast.
    Why didn't you try that first yourself?

    /* BEGIN new.c */

    #include <stdio.h>
    #include <limits.h>

    long unsigned fib(int n);
    void lutoa(long unsigned n, char *s);

    int main(void)
    {
    FILE *fp;
    long unsigned loop = 10000000;
    char lutoa_buff[(sizeof(long) * CHAR_BIT ) / 3 + 1];

    if ((fp = fopen("log.txt", "w")) != NULL) {
    do {
    lutoa(fib(10), lutoa_buff);
    fputs(lutoa_buff, fp);
    putc('\n', fp);
    } while (--loop != 0);
    fclose (fp);
    }
    return 0;
    }

    long unsigned fib(int n)
    {
    return n != 1 ? n * fib(n - 1) : 1;
    }

    void lutoa(long unsigned n, char *s)
    {
    long unsigned tenth;
    char *p, swap;

    p = s;
    tenth = n;
    do {
    tenth /= 10;
    *p++ = (char)(n - 10 * tenth + '0');
    n = tenth;
    } while (tenth != 0);
    *p-- = '\0';
    while (p > s) {
    swap = *s;
    *s++ = *p;
    *p-- = swap;
    }
    }

    /* END new.c */


    --
    pete
     
    pete, Dec 13, 2007
    #11
  12. c

    Default User Guest

    Re: Help me, a friend of mine wrote a program in C# and I wrote the same program in C..His is faster than mine on the same machine...How Come ?

    c wrote:

    > On Dec 13, 2:51 am, wrote:


    > > The answer to your question is 42.

    >
    > What do you mean by 42..please give me more details..


    <http://en.wikipedia.org/wiki/Answer_to_Life,_the_Universe,_and_Ever
    ything>



    Brian
     
    Default User, Dec 13, 2007
    #12
  13. c

    c Guest

    Re: Help me, a friend of mine wrote a program in C# and I wrote thesame program in C..His is faster than mine on the same machine...How Come ?

    On Dec 13, 3:43 am, pete <> wrote:
    > c wrote:
    >
    > > On Dec 13, 3:07 am, pete <> wrote:
    > > > My first guess is that fprintf is your slowest dog.
    > > > The second most obvious improvement would be to
    > > > remove recursion from your fib function.

    >
    > > > Your program runs in 19 cursor blinks on my machine.
    > > > My program, new.c, runs in 11 cursor blinks.

    > > thanks pete...
    > > But I gotta use recursion..It's not fair..My friend uses recursion in
    > > his program, the program that he wrote in C#..So I have to :)
    > > otherwise, I am cheating.

    >
    > I put the recursion back in and timed it again.
    > It's still just as fast.
    > Why didn't you try that first yourself?
    >
    > /* BEGIN new.c */
    >
    > #include <stdio.h>
    > #include <limits.h>
    >
    > long unsigned fib(int n);
    > void lutoa(long unsigned n, char *s);
    >
    > int main(void)
    > {
    > FILE *fp;
    > long unsigned loop = 10000000;
    > char lutoa_buff[(sizeof(long) * CHAR_BIT ) / 3 + 1];
    >
    > if ((fp = fopen("log.txt", "w")) != NULL) {
    > do {
    > lutoa(fib(10), lutoa_buff);
    > fputs(lutoa_buff, fp);
    > putc('\n', fp);
    > } while (--loop != 0);
    > fclose (fp);
    > }
    > return 0;
    >
    > }
    >
    > long unsigned fib(int n)
    > {
    > return n != 1 ? n * fib(n - 1) : 1;
    >
    > }
    >
    > void lutoa(long unsigned n, char *s)
    > {
    > long unsigned tenth;
    > char *p, swap;
    >
    > p = s;
    > tenth = n;
    > do {
    > tenth /= 10;
    > *p++ = (char)(n - 10 * tenth + '0');
    > n = tenth;
    > } while (tenth != 0);
    > *p-- = '\0';
    > while (p > s) {
    > swap = *s;
    > *s++ = *p;
    > *p-- = swap;
    > }
    >
    > }
    >
    > /* END new.c */
    >
    > --
    > pete


    Thank you very much, that's very sweet of you.
     
    c, Dec 13, 2007
    #13
  14. c

    c Guest

    Re: Help me, a friend of mine wrote a program in C# and I wrote thesame program in C..His is faster than mine on the same machine...How Come ?

    On Dec 13, 3:46 am, "Default User" <> wrote:
    > c wrote:
    > > On Dec 13, 2:51 am, wrote:
    > > > The answer to your question is 42.

    >
    > > What do you mean by 42..please give me more details..

    >
    > <http://en.wikipedia.org/wiki/Answer_to_Life,_the_Universe,_and_Ever
    > ything>
    >
    > Brian


    still don't know that do you guys mean by 42!!
    !
    !!
    !!!
     
    c, Dec 13, 2007
    #14
  15. c

    Tor Rustad Guest

    Re: Help me, a friend of mine wrote a program in C# and I wrote thesame program in C..His is faster than mine on the same machine...How Come?

    c wrote:
    > Hi every one,
    >
    > Me and my Cousin were talking about C and C#, I love C and he loves
    > C#..and were talking C is ...blah blah...C# is Blah Blah ...etc
    >
    > and then we decided to write a program that will calculate the
    > factorial of 10, 10 millions time and print the reusult in a file with
    > the name log.txt.


    Methinks, you are not really measuring program speed, but I/O performance.

    You was printing '3628800' 10 million times, that's only 7 bytes per I/O.

    In C, try using fwrite() instead, and issue only one I/O per 4 kb, even
    better should be one I/O per 64 kb. If doing that, I guess you drop
    below 2 seconds on XP.

    --
    Tor < | tr i-za-h a-z>
     
    Tor Rustad, Dec 13, 2007
    #15
  16. c

    Doug Guest

    Re: Help me, a friend of mine wrote a program in C# and I wrote thesame program in C..His is faster than mine on the same machine...How Come ?

    On Dec 13, 12:27 am, c <> wrote:

    > > c wrote:

    >
    > > > Hi every one,

    >
    > > > Me and my Cousin were talking about C and C#, I love C and he loves
    > > > C#..and were talking C is ...blah blah...C# is Blah Blah ...etc


    <snip>

    My friend uses recursion in
    > his program, the program that he wrote in C#..So I have to :)
    > otherwise, I am cheating.


    Hi c,

    At the risk of starting yet another flame war on topicality... This
    is all OT here, and you'd be better asking...mmm, I'm really not sure
    actually. A windows comp group, maybe.

    Have you checked the compiler settings you used when you compiled on
    XP? (Richard Tobin mentioned this above, not sure if you spotted it.)

    The fib() function you list (which, as others have said, should
    probably be called fact()!) is an excellent candidate for
    optimisation. Google for "tail recursion". If you turn up the
    optimisation on your compiler then this might kick in and help a bit.

    But I think that might be cheating too. AFAIK, the current C#
    compiler doesn't use this optimisation. (The CLR supports it, but the
    C# compiler doesn't yet emit it.) So I don't think your cousin's
    program benefits from this. (Don't take my word as gospel.)

    But as others have stated, it actually looks like it's the IO that's
    being compared at the moment. I guess you're really comparing your c
    standard lib implementation against the CLR, in one particular use
    case.

    Which is interesting, but probably not what you and your cousin set
    out to compare. Maybe you and your cousin could come up with a range
    of other tests - like purely compute-bound (crunch some matrices or
    calculate some primes) or IO-bound (copy some files).

    Doug
     
    Doug, Dec 13, 2007
    #16
  17. c

    Ian Collins Guest

    Re: Help me, a friend of mine wrote a program in C# and I wrote thesame program in C..His is faster than mine on the same machine...How Come?

    c wrote:
    > Hi every one,
    >
    > Me and my Cousin were talking about C and C#, I love C and he loves
    > C#..and were talking C is ...blah blah...C# is Blah Blah ...etc
    >
    > and then we all have the same laptop..DELL Inspiron 6000.
    >
    > I ran my program, I took 18 seconds to get done..his program took 7
    > seconds..Wow
    >

    Must be down to your compiler or options, your code ran in 3 seconds on
    my laptop (Dell M60, same CPU as the Inspiron 6000) and 1 second on my
    desktop...

    --
    Ian Collins.
     
    Ian Collins, Dec 13, 2007
    #17
  18. c

    Gene Guest

    Re: Help me, a friend of mine wrote a program in C# and I wrote thesame program in C..His is faster than mine on the same machine...How Come ?

    On Dec 12, 6:23 pm, c <> wrote:
    > Hi every one,
    >
    > Me and my Cousin were talking about C and C#, I love C and he loves
    > C#..and were talking C is ...blah blah...C# is Blah Blah ...etc
    >
    > and then we decided to write a program that will calculate the
    > factorial of 10, 10 millions time and print the reusult in a file with
    > the name log.txt..
    >


    [ code elided ]

    The two compilers you mentioned don't do much code optimization. On
    the other hand, the MS C# compiler has a huge investment in the latest
    technologies. If you use, say, Visual C++ 2005 Express Edition (which
    I mention only because it's freely downloadable) and highest
    optimization settings, the difference ought to be much smaller. Which
    will be faster will depend on unpredictable details. For example,
    some very aggressive optimizers will inline-expand a recursive
    function in nested fashion a few times a la loop unrolling. This can
    lead to very big speedups for codes like yours. Don't know if C# does
    that. I don't think VC++ EE does.
     
    Gene, Dec 13, 2007
    #18
  19. c

    user923005 Guest

    Re: Help me, a friend of mine wrote a program in C# and I wrote thesame program in C..His is faster than mine on the same machine...How Come ?

    On Dec 12, 4:05 pm, c <> wrote:
    > On Dec 13, 2:54 am, user923005 <> wrote:
    >
    >
    >
    >
    >
    > > On Dec 12, 3:23 pm, c <> wrote:

    >
    > > > Hi every one,

    >
    > > > Me and my Cousin were talking about C and C#, I love C and he loves
    > > > C#..and were talking C is ...blah blah...C# is Blah Blah ...etc

    >
    > > > and then we decided to write a program that will calculate the
    > > > factorial of 10, 10 millions time and print the reusult in a file with
    > > > the name log.txt..

    >
    > > > I wrote something like this

    >
    > > > #include <stdio.h>
    > > > unsigned int fib (int n);

    >
    > > > int main()
    > > > {
    > > > FILE *fp;
    > > > unsigned int loop =1 ;
    > > > if ( (fp = fopen( "log.txt", "a" )) != NULL )
    > > > for (loop; loop <= 10000000 ; loop++)
    > > > {

    >
    > > > fprintf(fp,"%u\n",fib(10));
    > > > }
    > > > fclose (fp);
    > > > return 0;

    >
    > > > }

    >
    > > > unsigned int fib (int n)
    > > > { if (n != 1 )
    > > > return n * fib(n-1);
    > > > else
    > > > return 1;
    > > > }

    >
    > > > and he did the something in C#

    >
    > > > and then we all have the same laptop..DELL Inspiron 6000.

    >
    > > > I ran my program, I took 18 seconds to get done..his program took 7
    > > > seconds..Wow

    >
    > > > and then I asked him to run my program in his laptop..it's all the
    > > > same ..but I wanted to...I ran it...gave me the same time..

    >
    > > > How come ..?!

    >
    > > > Next day, I tried some Optimization

    >
    > > > and developed the loop and wrote something like this

    >
    > > > for (loop; loop <= 1000000 ; loop++)
    > > > {
    > > > fprintf(fp,"%u\n %u\n %u\n %u\n %u\n
    > > > ",fib(10),fib(10),fib(10),fib(10),fib(10));
    > > > fprintf(fp,"%u\n %u\n %u\n %u\n %u\n
    > > > ",fib(10),fib(10),fib(10),fib(10),fib(10));
    > > > }

    >
    > > > But his program still faster than mine..

    >
    > > > then, I tried the program under Slackware 12....it took 3.8 Seconds to
    > > > get done..Wow, I won the Challenge..

    >
    > > > anyway, he want me to beat him under windows XP...Please guys help me
    > > > out..

    >
    > > The recursive factorial function will be a lot slower than an
    > > iterative one.
    > > But the lion's share of the time is going to be in writing out the
    > > text file.
    > > It occupies 160 MB on my machine. It appears that your friend has a
    > > faster disk than you do.
    > > This might be marginally faster:

    >
    > > #include <stdio.h>

    >
    > > unsigned fact(unsigned n)
    > > {
    > > unsigned result = 1;
    > > while (n-- > 1)
    > > result *= n;
    > > return result;

    >
    > > }

    >
    > > int main()
    > > {
    > > FILE *fp;
    > > unsigned loop;
    > > if ((fp = fopen("log.txt", "a")) != NULL) {
    > > setvbuf(fp, NULL, _IOFBF, 16000);
    > > for (loop=1; loop <= 10000000; loop++) {
    > > fprintf(fp, "%u\n", fact(10));
    > > }
    > > fclose(fp);
    > > }
    > > return 0;

    >
    > > }

    >
    > > This program executes in less than one second on my machine (showing
    > > that the time is almost exclusively I/O):

    >
    > > #include <stdio.h>

    >
    > > unsigned fact(unsigned n)
    > > {
    > > unsigned result = 1;
    > > while (n-- > 1)
    > > result *= n;
    > > return result;

    >
    > > }

    >
    > > int main()
    > > {
    > > FILE *fp;
    > > unsigned loop;
    > > double sum = 0;
    > > if ((fp = fopen("log.txt", "a")) != NULL) {
    > > setvbuf(fp, NULL, _IOFBF, 16000);
    > > for (loop=1; loop <= 10000000; loop++) {
    > > sum += fact(10);
    > > }
    > > printf("sum was %f\n", sum);
    > > fclose(fp);
    > > }
    > > return 0;}

    >
    > > /*
    > > C:\tmp>foo
    > > sum was 3628800000000.000000
    > > */

    >
    > Thanks sir for you reply..
    > you mentioned "It appears that your friend has a
    > faster disk than you do"
    > We both have the same laptop..same model..anyway, I tested my program
    > in his laptop just in case..
    > anyway, I compiled the code you posted..its save a 0-byte text file on
    > machine..
    > I will try with another compiler..I'll get back to you..


    If your disk drives are the same, that indicates that the buffered I/O
    of C# is superior to the buffered I/O of C.

    You must have compiled the second program, which does not write
    anything to disk.
    The first program writes a 160 MB file.

    Here is a very cheesy alternative:

    #include <stdio.h>
    #include <string.h>
    #include <errno.h>

    unsigned fact(unsigned n)
    {
    unsigned result = 1;
    while (n-- > 1)
    result *= n;
    return result;
    }

    static char absurd_buffer[160000000];

    int main(void)
    {
    FILE *fp;
    unsigned loop;
    if ((fp = fopen("log.txt", "w")) != NULL) {

    if (setvbuf(fp, absurd_buffer, _IOFBF, sizeof absurd_buffer) !
    = 0) {
    int e = errno;
    puts(strerror(e));
    printf("Incorrect type or size of buffer for log.txt.
    Value of errno is %d.\n", e);
    }
    for (loop = 1; loop <= 10000000; loop++) {
    fprintf(fp, "%u\n", fact(10));
    }
    fclose(fp);
    }
    return 0;
    }
     
    user923005, Dec 13, 2007
    #19
  20. c

    James Kuyper Guest

    Re: Help me, a friend of mine wrote a program in C# and I wrote thesame program in C..His is faster than mine on the same machine...How Come?

    c wrote:
    > On Dec 13, 3:46 am, "Default User" <> wrote:
    >> c wrote:
    >>> On Dec 13, 2:51 am, wrote:
    >>>> The answer to your question is 42.
    >>> What do you mean by 42..please give me more details..

    >> <http://en.wikipedia.org/wiki/Answer_to_Life,_the_Universe,_and_Ever
    >> ything>
    >>
    >> Brian

    >
    > still don't know that do you guys mean by 42!!


    If you followed up that wikipedia link, you should know that '42' is
    meant as a nonsense answer to a question that hasn't been well-thought out.
     
    James Kuyper, Dec 13, 2007
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bjoern
    Replies:
    0
    Views:
    583
    Bjoern
    Oct 21, 2003
  2. xyZed
    Replies:
    8
    Views:
    394
    xyZed
    Apr 5, 2006
  3. FLEB
    Replies:
    2
    Views:
    428
    =?ISO-8859-1?Q?J=FCrgen_Kahrs?=
    Aug 29, 2004
  4. Richard Harter

    Your getline is better than mine

    Richard Harter, Nov 29, 2007, in forum: C Programming
    Replies:
    0
    Views:
    327
    Richard Harter
    Nov 29, 2007
  5. Armando Montes De Oca
    Replies:
    19
    Views:
    288
    Armando Montes De Oca
    Jun 6, 2013
Loading...

Share This Page