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

C

c

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..
 
R

Richard Tobin

c said:
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
 
C

c

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



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.



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

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


Than you .
 
U

user923005

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
*/
 
C

c

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..
 
P

pete

c said:
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 */
 
C

c

c said:
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 */

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.
 
P

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.

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 */
 
C

c

c said:
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 */

Thank you very much, that's very sweet of you.
 
T

Tor Rustad

c said:
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.
 
D

Doug

<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
 
I

Ian Collins

c said:
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...
 
G

Gene

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.
 
U

user923005

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;
}
 
J

James Kuyper

c said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top