-
with is much slower ?
PHP Code:
t1s = getTimer();
with (AA)
{
for (var i =1000000;i;i--)
{
AA.hSpeed = getDir();
}
}
t1e = getTimer();
trace(t1e - t1s);
seems to be about 4 times as slow as:
PHP Code:
t1s = getTimer();
//with (AA)
{
for (var i =1000000;i;i--)
{
AA.hSpeed = AA.getDir();
}
}
t1e = getTimer();
trace(t1e - t1s);
is this normal, or am i doing something wrong. because i often read code optimization sites and they aways use with.
-
What happens if you remove the AA from AA.hSpeed in the first example?
-
maybe a small increase, but not much, if any.
and btw if i measure the time within the with statement, instead of out of it, theres no difference in time.
-
With is intended to be used for many repeated calls to the same scope - your version is only using that scope once.
I ran this through my benchmark code and the with version came back in about 75% of the time of the explicit version:
PHP Code:
// 480 ms at 1million iterations
with(graphics){
clear();
clear();
clear();
}
// 640 ms
graphics.clear();
graphics.clear();
graphics.clear();
-
i see. however, when i pujt three of them on the list its still much slower. besides i looped it 1,000,000 times.
EDIT: i solved it, but i dont understand why:
i changed the second part into:
PHP Code:
t1s = getTimer(); for (i =1000000;i;i--) { with (AA) { hSpeed = getDir(); hSpeed = getDir(); hSpeed = getDir(); } } t1e = getTimer(); trace("with: " + (t1e - t1s));
so now the for statement is outside and the with is inside. this increases performence with about 500 percent or more.
why is this? and is there a way to have the for statement inside and not decrease performance?
Last edited by omniscient232; 11-13-2008 at 02:34 AM.
-
Putting the loop inside the with statement is like saying
for(AA.i = 1000000, AA.i, AA.i--){ ... }
So instead of keeping the iterator variable close at hand in the local code, it needs to be looked up twice every time you loop...which adds a lot of friction while one class has to search through another.
-
ah, yes. how can you change that?
maybe declare a var i in the class of AA? or is that not a good idea?
-
I think you should be able to decalare i outside the with statement and then use it in the loop within but I havent tested that yet...
-
nope. no performance increase. if im correct, but it could be coincidence, even a slight decrease. :/
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|