for loop speed trick? [英] for loop speed trick?

查看:51
本文介绍了for loop speed trick?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




在Yahoo! UI event.js文件我看到以下相当多的


for(var i = 0,len = unloadListeners.length; i< len; ++ i){


当我总是写的时候(/ i $ b; b< unloadListeners.length; ++ i){

是否值得申报变量len以节省时间评估

unloadListeners.length?


谢谢,

Peter

解决方案

2006年8月31日16:49:30 -0700, pe ********** @ gmail.com 写道:


>在Yahoo! UI event.js文件我看到以下相当多的


for(var i = 0,len = unloadListeners.length; i< len; ++ i){

当我总是写的时候($ i = 0; i< unloadListeners.length; ++ i){

值得吗?它声明变量len以节省时间评估
unloadListeners.length?



是的。


Jim。


Jim Ley写道:


>声明变量len以节省时间评估
unloadListeners.length是否值得?



是的。



IIF你的数组长度在迭代期间不会发生变化。


另外,除非你迭代一个多次,你不太可能看到任何可观察到的好处。例如,迭代0..10可能不会实现任何显着的速度提升。


参见循环速度上这个有趣的线程:
http://groups.google.com /group/comp....ecf5e28064c78c


-

Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com


JRS:文章< 11 ***** ****************@b28g2000cwb.googlegroups。 com>,

日期为2006年8月31日星期四16:49:30远程,见于

news:comp.lang.javascript, pe ********** @ gmail.com 发布:


>在Yahoo! UI event.js文件我看到以下相当多的


for(var i = 0,len = unloadListeners.length; i< len; ++ i){

当我总是写的时候($ i = 0; i< unloadListeners.length; ++ i){

值得吗?它声明变量len以节省时间评估
unloadListeners.length?



这将节省时间。但是,除非循环体很小并且多次执行
,否则节省的金额将与

总额成比例。


如果循环中的操作可以按相反的顺序执行,


i = unLoadListeners.length; while(i--){loop body}


应该还要快一点。


最好不要使用i ;作为代码中的标识符,其他人可以阅读; b $ b;在某些字体中它看起来像我jl 1,并且可能会被

语法检查器所憎恨。

您应该很少需要询问有关

相当于一段代码,因为自己很容易进行测试。


阅读新闻组及其常见问题解答是个好主意。

-

?约翰斯托克顿,英国萨里。 ?@merlyn.demon.co.uk Turnpike v4.00 IE 4?

< URL:http://www.jibbering.com/faq/>? JL / RC:新闻常见问题:comp.lang.javascript

< URL:http://www.merlyn.demon.co.uk/js-index.htmjscr数学,日期,来源。

< URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/& c,常见问题项目,链接。


Hi,

In the Yahoo! UI event.js file I see the following quite a bit

for (var i=0,len=unloadListeners.length; i<len; ++i) {

when I always just write

for (var i=0; i<unloadListeners.length; ++i) {

Is it worth it to declare the variable len to save time evaluating
unloadListeners.length?

Thanks,
Peter

解决方案

On 31 Aug 2006 16:49:30 -0700, pe**********@gmail.com wrote:

>In the Yahoo! UI event.js file I see the following quite a bit

for (var i=0,len=unloadListeners.length; i<len; ++i) {

when I always just write

for (var i=0; i<unloadListeners.length; ++i) {

Is it worth it to declare the variable len to save time evaluating
unloadListeners.length?

Yes it is.

Jim.


Jim Ley wrote:

>Is it worth it to declare the variable len to save time evaluating
unloadListeners.length?

Yes it is.

IIF your array length isn''t expected to change during the iteration.

Also, unless you iterate quite a number of times, you are unlikely to see
any observable benefit. Iterating 0..10, for example, will probably not
realize any noticeable speed improvement.

See also this interesting thread on loop speed:
http://groups.google.com/group/comp....ecf5e28064c78c

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com


JRS: In article <11*********************@b28g2000cwb.googlegroups. com>,
dated Thu, 31 Aug 2006 16:49:30 remote, seen in
news:comp.lang.javascript, pe**********@gmail.com posted :

>In the Yahoo! UI event.js file I see the following quite a bit

for (var i=0,len=unloadListeners.length; i<len; ++i) {

when I always just write

for (var i=0; i<unloadListeners.length; ++i) {

Is it worth it to declare the variable len to save time evaluating
unloadListeners.length?

It will save time. But, unless the loop body is small and will be
executed many times, the saving will be small in proportion to the
total.

If the operations in the loop can be performed in the reverse order,

i = unLoadListeners.length ; while (i--) { loop body }

should be a little faster still.

It is better not to use "i" as an identifier in code that others may
read; in some fonts it looks like I j l 1, and may be resented by
grammar-checkers.
You should rarely need to ask questions about the comparative speeds of
equivalent pieces of code, since it is so easy to do the test yourself.

It''s a good idea to read the newsgroup and its FAQ.
--
? John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ?
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.


这篇关于for loop speed trick?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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