使这个子程序更快 [英] Make This Subroutine Faster

查看:67
本文介绍了使这个子程序更快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有建议我可以让这段代码运行得更快:


if(document.getElementById(" 1")){doOne(); }

if(document.getElementById(" 2")){doTwo(); }

....................

if(document.getElementById(" n")){ DON(); }


这是上面的简化版本。这些重复行动有很多。所以我想改变它们:


var arr = new Array(1,2,N);

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

switch(arr [i])

{

case" ; 1":doOne();

case" 2":doTwo();

case" N":doN();

}

}


但我怀疑这会加速,还是会呢?你会建议什么?

Is there a suggestion I can make this code run faster:

if(document.getElementById("1")){ doOne(); }
if(document.getElementById("2")){ doTwo(); }
....................
if(document.getElementById("n")){ doN(); }

It is a simplified version above. There is a large number of these
repetitive actions. So I wanted to change them for:

var arr = new Array("1","2","N");
for(var i=0;i<arr.length;i++){
switch(arr[i])
{
case "1": doOne();
case "2": doTwo();
case "N": doN();
}
}

But I doubt this will speed up, or will it? What would you suggest?

推荐答案

vu******@gmail.com 说:

>
是否有建议我可以让这段代码运行得更快:

if(document.getElementById(" 1")){doOne(); }
if(document.getElementById(" 2")){doTwo(); }
...................
if(document.getElementById(" n")){doN(); }

这是上面的简化版本。这些重复行动有很多。所以我想改变它们:

var arr = new Array(1,2,N);
for(var i = 0; i< ; arr.length; i ++){

switch(arr [i])

{

case" 1":doOne();

case" 2":doTwo();

case" N":doN();

}
}

但我怀疑这会加快,还是会呢?你会建议什么?
>
Is there a suggestion I can make this code run faster:

if(document.getElementById("1")){ doOne(); }
if(document.getElementById("2")){ doTwo(); }
...................
if(document.getElementById("n")){ doN(); }

It is a simplified version above. There is a large number of these
repetitive actions. So I wanted to change them for:

var arr = new Array("1","2","N");
for(var i=0;i<arr.length;i++){
switch(arr[i])
{
case "1": doOne();
case "2": doTwo();
case "N": doN();
}
}

But I doubt this will speed up, or will it? What would you suggest?



甚至不能做你想做的事。


重新思考你可能会获得更加显着的加速

无论你对每个元素采取不同的做法。至少在

,例如,如果doOne()需要引用id为1的

元素,则应该将该引用传递给它,所以

它不必重复getElementById调用:


var id;

if(id = document .getElementById(" 1")){doOne(id)}

if(id = document.getElementById(" 2")){doOne(id)}

>
稍加思考,您可以将所有

doOne(),doTwo(),...函数组合到一个函数中。

-

That won''t even do what you want.

You could probably get a much more significant speedup by rethinking
whatever it is that you do differently for each element. At the
very least, if, for example, doOne() requires a reference to the
element with id of "1", you should pass that reference to it, so
it doesn''t have to repeat the getElementById call:

var id;
if(id=document.getElementById("1")){ doOne(id) }
if(id=document.getElementById("2")){ doOne(id) }

With a little thought, you can probably combine all of your
doOne(), doTwo(),... functions into a single function.
--


6月20日上午11点16分,Lee< REM0VElbspamt ... @ cox.netwrote:
On Jun 20, 11:16 am, Lee <REM0VElbspamt...@cox.netwrote:

vunet ... @ gmail.com说:


vunet...@gmail.com said:



有没有建议我可以做到这一点代码运行得更快:
Is there a suggestion I can make this code run faster:


if(document.getElementById(" 1")){doOne(); }

if(document.getElementById(" 2")){doTwo(); }

...................

if(document.getElementById(" n")){doN (); }
if(document.getElementById("1")){ doOne(); }
if(document.getElementById("2")){ doTwo(); }
...................
if(document.getElementById("n")){ doN(); }


这是上面的简化版本。这些重复行动有很多。所以我想改变它们:
It is a simplified version above. There is a large number of these
repetitive actions. So I wanted to change them for:


var arr = new Array(" 1"," 2"" N");

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

switch(arr [i])

{

case" 1":doOne();

case" 2":doTwo();

case" N":doN( );

}

}
var arr = new Array("1","2","N");
for(var i=0;i<arr.length;i++){
switch(arr[i])
{
case "1": doOne();
case "2": doTwo();
case "N": doN();
}
}


但我怀疑这会加速,还是会它?你会建议什么?
But I doubt this will speed up, or will it? What would you suggest?



甚至不能做你想做的事。


重新思考你可能会获得更加显着的加速

无论你对每个元素采取不同的做法。至少在

,例如,如果doOne()需要引用id为1的

元素,则应该将该引用传递给它,所以

它不必重复getElementById调用:


var id;

if(id = document .getElementById(" 1")){doOne(id)}

if(id = document.getElementById(" 2")){doOne(id)}

>
经过一番思考,您可以将所有的

doOne(),doTwo(),...函数组合到一个函数中。


-


That won''t even do what you want.

You could probably get a much more significant speedup by rethinking
whatever it is that you do differently for each element. At the
very least, if, for example, doOne() requires a reference to the
element with id of "1", you should pass that reference to it, so
it doesn''t have to repeat the getElementById call:

var id;
if(id=document.getElementById("1")){ doOne(id) }
if(id=document.getElementById("2")){ doOne(id) }

With a little thought, you can probably combine all of your
doOne(), doTwo(),... functions into a single function.

--



谢谢。你的观点很好。但是,我的doOne()和doTwo是如此的不同,组合它们不会有太大变化。此外,他们在我的应用程序的其他部分重用了
。我真的希望简化

document.getElementById(" N")例程因为有太多的

那些...但我会重新考虑基于的逻辑你的建议。

Thanks. Your point is good. However, my doOne() and doTwo are so
different, that combining them will not change much. Besides, they are
reused on other parts of my app. I was really hoping to simplify
document.getElementById("N") routine because there are too many of
those... But I will reconsider the logic based on your suggestion.


6月20日下午5:38,vunet ... @ gmail.com写道:
On Jun 20, 5:38 pm, vunet...@gmail.com wrote:

6月20日上午11点16分,Lee< REM0VElbspamt ... @ cox.netwrote:
On Jun 20, 11:16 am, Lee <REM0VElbspamt...@cox.netwrote:

vunet ... @ gmail .com说:
vunet...@gmail.com said:


>有没有建议我可以让这段代码运行得更快:
>Is there a suggestion I can make this code run faster:


> if(document.getElementById(" 1")){doOne(); }

> if(document.getElementById(" 2")){doTwo(); }

> ...................

> if(document.getElementById(" n" )){doN(); }
>if(document.getElementById("1")){ doOne(); }
>if(document.getElementById("2")){ doTwo(); }
>...................
>if(document.getElementById("n")){ doN(); }


>这是上面的简化版本。这些重复行动中有大量的这些重复行动。所以我想改变它们:
>It is a simplified version above. There is a large number of these
>repetitive actions. So I wanted to change them for:


> var arr = new Array(" 1,2,N);

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

switch( arr [i])

{

case" 1":doOne();

case" 2":doTwo();

case" N":doN();

}

>}
>var arr = new Array("1","2","N");
>for(var i=0;i<arr.length;i++){
switch(arr[i])
{
case "1": doOne();
case "2": doTwo();
case "N": doN();
}
>}


>但我怀疑这会加速,还是会呢?你会建议什么?
>But I doubt this will speed up, or will it? What would you suggest?


甚至不能做你想要的。
That won''t even do what you want.


你可以通过重新思考
来获得更加显着的加速,无论你对每个元素采取不同的做法。至少在

,例如,如果doOne()需要引用id为1的

元素,则应该将该引用传递给它,所以

它不必重复getElementById调用:
You could probably get a much more significant speedup by rethinking
whatever it is that you do differently for each element. At the
very least, if, for example, doOne() requires a reference to the
element with id of "1", you should pass that reference to it, so
it doesn''t have to repeat the getElementById call:


var id;

if(id = document.getElementById(" 1")){doOne(id)}

if(id = document.getElementById(" 2")){doOne(id) }
var id;
if(id=document.getElementById("1")){ doOne(id) }
if(id=document.getElementById("2")){ doOne(id) }


稍作思考,你可以把所有的

doOne(),doTwo()组合起来。 ..函数成一个函数。
With a little thought, you can probably combine all of your
doOne(), doTwo(),... functions into a single function.


-
--



谢谢。你的观点很好。但是,我的doOne()和doTwo是如此的不同,组合它们不会有太大变化。此外,他们在我的应用程序的其他部分重用了
。我真的希望简化

document.getElementById(" N")例程因为有太多的

那些...但我会重新考虑基于的逻辑你的建议。


Thanks. Your point is good. However, my doOne() and doTwo are so
different, that combining them will not change much. Besides, they are
reused on other parts of my app. I was really hoping to simplify
document.getElementById("N") routine because there are too many of
those... But I will reconsider the logic based on your suggestion.



那些id'真的是1,2,3,...... n,还是只是一个插图?

是它实际上是''a'',''blah'','trt''等等?此外,这些

函数是否真的命名为doOne,doTwo等等,如果它们是,它们是否可以将
重命名为do1,do2,...,don?也许在这种情况下,你可以在代码简单性和简洁性方面做一些改进,但是再一次,

本身并不是一个加速。你在这里要问的是如何用更快的速度替换document.getElementById()是多么的好 - 你不能确定这个,但你能做什么呢?例如,将

相同的''name''属性放入您感兴趣的所有元素中,

然后执行document.getElementsByName()in根据你在其中找到的id,只处理

这些,所以你不要为每个元素调用

document.getElementById()。这可能会提高代码的速度。只剩下一个插图,我们没有天才的想法......


对于那些有经验的人的问题 - 文本大小

对代码执行时间有什么影响? Id est,如果将

变量命名为oddGroupDescriptionSmall或ogds,这是否重要? Javascript

是一种脚本语言,在找到它时执行代码,所以我想

可能速度可能取决于js引擎需要解析的时间

代码......?

Are those id''s really 1, 2, 3, ... n, or is it just an illustration?
Is it actually ''a'', ''blah'', ''trt'', e.t.c.? Further, are these
functions really named doOne, doTwo etc. and if they are, could they
be renamed into do1, do2, ... , don? Maybe in that case you could do
some improvement on code simplicity and brevity, but then again that
wouldn''t be much of a speed-up itself. What you''re asking here is how
to replace document.getElementById() with something faster - you can''t
really get that, but what you could do, for an example, is put the
same ''name'' attribute into all of the elements you are interested in,
and then do document.getElementsByName() in order to process only
these, according to the id you find in them, so you don''t call
document.getElementById() for each element. That could maybe improve
the speed of the code. Having nothing more than an illustration leaves
us with no genius ideas...

A question for those having experience with it - does the text-size
has any impact on code-execution time? Id est, does it matter if the
variables are named like oddGroupDescriptionSmall or ogds? Javascript
is a script-language, executing the code as it finds it, so I thought
maybe the speed might depend on the time the js engine needs to parse
the code...?


这篇关于使这个子程序更快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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