这个语法糖果怎么样? [英] How about this syntactic candy?

查看:56
本文介绍了这个语法糖果怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




for(int i = 0; i< list.Count; i ++)


隐藏的性能打击; ie list.Count每次都会被评估,所以

我们写的东西如下:


int listCount = list.Count;

for(int i = 0; i< listCount; i ++)


如何简单地说:


for(int i = 0; i < const list.Count; i ++)

我觉得它看起来不错,简单而干净,可以提高性能

(或至少删除性能打击)。想法?


希尔顿

PS:免责声明,我不太熟悉新的C#更改所以如果

的话类似的是在那里,让我知道。

Hi,

for (int i = 0; i < list.Count; i++)

has a hidden performance hit; i.e. list.Count gets evaluated each time, so
we write something like:

int listCount = list.Count;
for (int i = 0; i < listCount; i++)

How about simply:

for (int i = 0; i < const list.Count; i++)

I think it looks kinda nice and is a simple and clean to improve performance
(or at least remove the performance hit). Thoughts?

Hilton
P.S.: Disclaimer, I''m not too familiar with the new C# changes so if
something similar is in there, let me know.

推荐答案

希尔顿写道:
Hilton wrote:

for (int i = 0; i< list.Count; i ++)

隐藏的性能损失; ie list.Count每次都会被评估,所以

我们写的东西如下:


int listCount = list.Count;

for(int i = 0; i< listCount; i ++)


如何简单地说:


for(int i = 0; i < const list.Count; i ++)

我觉得它看起来不错,简单而干净,可以提高性能

(或至少删除性能打击)。思考?
for (int i = 0; i < list.Count; i++)

has a hidden performance hit; i.e. list.Count gets evaluated each time, so
we write something like:

int listCount = list.Count;
for (int i = 0; i < listCount; i++)

How about simply:

for (int i = 0; i < const list.Count; i++)

I think it looks kinda nice and is a simple and clean to improve performance
(or at least remove the performance hit). Thoughts?



我不喜欢它。


我认为你应该把代码写成:


for(int i = 0; i< list.Count; i ++)


这是最易读的代码并保留优化

到编译器(csc& clr jit)。


他们应该优化那个调用而不是程序员。


(我不知道他们是否已经这样做了)


Arne


I don''t like it.

I think you should write the code as:

for (int i = 0; i < list.Count; i++)

which is the most readable code and leave the optimization
to the compiler (csc & clr jit).

They should optimize that call away not the programmer.

(I don''t know if they already do)

Arne



我同意Arne 。你真的*知道* list.Count被评估

每次最终结果*实际上*更慢?


我没有要求要知道,但怀疑优化的可能性很大

无论如何都会对它进行排序。我曾经看过一个for循环的演示,所有压缩的

变成一行,带有巧妙的快捷方式和收缩,并且在

优化编译器经过它后,它完全生成了相同的

机器代码作为完整写出的替代循环。


事实上,我依稀记得有人解释代码非常好
$由编写器优化的b $ b甚至可以使优化编译器产生比简单但冗长的算法版本更好的代码。不幸的是,我不能记住这个产品。它可能是一个旧的Delphi吗?


我一直认为在书面代码阶段进行优化是:


1 /不受欢迎,因为它使代码更难理解,调试

并维持


2 /不太可能正常工作


SteveT

I agree with Arne. Do you actually *know* that list.Count gets evaluated
each time and the end result is *actually* slower?

I don''t claim to know, but suspect there is a good chance the optimisation
will sort it all out anyway. I once saw a demo of a for loop all compressed
into one line with ingenious shortcuts and contractions, and after the
optimising compiler had been through it, it generated EXACTLY the same
machine code as an alternative loop written out in full.

In fact, I vaguely remember someone explaining that code which is highly
optimised by the writer can even make the optimising compiler produce
*worse* code than a simple, but verbose, version of the algorithm. I can''t
remember the product, unfortunately. Could it have been an old Delphi?

I''ve always believed that optimising at the written code stage is:

1/ undesirable, because it makes the code much harder to understand, debug
and maintain

2/ unlikely to work anyway

SteveT


Arne Vajh?j写道:
Arne Vajh?j wrote:

Hilton写道:
Hilton wrote:

> for(int i = 0; i< list.Count; i ++)

隐藏性能损失;即list.Count每个时间进行评估,所以我们写了类似的东西:

int listCount = list.Count;
for(int i = 0; i< listCount;简单来说:

for(int i = 0; i< const list.Count; i ++)

我认为它看起来有点不错,是一个简单而干净的提高性能(或至少消除性能损失)。思考?
>for (int i = 0; i < list.Count; i++)

has a hidden performance hit; i.e. list.Count gets evaluated each
time, so we write something like:

int listCount = list.Count;
for (int i = 0; i < listCount; i++)

How about simply:

for (int i = 0; i < const list.Count; i++)

I think it looks kinda nice and is a simple and clean to improve
performance (or at least remove the performance hit). Thoughts?



我不喜欢它。


我认为你应该把代码写成:


for(int i = 0; i< list.Count; i ++)


这是最易读的代码并保留优化

到编译器(csc& clr jit)。


他们应该优化那个调用而不是程序员。


(我不知道他们是否已经这么做了)


I don''t like it.

I think you should write the code as:

for (int i = 0; i < list.Count; i++)

which is the most readable code and leave the optimization
to the compiler (csc & clr jit).

They should optimize that call away not the programmer.

(I don''t know if they already do)



我做了一个小测试。在我3岁的PC上,在for循环中使用

..Count的开销似乎是每个

迭代约0.2纳秒。这很少。


Arne

I made a small test. It seems as the overhead by having
..Count in the for loop is about 0.2 nanoseconds per
iteration on my 3 year old PC. That is very little.

Arne


这篇关于这个语法糖果怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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