为什么C#比c ++慢得多??? [英] WHy is C# so much slower than c++???

查看:85
本文介绍了为什么C#比c ++慢得多???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做错了什么吗?我在dotnet

开发小组上发布了这个 - 对不起,如果它是双重发布,但我们认真考虑去#c
这可能是一个显示阻止。


我运行了以下C#程序,它在9秒内运行(给予或取1

秒)


使用System;

使用System.Collections.Generic;

使用System.Text;


命名空间DotProduct1Million

{

class program

{

static void Main(string [] args)

{


double d1 = 0.727272;

double d2 = 0.26252;

double d3 = 343432.232;

Console.WriteLine(" {0}",System.DateTime.Now);

for(long i = 0; i< 2000000000; i ++)

{

d3 = d1 * d2;

d1 = d3 * d2;

d2 = d2 * d2;

d3 = d1 * d2;

d3 = d3 * d2;

}

Console.WriteLine(" {0}", System.DateTime.Now) ;

}

}


}


我运行了以下C ++程序 - 它在1秒内运行。 C#

真的比C ++慢得多????

这里有什么问题?


#include< iostream>

#include< cstdlib>

#include< ctime>


使用命名空间std;


int main(int argc,char * argv [])

{

double d1 = 0.727272;

double d2 = 0.26252;

double d3 = 343432.232;


cout<<时间(NULL)<< endl;


for(long i = 0; i< 2000000000; i ++)

{


d3 = d1 * d2;

d1 = d3 * d2;

d2 = d2 * d2;

d3 = d1 * d2;

d3 = d3 * d2;

}

cout<<时间(NULL)<< endl;

Did I do something wrong? I cross posted this on the dotnet
development group -- sorry if it is a double posting but we are
seriously considering going to c# and this could be a show stopper.

I ran the following C# program and it ran in 9 seconds (give or take 1
sec)

using System;
using System.Collections.Generic;
using System.Text;

namespace DotProduct1Million
{
class Program
{
static void Main(string[] args)
{

double d1 = 0.727272;
double d2 = 0.26252;
double d3 = 343432.232;
Console.WriteLine(" {0}", System.DateTime.Now);
for (long i = 0; i < 2000000000; i++)
{
d3 = d1 * d2;
d1 = d3 * d2;
d2 = d2 * d2;
d3 = d1 * d2;
d3 = d3 * d2;
}
Console.WriteLine(" {0}", System.DateTime.Now);
}
}

}

I ran the following C++ program -- it ran in under 1 second. Is C#
really that much slower than C++????
What is the problem here?

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main(int argc,char *argv[])
{
double d1 = 0.727272;
double d2 = 0.26252;
double d3 = 343432.232;

cout << time(NULL) << endl;

for (long i = 0; i < 2000000000; i++)
{

d3 = d1 * d2;
d1 = d3 * d2;
d2 = d2 * d2;
d3 = d1 * d2;
d3 = d3 * d2;
}
cout << time(NULL) << endl;

推荐答案

好吧 - 稍微修改一下代码:

double d1 = 0.92;

double d2 = 1.34;

double d3 = 1.0;

Console.WriteLine(" {0}",System.DateTime.Now );

for(long i = 0; i< 2000000000; i ++)

{

d3 = d1 * d2;

d1 = d3 * d2;

d2 = d2 * d2;

d3 = d1 * d2;

d3 = d3 * d2;

如果(d1 1.0e10)d1 = 9.0;

if(d2 9e10)d2 = 1.6;

这使得时间C ++为12.6秒,C#为15-16秒。

更合理。

我怀疑存在下溢/溢出情况和C ++代码

停止执行。


仍然C#仍然比C ++慢约20%。管理C ++也是如此!


jimocz写道:
okay -- chnaged some code a bit:
double d1 = 0.92;
double d2 = 1.34;
double d3 = 1.0;
Console.WriteLine(" {0}", System.DateTime.Now);
for (long i = 0; i < 2000000000; i++)
{
d3 = d1 * d2;
d1 = d3 * d2;
d2 = d2 * d2;
d3 = d1 * d2;
d3 = d3 * d2;
if (d1 1.0e10) d1 = 9.0;
if (d2 9e10) d2 = 1.6;
This makes the times 12.6 secs for C++ and 15-16 seconds for C#.
Much more reasonable.
I suspect there was an underflow/overflow situation and the C++ code
stopped executing.

Still C# is still ~ 20% slower than C++. Managed C++ too!

jimocz wrote:

我做错了什么?我在dotnet

开发小组上发布了这个 - 对不起,如果它是双重发布,但我们认真考虑去#c
这可能是一个显示阻止。


我运行了以下C#程序,它在9秒内运行(给予或取1

秒)


使用System;

使用System.Collections.Generic;

使用System.Text;


命名空间DotProduct1Million

{

class program

{

static void Main(string [] args)

{


double d1 = 0.727272;

double d2 = 0.26252;

double d3 = 343432.232;

Console.WriteLine(" {0}",System.DateTime.Now);

for(long i = 0; i< 2000000000; i ++)

{

d3 = d1 * d2;

d1 = d3 * d2;

d2 = d2 * d2;

d3 = d1 * d2;

d3 = d3 * d2;

}

控制台.WriteLine(QUOT; {0}",System.DateTime.Now);

}

}


}


我运行了以下C ++程序 - 它运行时间不到1秒。 C#

真的比C ++慢得多????

这里有什么问题?


#include< iostream>

#include< cstdlib>

#include< ctime>


使用命名空间std;


int main(int argc,char * argv [])

{

double d1 = 0.727272;

double d2 = 0.26252;

double d3 = 343432.232;


cout<<时间(NULL)<< endl;


for(long i = 0; i< 2000000000; i ++)

{


d3 = d1 * d2;

d1 = d3 * d2;

d2 = d2 * d2;

d3 = d1 * d2;

d3 = d3 * d2;

}

cout<<时间(NULL)<< ENDL;
Did I do something wrong? I cross posted this on the dotnet
development group -- sorry if it is a double posting but we are
seriously considering going to c# and this could be a show stopper.

I ran the following C# program and it ran in 9 seconds (give or take 1
sec)

using System;
using System.Collections.Generic;
using System.Text;

namespace DotProduct1Million
{
class Program
{
static void Main(string[] args)
{

double d1 = 0.727272;
double d2 = 0.26252;
double d3 = 343432.232;
Console.WriteLine(" {0}", System.DateTime.Now);
for (long i = 0; i < 2000000000; i++)
{
d3 = d1 * d2;
d1 = d3 * d2;
d2 = d2 * d2;
d3 = d1 * d2;
d3 = d3 * d2;
}
Console.WriteLine(" {0}", System.DateTime.Now);
}
}

}

I ran the following C++ program -- it ran in under 1 second. Is C#
really that much slower than C++????
What is the problem here?

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main(int argc,char *argv[])
{
double d1 = 0.727272;
double d2 = 0.26252;
double d3 = 343432.232;

cout << time(NULL) << endl;

for (long i = 0; i < 2000000000; i++)
{

d3 = d1 * d2;
d1 = d3 * d2;
d2 = d2 * d2;
d3 = d1 * d2;
d3 = d3 * d2;
}
cout << time(NULL) << endl;


这使得C ++的时间为12.6秒,C#的时间为15-16秒。
This makes the times 12.6 secs for C++ and 15-16 seconds for C#.

更合理。

我怀疑存在下溢/溢出情况且C ++代码

已停止执行。


仍然C#仍比C ++慢约20%。管理C ++呢!
Much more reasonable.
I suspect there was an underflow/overflow situation and the C++ code
stopped executing.

Still C# is still ~ 20% slower than C++. Managed C++ too!



您好,

在您的C#项目设置中启用优化器。它默认是禁用的。

然后C ++。NET和C#结果应该几乎相同。

原生C ++可能会更快地降低内存开销。


为了更准确的计时,你应该使用StopWatch类,而不是在你的时间测量中包括

的Console.WriteLine语句。这将给你

你的准确性或更好。


至于切换到C#的话:从个人经验我可以说

应用程序开发时间和调试时间在C#

中比在本机C ++甚至C ++ .NET中短得多。

YMMV。


-


亲切的问候,

Bruno van Dooren
BR ********************** @ hotmail.com

仅删除_nos_pam

Hi,
enable the optimizer in your C# project settings. It is disabled by default.
Then the C++.NET and C# results should be nearly identical.
Native C++ will probably a bit faster dues to lower memory overhead.

For more accurate timing you should use the StopWatch class, and not include
the Console.WriteLine statements in your time measurements. That will give
you ms accuracy or better.

As far as switching to C# goes: from personal experience I can say that
application development times and debugging times are much shorted in C#
than in native C++ or even C++.NET.
YMMV.

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"





for(long i = 0。 ..

in C#



for(long i = 0 ...
$ C $ b in C ++ not not C#中的长整数是64位,而在C ++中它是一个32位的价值。

让我成为一个整数,你会发现两者都是以相同的速度运行。

Willy。


" jimocz"< dw **** @ gmail.comwrote in message

news:11 ********************** @ k70g2000cwa.googlegr oups.com ...

|好吧 - 有点代码:

| double d1 = 0.92;

| double d2 = 1.34;

| double d3 = 1.0;

| Console.WriteLine(" {0}",System.DateTime.Now);

| for(long i = 0; i< 2000000000; i ++)

| {

| d3 = d1 * d2;

| d1 = d3 * d2;

| d2 = d2 * d2;

| d3 = d1 * d2;

| d3 = d3 * d2;

| if(d1 1.0e10)d1 = 9.0;

|如果(d2 9e10)d2 = 1.6;

|

|

|这使得C ++的时间为12.6秒,C#的时间为15-16秒。

|更合理。

|我怀疑有一个下溢/溢出的情况和C ++代码

|停止执行。

|

| C#仍然比C ++慢约20%。也管理C ++!

|

|

|

| jimocz写道:

|我做错什么了吗?我在dotnet上发布了这个帖子

|开发小组 - 对不起,如果它是双重发布,但我们是

|认真考虑转到c#,这可能是一个显示停止。

| >

|我运行了下面的C#程序,它在9秒内运行(给或取1

|秒)

| >

|使用系统;

|使用System.Collections.Generic;

|使用System.Text;

| >

|命名空间DotProduct1Million

| {

|课程计划

| {

| static void Main(string [] args)

| {

| >

| double d1 = 0.727272;

| double d2 = 0.26252;

| double d3 = 343432.232;

| Console.WriteLine(" {0}",System.DateTime.Now);

| for(long i = 0; i< 2000000000; i ++)

| {

| d3 = d1 * d2;

| d1 = d3 * d2;

| d2 = d2 * d2;

| d3 = d1 * d2;

| d3 = d3 * d2;

| }

| Console.WriteLine(" {0}",System.DateTime.Now);

| }

| }

| >

| }

| >

|我运行了以下C ++程序 - 它运行时间不到1秒。是C#

|真的比C ++慢得多????

|这有什么问题?

| >

| #include< iostream>

| #include< cstdlib>

| #include< ctime>

| >

|使用命名空间std;

| >

| int main(int argc,char * argv [])

| {

| double d1 = 0.727272;

| double d2 = 0.26252;

| double d3 = 343432.232;

| >

| cout<<时间(NULL)<< endl;

| >

| for(long i = 0; i< 2000000000; i ++)

| {

| >

| d3 = d1 * d2;

| d1 = d3 * d2;

| d2 = d2 * d2;

| d3 = d1 * d2;

| d3 = d3 * d2;

| }

| cout<<时间(NULL)<< endl;

|


for(long i = 0...
in C#
and
for(long i = 0...
in C++ are not the same loops. A long in C# is 64 bit while in C++ it''s a
32bit value.
Make i an int and you''ll see that both will run at the same speed.
Willy.

"jimocz" <dw****@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
| okay -- chnaged some code a bit:
| double d1 = 0.92;
| double d2 = 1.34;
| double d3 = 1.0;
| Console.WriteLine(" {0}", System.DateTime.Now);
| for (long i = 0; i < 2000000000; i++)
| {
| d3 = d1 * d2;
| d1 = d3 * d2;
| d2 = d2 * d2;
| d3 = d1 * d2;
| d3 = d3 * d2;
| if (d1 1.0e10) d1 = 9.0;
| if (d2 9e10) d2 = 1.6;
|
|
| This makes the times 12.6 secs for C++ and 15-16 seconds for C#.
| Much more reasonable.
| I suspect there was an underflow/overflow situation and the C++ code
| stopped executing.
|
| Still C# is still ~ 20% slower than C++. Managed C++ too!
|
|
|
| jimocz wrote:
| Did I do something wrong? I cross posted this on the dotnet
| development group -- sorry if it is a double posting but we are
| seriously considering going to c# and this could be a show stopper.
| >
| I ran the following C# program and it ran in 9 seconds (give or take 1
| sec)
| >
| using System;
| using System.Collections.Generic;
| using System.Text;
| >
| namespace DotProduct1Million
| {
| class Program
| {
| static void Main(string[] args)
| {
| >
| double d1 = 0.727272;
| double d2 = 0.26252;
| double d3 = 343432.232;
| Console.WriteLine(" {0}", System.DateTime.Now);
| for (long i = 0; i < 2000000000; i++)
| {
| d3 = d1 * d2;
| d1 = d3 * d2;
| d2 = d2 * d2;
| d3 = d1 * d2;
| d3 = d3 * d2;
| }
| Console.WriteLine(" {0}", System.DateTime.Now);
| }
| }
| >
| }
| >
| I ran the following C++ program -- it ran in under 1 second. Is C#
| really that much slower than C++????
| What is the problem here?
| >
| #include <iostream>
| #include <cstdlib>
| #include <ctime>
| >
| using namespace std;
| >
| int main(int argc,char *argv[])
| {
| double d1 = 0.727272;
| double d2 = 0.26252;
| double d3 = 343432.232;
| >
| cout << time(NULL) << endl;
| >
| for (long i = 0; i < 2000000000; i++)
| {
| >
| d3 = d1 * d2;
| d1 = d3 * d2;
| d2 = d2 * d2;
| d3 = d1 * d2;
| d3 = d3 * d2;
| }
| cout << time(NULL) << endl;
|


这篇关于为什么C#比c ++慢得多???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆