8 Replies Latest reply on Oct 2, 2013 12:15 AM by sanjascreenname

    Execution time

    sanjascreenname

      I have 2 bits of code that do the same thing. They take frames and override them on a page.

      Next I link the frames.

      My master page has 100 frames, and in a document I have 50 pages.

      I get these results when I measure time:

      1. 147 seconds

      2. 223 seconds

       

      These are bits of code:

      1.

      masterPg.textFrames.everyItem().override(myDocument.pages.item(0));

      for (pg=1; pg<50; pg++) {

           page=myDocument.pages.item(pg);

            masterPg.textFrames.everyItem().override(page);

            for (var frameNumb=0; frameNumb<framesTotal; frameNumb++) {

               txt1=myDocument.pages.item(pg-1).textFrames.item(frameNumb);

                txt2=myDocument.pages.item(pg).textFrames.item(frameNumb);

                txt1.nextTextFrame=txt2;

             }

         }

       

      2.

      for (var pg=0; pg<50; pg++) {

          page=myDocument.pages.item(pg);

          masterPg.textFrames.everyItem().override(page);

         }

      for (pg=1; pg<50; pg++) {

              for (var frameNumb=0; frameNumb<framesTotal; frameNumb++) {

               txt1=myDocument.pages.item(pg-1).textFrames.item(frameNumb);

                txt2=myDocument.pages.item(pg).textFrames.item(frameNumb);

                txt1.nextTextFrame=txt2;

                 }

         }

       

       

      I cannot explain the time difference! 1 extra for loop in the second bit can’t be the problem because it loops only 50 times?! or?

      Maybe object cache is the problem?

      Please help!?

        • 1. Re: Execution time
          Andreas Jansson Level 2

          If framesTotal is high, I would say that is the cause.

          The second loop in example 1 will run 50 * framesTotal in example 2.

           

          compare.PNG

          • 2. Re: Execution time
            sanjascreenname Level 1

            The difference in the code is only 1 extra for loop. Nothing else.

            All the code is running same amount of times in examples, I mean it gets executed the same amount of times.

            I think that ExtendScript engine has some optimization for this (memory management?!), but i don’t know which optimization because I can’t find any documentation on the engine and the way it works.

            • 3. Re: Execution time
              Andreas Jansson Level 2

              You have 1 extra loop in example number 2 (looping 50 times). But inside that loop you still do the same loop that you have got in example number 1.

               

              Where is framesTotal set? And what is the value of it?

              • 4. Re: Execution time
                sanjascreenname Level 1

                framesTotal is 100 frames, and I have them on a master page.

                The extra for loop in the second example is a loop that I use to override all master text frames on every page.

                 

                In first example I deleted that loop, and pages get overriden in the second loop.

                 

                I do overriding of frames the same amount of times in both examples. 5000 frames total get overridden.

                • 5. Re: Execution time
                  Andreas Jansson Level 2

                  Yes, sorry, you are right, my mistake.

                   

                  Could you check whether the time for  linking is low at first, and then increasing in sample 1, and if the time to relink is long, from the beginning, in sample 2?

                   

                  And / or time the override calls, to get to know what part of the code that is the problem.

                  • 6. Re: Execution time
                    sanjascreenname Level 1

                    I have already measured the times

                    First example;

                    Override 89 sec

                    Linking 41 sec

                    Second example;

                    Override 96 sec

                    Linking 137 sec

                    • 7. Re: Execution time
                      Vamitul Level 4

                      The difference is caused by the extra for. More exactly it is caused by the extra DOM acess. Your first script goes over 50 pages, and for each it does it stuff. The second one, goes over 50 pages, overrides everything, then again goes over 50 pages and links stuff. Quite a lot more overhead.

                      Now, there are quite a lot of ways you can optimize even the first one (i am assuming all your variables are predeclared somwhere, not globals).

                      For examle, just replacing the txt2 line to something like this:

                       

                      txt2=page.textFrames.item(frameNumb);

                       

                      will give you a nice speed boost (general javascript object caching).

                       

                      Add just before closing the outer loop a line like:

                      prevPage=page;

                      and change the txt1 line to

                      txt1=prevPage.textFrames.item(frameNumb);

                      and the execution time will be.. i don't know, but i'm guessing about half..

                       

                      also, make sure you have scriptRedraw set to false.

                       

                      Good luck

                      • 8. Re: Execution time
                        sanjascreenname Level 1

                        Actually, at the end I did this and got the best times….

                             for (pg=1; str<50; pg++) {

                                     framesMaster.override(pgDoc.item(pg));

                                     page1=pgDoc.item(pg).textFrames.everyItem().getElements();

                                     page=pgDoc.item(pg-1).textFrames.everyItem().getElements();

                                for (frameNumb=0; frameNumb<framesTotal; frameNumb++) {

                                     page[frameNumb].nextTextFrame=page1[frameNumb];

                                     }

                               }