3 Replies Latest reply on Aug 27, 2014 7:18 AM by JJMack

    Getting different time duration in executing same code on same layer!

    _VG Level 1

      Hi all,

       

      I wrote a function to get the selected layers. I want to log the time duration of the execution of this function. So I write as follows,

       

      function getSelectedLayers()
      {
          var selectedLayers;
           
          try
          {
              var doc = app.activeDocument;
              selectedLayers   = new Array();
              var isBackGroundPresent;
           
              try
              {
                  isBackGroundPresent = doc.backgroundLayer;
              }
              catch(e)
              {
                  isBackGroundPresent = false;
              }
           
              var layerRef = new ActionReference();
              layerRef.putEnumerated(app.charIDToTypeID("Dcmn"), app.charIDToTypeID("Ordn"), app.charIDToTypeID("Trgt"));
              var layerDesc = app.executeActionGet(layerRef);
              var listOfSlctedLyr;
      
              if(layerDesc.hasKey(app.stringIDToTypeID('targetLayers')))
              {
                  listOfSlctedLyr = layerDesc.getList(app.stringIDToTypeID('targetLayers'));
                  var noOfSlctedLayer = listOfSlctedLyr.count;
                  for(var i=0; i < noOfSlctedLayer; i++)
                  {
                      if(isBackGroundPresent)
                          selectedLayers.push(listOfSlctedLyr.getReference(i).getIndex());
                      else
                          selectedLayers.push(listOfSlctedLyr.getReference(i).getIndex()+1);
                  }
              }
              else
              {
                  layerRef = new ActionReference();
                  layerRef.putProperty(app.charIDToTypeID("Prpr"), app.charIDToTypeID("ItmI"));
                  layerRef.putEnumerated(app.charIDToTypeID("Lyr "), app.charIDToTypeID("Ordn"), app.charIDToTypeID("Trgt"));
                  if(isBackGroundPresent)
                      selectedLayers.push(app.executeActionGet(layerRef).getInteger(app.charIDToTypeID("ItmI"))-1);
                  else
                      selectedLayers.push(app.executeActionGet(layerRef).getInteger(app.charIDToTypeID("ItmI")));
           
                  var layerVisibility = doc.activeLayer.visible;
                  if(layerVisibility == true)
                      doc.activeLayer.visible = false;
           
                  layerDesc = new ActionDescriptor();
                  listOfSlctedLyr = new ActionList();
                  layerRef = new ActionReference();
                  layerRef.putEnumerated(app.charIDToTypeID('Lyr '), app.charIDToTypeID('Ordn'), app.charIDToTypeID('Trgt'));
                  listOfSlctedLyr.putReference(layerRef);
                  layerDesc.putList(app.charIDToTypeID('null'), listOfSlctedLyr);
                  app.executeAction(app.charIDToTypeID('Shw '), layerDesc, DialogModes.NO);
           
                  if(doc.activeLayer.visible == false)
                      selectedLayers.shift();
           
                  doc.activeLayer.visible = layerVisibility;
              }
          }
          catch(e)
          {
              selectedLayers =  null;
          }
          return selectedLayers;
      }
      
      var sTime = new Date().getTime();
      var sourceItem = getSelectedLayers();
      var eTime = new Date().getTime();
      $.writeln (((eTime - sTime)/1000) + " sec");
      

       

      Every time, I see different time duration ranging from 0.177 sec to 0.450.

      I don't understand why it behaves like this? Shouldn't the value be same every time or approximate?

      What could be the reason of such difference?

       

      Thanks

        • 1. Re: Getting different time duration in executing same code on same layer!
          JJMack Most Valuable Participant

          Windows and Unix do other things as well as runnin Photoshop processes get interrupted and resumed

          • 2. Re: Getting different time duration in executing same code on same layer!
            _VG Level 1

            Thanks for the reply JJMack..

             

             

            I have one more question,

             

            I am doing following task,

            - Fetch the info of text/shape object.

            - Create a text item which have the info in it's content.

            - Doing some calculation to place the text object.

            - Creating circle and other shape objects and merging them.

            - Doing some other task using action listener code.

             

            I am using script to do the above task. It is taking some time approximately 7 to 8 sec to complete.

            My question is If I use plugin (c++ code) to execute these task, does it make any difference in terms of execution?

            Will it become fast?

             

            Thanks for any help..

            • 3. Re: Getting different time duration in executing same code on same layer!
              JJMack Most Valuable Participant

              I can't answer for sure for I have never used Adobe SDK so I do not know how efficient the Photoshop API are you processing loop needs to use several Photoshop features. The potential is surly there for the code is compiled and does not need to be read and interpreted with each iteration.   When I work for a living I wrote a Rex script to update a Phone book basically it added  corporation big shots nick name into the monthly update corporate phone book. Though I read the nickname file and read and wrote the phone book with  single reads to arrays and after processing the arrays a single write from an array the script took three hours to run looping through the arrays.  I choose Rex scripting for its syntax is PL1 like all I needed to do was define the variables I used in the script then compile it with the PL1 compiler.  The compiled version took three minuets to run. There were only three external API calls used the same as the script the rest time was the Phone book array loop. The compile version took a small fraction of the time the interpreted script version.