A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: with is much slower ?

  1. #1
    Senior Member
    Join Date
    Jul 2008
    Posts
    418

    with is much slower ?

    PHP Code:
    t1s getTimer();
    with (AA)
    {
        for (var 
    =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 
    =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.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    What happens if you remove the AA from AA.hSpeed in the first example?

  3. #3
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    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.

  4. #4
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    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(); 

  5. #5
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    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 (
    =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.

  6. #6
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    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.

  7. #7
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    ah, yes. how can you change that?

    maybe declare a var i in the class of AA? or is that not a good idea?

  8. #8
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    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...

  9. #9
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center