C和C#中的相同程序。 C#比C快。怎么来? [英] Same program in C and in C#. C# is faster than C. How Come ?
问题描述
大家好,
我和我的堂兄在谈论C和C#,我喜欢C而他喜欢
C#..并且正在谈论C是......等等...... C#是Blah Blah ......等等b $ b然后我们决定编写一个程序来计算
factorial为10,1000万次,并将文件中的reusult打印成
,名称为log.txt ..
我写了这样的东西
#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);
返回0;
}
unsigned int fib(int n)
{if(n! = 1)
返回n * fib(n-1);
else
retu 1;
}
他用C#
做了一些事情然后我们都有相同的笔记本电脑......戴尔Inspiron 6000。 br />
我跑了我的程序,我花了18秒才完成。这个程序花了7
秒..好吧
然后我让他在他的笔记本电脑上运行我的程序..这就是所有的
一样..但我想...我跑了...给了我同时..
怎么来的??!
第二天,我尝试了一些优化
并开发了循环并写了这样的东西
for(loop; loop< = 1000000;循环++)
{
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%\\ u%u%u%u $ / u $
",fib(10),fib(10),fib(10 ),fib(10),fib(10));
}
但他的程序仍然比我的快......
>
然后,我在Slackware 12下尝试了这个程序....它花了3.8秒来完成
完成..哇,我赢得了挑战..
>
无论如何,他希望我在Windows XP下击败他......请大家帮帮我
out ..
在文章< da ********************************** @ c4g2000hsg.g ooglegroups中。 com>,
c< al ****** @ gmail.comwrote:
> unsigned int fib(int n)
{if(n!= 1)
返回n * fib(n-1);
返回1;
}
我hink事实将是这个功能的更好的名称。
>然后我们都有相同的笔记本电脑..戴尔Inspiron 6000.
我运行我的程序,我花了18秒才完成
您使用了什么编译器?什么优化设置?你不能用b $ b来表示这些语言的相对优势,就像这样的随机数字一样。
>并开发了循环并写了类似的东西
for(loop; loop< = 1000000; loop ++)
{
fprintf(fp," %u \ n%u%u%u%u%u u u u u u u u u u u u u u u u fib fib fib fib fib fib fib fib fib ),fib(10)); fprintf(fp,%u \ n%u \ n%u \ n%u \ n%u \ n
, fib(10),fib(10),fib(10),fib(10),fib(10));
}
这很可能是一个毫无意义的变化。与使用阶乘函数所花费的时间相比,循环开销很小
。
如果你使用
$,你的程序很可能会快得多b $ ba循环来计算阶乘而不是递归。
- 理查德
-
:wq
12月13日凌晨2:35,富... @ cogsci.ed.ac.uk(Richard Tobin)写道:
文章< da48736e-99f7-495f-90d8-627d34845 ... @ c4g2000hsg.googlegroups.com>,
c< alcon ... @ gmail。 comwrote:
unsigned int fib(int n)
{if(n!= 1)
return n * fib(n-1);
else
返回1;
}
我认为事实这将是一个更好的名称。
然后我们都有相同的笔记本电脑..戴尔Inspiron 6000.
我运行了我的程序,我用了18秒才完成了
您使用了什么编译器?什么优化设置?你不能用b $ b来表示这些语言的相对优势,就像这样的随机数字一样。
并开发了循环并写了类似这样的东西
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%)你不喜欢这些美元,不包括美元,美国美元,美国国债,美国国债(10),美国国债(10),美国国债(10),美国国债(10), fib(10));
}
这可能是一个毫无意义的变化。与使用阶乘函数所花费的时间相比,循环开销很小
。
如果你使用
$,你的程序很可能会快得多b $ ba循环来计算阶乘而不是递归。
- 理查德
-
:wq
我尝试过Tiny C编译器,使用Turbo C ......
我们(我和我的堂兄)在我们的程序中使用了递归。 />
比你。
12月13日凌晨2:41,c< alcon ... @ gmail.comwrote:
< blockquote class =post_quotes>
12月13日凌晨2:35,富有... @ cogsci.ed.ac.uk(Richard Tobin)写道:
文章< da48736e-99f7-495f-90d8-627d34845 ... @ c4g2000hsg.googlegroups.com>,
c< alcon ... @ gmail.comwrote:
> unsigned int fib(int n)
{if(n!= 1)
返回n * fib(n-1);
else
返回1;
}
我认为事实将是这个功能的更好的名称。
>然后我们都拥有相同的笔记本电脑......戴尔Inspiron 6000.
>我运行了我的程序,我花了18秒才完成
您使用了什么编译器?什么优化设置?你不能用b $ b来表示这些语言的相对优势,就像那样的随机数字。
>并开发了循环并写了类似这样的东西
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));
}
这可能是一个毫无意义的改变。与阶乘函数所用的时间相比,循环开销很小。
如果你使用
循环来计算阶乘而不是递归,那么你的程序很可能会快得多。
- Richard
-
:wq
我尝试使用Tiny C编译器,使用Turbo C ...
我们(我和我的表弟)在我们的程序中使用了递归。
比你。
还有一件事,为什么在Slackware下使用GCC ..我的程序变得更快?
我用时间测试它
[slackware]时间./fact
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..
In article <da**********************************@c4g2000hsg.g ooglegroups.com>,
c <al******@gmail.comwrote:
>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 doneWhat 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
On Dec 13, 2:35 am, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:In article <da48736e-99f7-495f-90d8-627d34845...@c4g2000hsg.googlegroups.com>,
c <alcon...@gmail.comwrote: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
--
:wqI tried Tiny C Compiler, and with Turbo C...
we both (me and my cousin) used recursion in our programs.
Than you .
On Dec 13, 2:41 am, c <alcon...@gmail.comwrote:On Dec 13, 2:35 am, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <da48736e-99f7-495f-90d8-627d34845...@c4g2000hsg.googlegroups.com>,
c <alcon...@gmail.comwrote:>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和C#中的相同程序。 C#比C快。怎么来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!