静态与非静态功能性能 [英] Static vs Non-Static Function Performance

查看:51
本文介绍了静态与非静态功能性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们目前正在做一些测试来确定静态vs

非静态函数的性能,我们想出一些奇怪的(在我们看来)

结果。我们使用了非常简单的设置。一个类具有静态函数,并且一个类具有非静态函数。
。这两个函数都完全相同。


测试函数:

public void Test(){

十进制y = 2;

十进制x = 3;

十进制d = 0;


for (十进制z = 0; z <1000000; z ++){

if(z%2 == 0){

d = y * d;

}否则{

d = x * d;

}

}

}


测试运行代码:

for(int i = 0; i< 1000; i ++){

StaticTest.Test();

}


for(int i = 0; i< 1000; i ++){

NonStaticTest nst = new NonStaticTest( );

nst.Test();

}


令我们惊讶的是,非静态方法在15分钟后运行,并且静态

方法在17分钟内完成。我们已经想过,由于需要每次调用非静态方法来实例化一个新对象,所以这种方式需要更长的时间。我们还尝试先运行非静态方法

,然后静态方法第二,结果是一样的。我们每次运行

这些测试3次,以确保我们得到了一个不错的平均值。


我们是否以某种方式偏向于非静态方法的测试?如果对象实例正在进行,那么

如何,非静态方法能够在2分钟的时间内运行
。这当然是一个相当大的余量,并且对于那些构建

性能导向代码的人来说,b $ b可能会产生一定的性能影响。


谢谢,

史蒂夫

We''re currently doing some tests to determine the performance of static vs
non-static functions, and we''re coming up with some odd(in our opinion)
results. We used a very simple setup. One class had a static function, and
the one class had a non-static function. Both of these functions did the
exact same thing.

The test function:
public void Test(){
decimal y = 2;
decimal x = 3;
decimal d = 0;

for(decimal z = 0; z < 1000000;z++){
if(z % 2 == 0){
d = y*d;
} else {
d = x*d;
}
}
}

The test running code:
for (int i = 0; i < 1000; i++) {
StaticTest.Test();
}

for (int i = 0; i < 1000; i++) {
NonStaticTest nst = new NonStaticTest();
nst.Test();
}

To our suprise the non-static approach ran in 15 minutes, and the static
approach ran in 17 minutes. We had figured that due to the need to
instantiate a new object with each call for the non-static approach, that
this way would take longer. We also tried running the non-static approach
first, and the static approach second, the results were the same. We ran
these tests 3 times each to make sure that we got a nice average.

Did we somehow bias the test towards the non-static approach? How is it that
with an object instantion going on, the non-static approach is able to run
in 2 minutes less time. This is of course a substantially large margin, and
could have some definite performance implications for those building
performance oriented code.

Thanks,
Steve

推荐答案

史蒂夫,


有可能正在进行的一些优化正在给出错误的结果。


-

Michael Culley

Steve - DND < ng@digitalnothing.com>在消息新闻中写道:#w ************** @ tk2msftngp13.phx.gbl ...
Hi Steve,

There could be some optimisations going on that are giving false results.

--
Michael Culley
"Steve - DND" <ng@digitalnothing.com> wrote in message news:#w**************@tk2msftngp13.phx.gbl...
我们目前正在做一些测试以确定性能静态vs非静态函数,我们想出一些奇怪的(在我们看来)
结果。我们使用了非常简单的设置。一个类具有静态函数,并且一个类具有非静态函数。这两个函数完全相同。

测试函数:
public void Test(){/ / / / / / / / / / / / / / x = 3;
十进制d = 0;

(十进制z = 0; z <1000000; z ++){
if(z%2 == 0){
d = y * d;
}否则{
d = x * d;
}
}
}

测试运行代码:
for(int i = 0; i< 1000; i ++){
StaticTest.Test();
}
for(int i = 0; i< 1000; i ++){
NonStaticTest nst = new NonStaticTest();
nst.Test();
}

令我们惊讶的是非静态方法在15分钟内完成,静态方法在17分钟内完成。我们已经想到,由于需要每次调用非静态方法来实例化一个新对象,这种方式需要更长的时间。我们还尝试先运行非静态方法,然后静态方法,结果是一样的。我们每次运行这些测试3次,以确保我们得到了一个不错的平均值。

我们是否以某种方式将测试偏向于非静态方法?如果对象实例继续运行,非静态方法能够在2分钟内运行
。这当然是一个相当大的余量,并且对于那些构建性能导向代码的人来说可能会有一些明确的性能影响。

谢谢,
Steve
We''re currently doing some tests to determine the performance of static vs
non-static functions, and we''re coming up with some odd(in our opinion)
results. We used a very simple setup. One class had a static function, and
the one class had a non-static function. Both of these functions did the
exact same thing.

The test function:
public void Test(){
decimal y = 2;
decimal x = 3;
decimal d = 0;

for(decimal z = 0; z < 1000000;z++){
if(z % 2 == 0){
d = y*d;
} else {
d = x*d;
}
}
}

The test running code:
for (int i = 0; i < 1000; i++) {
StaticTest.Test();
}

for (int i = 0; i < 1000; i++) {
NonStaticTest nst = new NonStaticTest();
nst.Test();
}

To our suprise the non-static approach ran in 15 minutes, and the static
approach ran in 17 minutes. We had figured that due to the need to
instantiate a new object with each call for the non-static approach, that
this way would take longer. We also tried running the non-static approach
first, and the static approach second, the results were the same. We ran
these tests 3 times each to make sure that we got a nice average.

Did we somehow bias the test towards the non-static approach? How is it that
with an object instantion going on, the non-static approach is able to run
in 2 minutes less time. This is of course a substantially large margin, and
could have some definite performance implications for those building
performance oriented code.

Thanks,
Steve



" Michael Culley" < MC ***** @ NOSPAMoptushome.com.au>在消息中写道

新闻:O%**************** @ TK2MSFTNGP10.phx.gbl ...
"Michael Culley" <mc*****@NOSPAMoptushome.com.au> wrote in message
news:O%****************@TK2MSFTNGP10.phx.gbl...
嗨史蒂夫,

可能会有一些优化正在给出错误的结果。

-
Michael Culley
Hi Steve,

There could be some optimisations going on that are giving false results.

--
Michael Culley




知道这些改进可能是什么吗?如果他们不这样做,他们应该做同样的事情吗?


史蒂夫



Any idea what these enhancements could be? They should be doing the same
thing, should they not?

Steve


"史蒂夫 - DND < ng@digitalnothing.com>在消息新闻中写道:#q ************** @ TK2MSFTNGP11.phx.gbl ...
"Steve - DND" <ng@digitalnothing.com> wrote in message news:#q**************@TK2MSFTNGP11.phx.gbl...
知道这些改进可能是什么吗?他们应该做同样的事情,如果他们不这样做?


我可以看到几个可能性。编译器可能会发现函数没有返回任何内容,因此优化了函数中的一些

代码。它可以将函数视为静态函数,因为它不使用模块级变量,甚至可能根本不打算创建对象。所有这些都只是猜测。


-

Michael Culley

" Steve - DND" < ng@digitalnothing.com>在消息新闻中写道:#q ************** @ TK2MSFTNGP11.phx.gbl ..." Michael Culley" < MC ***** @ NOSPAMoptushome.com.au>在消息中写道
新闻:O%**************** @ TK2MSFTNGP10.phx.gbl ...
Any idea what these enhancements could be? They should be doing the same
thing, should they not?
I can see several posibilities. The compiler might work out that nothing is returned from the function so optimise out some of the
code in the function. It could treat the function as static because it doesn''t use an module level variables and it might not even
bother creating the object at all. All of these are just guesses though.

--
Michael Culley
"Steve - DND" <ng@digitalnothing.com> wrote in message news:#q**************@TK2MSFTNGP11.phx.gbl... "Michael Culley" <mc*****@NOSPAMoptushome.com.au> wrote in message
news:O%****************@TK2MSFTNGP10.phx.gbl...
嗨Steve,
可能会有一些优化正在给出错误的结果。

-
Michael Culley
Hi Steve,

There could be some optimisations going on that are giving false results.

--
Michael Culley



知道这些是什么增强可能是?他们应该做同样的事情,如果他们不这样做?

史蒂夫



Any idea what these enhancements could be? They should be doing the same
thing, should they not?

Steve



这篇关于静态与非静态功能性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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