Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to improve the performance?

Status
Not open for further replies.

anitalulu

Programmer
Nov 19, 2002
23
HK
There are 2 arrays. arrPermissionDeny is to store the function id which is denied to the user. arrPermissionAccess is to store the function id which is allowed to the user. After the following programming has been run, a final permission access is expected.
The logic of the programming is ok. However, the performance is very bad.

<cfloop index=&quot;i&quot; from=&quot;1&quot; to=&quot;#ArrayLen(arrPermissionDeny)#&quot;>
<cfloop index=&quot;j&quot; from=&quot;1&quot; to=&quot;#ArrayLen(arrPermissionAccess)#&quot;>
<cfif arrPermissionDeny[1] eq arrPermissionAccess[j][1]>
<cfset tmp = ArrayDeleteAt(arrPermissionAccess,j)>
<cfbreak>
</cfif>
</cfloop>
</cfloop>

Is there any suggestion to improve the performance?
 
Two possible suggestions... which may or may not work depending on your array structure (it looks like it's a 2-dimensional array... but it's unclear why, or what's in the other child elements).

First, you could use structures instead of arrays. If you set up structures, like:
Code:
  strPermissionDeny[&quot;some_function_id&quot;]
           :
and
Code:
  strPermissionAccess[&quot;some_function_id&quot;]
           :

then you would only have to have one loop, and use StructFind to see if there was a match:
Code:
<CFLOOP collection=&quot;#strPermissionDeny#&quot; item=&quot;whichFunction&quot;>
   <CFIF StructFind(strPermissionAccess,whichFunction)>
      <CFSET StructDelete(strPermissionAccess, whichFunction)>
   </CFIF>
</CFLOOP>
or, even faster, since StructDelete() doesn't throw an error if the key you're trying to delete doesn't actually exist, you could do without the StructFind():
Code:
<CFLOOP collection=&quot;#strPermissionDeny#&quot; item=&quot;whichFunction&quot;>
   <CFSET StructDelete(strPermissionAccess, whichFunction)>
</CFLOOP>


Second option, if structures won't work for you, use ArrayToList() to temporarily make the array a little easier to manage:
Code:
<CFSET lstDeny = ArrayToList(strPermissionDeny)>
<CFSET lstAccess = ArrayToList(strPermissionAccess)>

<CFLOOP list=&quot;#lstDeny#&quot; index=&quot;whichFunction&quot;> 
  <CFSET nFunctionPos = ListFindNoCase(&quot;#strPermissionAccess#&quot;,whichFunction)>
  <CFIF nFunctionPos GT 0>
     <CFSET lstAccess = ListDeleteAt(&quot;#lstAccess#&quot;,nFunctionPos)>
  </CFIF>
</CFLOOP>


Of course, I might also look at why you're using two arrays. If you were to use a structure, you could simply store a flag as to whether a given function was granted or denied... and keep it all in the same object:
Code:
<CFSCRIPT>
   strFunctionAccess = StructNew();
   strFunctionAccess[&quot;1234&quot;] = true;
   strFunctionAccess[&quot;5678&quot;] = false;
   strFunctionAccess[&quot;ABCD&quot;] = true;
   strFunctionAccess[&quot;EFGH&quot;] = true;
</CFSCRIPT>

so this user would be granted permission to use functions &quot;1234&quot;,&quot;ABCD&quot;, and &quot;EFGH&quot;... but not &quot;5678&quot;. So when it comes time to check whether a function should process or be displayed, you simply do:
Code:
  <CFIF strFunctionAccess[&quot;#function_id#&quot;]>
     <!--- perform functionality --->
          :
  <CFELSE>
     Sorry, you don't have permission to do that!
  </CFIF>

You can easily store other information about the function, if you wish (like I guess you're doing in the other dimension of your current array), by making the element in the structure a child/nested structure:
Code:
<CFSCRIPT>
   strFunctions = StructNew();
   strFunctions[&quot;1234&quot;] = StructNew();
   strFunctions[&quot;1234&quot;][&quot;permitted&quot;] = true;
   strFunctions[&quot;1234&quot;][&quot;name&quot;] = &quot;Delete Record&quot;;
   strFunctions[&quot;1234&quot;][&quot;url&quot;] = &quot;/utilities/delete_record.cfm&quot;;
   strFunctions[&quot;1234&quot;][&quot;email&quot;] = &quot;admin@mydomain.com&quot;;
      :
</CFSCRIPT>


A third option could be to set up a query... if that would be advantageous. So you'd have a resultset like:
Code:
function_id   permitted   name
------------------------------------------------
1234          y           delete record
5678          n           update record

you can do this dynamically with something like:
Code:
<CFSCRIPT>
    qryFunctions = QueryNew(&quot;function_id, permitted, name&quot;);
    QueryAddRow(qryFunctions,1);
    QuerySetCell(qryFunctions,&quot;function_id&quot;,&quot;1234&quot;,1);
    QuerySetCell(qryFunctions,&quot;permitted&quot;,&quot;y&quot;,1);
    QuerySetCell(qryFunctions,&quot;name&quot;,&quot;delete record&quot;,1);

    QuerySetCell(qryFunctions,&quot;function_id&quot;,&quot;5678&quot;,1);
    QuerySetCell(qryFunctions,&quot;permitted&quot;,&quot;n&quot;,1);
    QuerySetCell(qryFunctions,&quot;name&quot;,&quot;update record&quot;,1);
              :
</CFSCRIPT>

then you'd only have to run a query-on-query, like:
Code:
   <CFQUERY name=&quot;qryPermitted&quot; dbtype=&quot;query&quot;>
      SELECT *
       FROM qryFunctions
      WHERE function_id = '#sFunctionID#'
   </CFQUERY>

   <CFIF qryPermitted.RecordCount GT 0 AND qryPermitted.permitted EQ 'y'>
        <!--- proceed --->
   <CFELSE>
        Sorry, you're not permitted to do that.
   </CFIF>


-Carl
 
Thank you very much.
Your explaination is very clear. I will use structure to store the permission.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top