Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Is there a better way to achieve this?  (Potential performance problems with loops and delay???)

Posted on 2004-12-01
7
Medium Priority
?
495 Views
Last Modified: 2013-12-24
Hi everyone.  Here's the scenario:  I'm creating an application which allows users to select multiple clients and prefill PDFs (with info pulled from a datasource).  (User can also choose to complete multiple forms for multiple clients).  The following code IS working, but this application is going to be used exstensively and I'm concerned about performance.  Does anyone have any suggestions for improving the code below or is it as tight as it can be?  Thanks for any advice.



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Forms Selector</title>
<LINK rel="stylesheet" type="text/css" href="/images/FRWS.css" title="public">
</head>
<body TOPMARGIN="0" LEFTMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0">
<CFFLUSH>
<div id="Layer1" style="position:absolute; width:560px; height:115px; z-index:1; left: 30px; top: 0px; visibility: show">
    <OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab##version=6,0,0,0" ID="loading" WIDTH="160" HEIGHT="50" ALIGN="">
         <PARAM NAME=movie VALUE/loading.swf">
         <PARAM NAME=quality VALUE=high>
         <PARAM NAME=wmode VALUE=transparent>
         <PARAM NAME=bgcolor VALUE=FFFFFF>  
         <EMBED src="/loading.swf" quality=high wmode=transparent bgcolor=FFFFFF  WIDTH="160" HEIGHT="50" swLiveConnect=true ID="loading" NAME="loading" ALIGN="" TYPE="application/x-shockwave-flash" PLUGINSPAGE="https://www.macromedia.com/go/getflashplayer"></EMBED>
    </OBJECT><div align="center"><h1>SHORT FLASH MOVIE DISPLAYING SOMETHING TO INDICATE CHOSEN FORMS ARE BEING COMPLETED</h1></div>
</div>
<CFFLUSH>


<!---### REMOVE ANY PREVIOUSLY GENERATED FORMS, THEY ARE NOT SAVED ON THE SERVER ###--->
<cfif NOT DirectoryExists("D:\ftpusers\#request.userID#\Forms\")>
     <cfdirectory action="CREATE" directory="D:\ftpusers\#request.userID#\Forms\">
</cfif>
<cfset VAR.FormPath = "D:\ftpusers\#request.userID#\Forms\">
<cfdirectory action="LIST" directory="#VAR.FormPath#" name="RemoveFiles">
<cfoutput query="RemoveFiles">
     <cfset FileName = "#VAR.FormPath##RemoveFiles.Name#">
     <cfif FileExists("#FileName#")>
          <cffile action="DELETE" file="#FileName#">
     </cfif>      
</cfoutput>
<cfset VAR.InvestorList="#FORM.ClientList#">
<!---### RETRIEVE LIST OF CLIENTS ###--->
<cfset Count="1">
<cfset VAR.List="">
<cfquery name="getClients" datasource="MyDatasource">
     SELECT      FIRST_NAME,LAST_NAME,TAX_ID_NUMERIC
     FROM       dbo.Clients
     WHERE      Client_ID IN (#VAR.InvestorList#)
</cfquery>
<cfset Count=#Count#>
<!---### LOOP CLIENTS AND RETRIEVE SELECTED FORMS FOR COMPLETION ###--->
<cfloop query="getClients">
<cfset FormsList="#FORM.FormsList"">
<cfset Count="#Count#">
<cfset VAR.List="#VAR.List#">    
<cfloop index="i" list="#FormsList#" delimiters=" ">
<!---### CREATE FDF FOR EACH CLIENT AND EACH FORM ###--->
<cfset cr = chr(13)>
<cfset lf = chr(10)>
<cfset crlf = cr & lf>
<cfset FDF_String = "%FDF-1.2" & cr & "%âãÏÓ" & crlf & "1 0 obj" & cr & "<< " & cr & "/FDF << /Fields [ ">
<cfset FDF_String = FDF_String & "<< /V (#getClient.TAX_ID_NUMERIC#)/T (SSN)>> ">
<cfset FDF_String = FDF_String & "<< /V (#getClient.FIRST_NAME#)/T (FirstName)>> ">
<cfset FDF_String = FDF_String & "<< /V (#getClient.LAST_NAME#)/T (LastName)>> ">
<cfset FDF_String = FDF_String & " " & cr & "] " & cr & "/F (#application.root#/forms/forms/#i#.pdf) >> " & cr & ">> " & cr>
<cfset FDF_String = FDF_String & "endobj" & cr & "trailer" & cr & "<<" & cr & "/Root 1 0 R " & cr & cr & ">>" & cr & "%%EOF" & cr>
<!---### WRITE FDF ###--->
<cffile action="WRITE"
        file="#VAR.FormPath##Count#.fdf"
        output="#FDF_String#"
        attributes="Normal"
        addnewline="Yes">
<!---### FILL PDF WITH CLIENT INFO ###--->
 <cfexecute
          name="D:\Httpdocs\field\eTouch\pdftk\pdftk.exe"
          arguments="D:\Httpdocs\field\FORMS\Forms\#i#.pdf fill_form #VAR.FormPath##Count#.fdf output #VAR.FormPath##Count#.pdf" timeout="100">
</cfexecute>    
<!---### CREATE THE LIST OF FORMS TO BE MERGED ###--->
<cfset VAR.List=#ListAppend(VAR.List, "#VAR.FormPath##Count#.pdf")#>
<cfset VAR.list = #replace(VAR.list, ",", " ", "ALL")#>
<cfset Count=#Count#+1>
</cfloop>    
<cfset Count=#Count#>
</cfloop>
<!---### MERGE ALL FORMS ###--->
 <cfexecute
          name="D:\HTTPDOCS\FIELD\forms\forms\pdftk.exe"
          arguments="#VAR.List# cat output #VAR.FormPath#merged.pdf" timeout="100">
</cfexecute>  
<!---### SET DELAY TO ALLOW FOR COMPLETION OF MERGE ###--->
<cfset delay = 1000>
<cfset sTime = gettickcount()>
<cfloop condition="gettickcount() lt sTime + delay">
<!---### IF THE MERGED FILE EXISTS OPEN MERGED PDF ###--->
     <cfif FileExists("#VAR.FormPath#merged.pdf")>
     <cfflush>
          <cfoutput>
               <script>
               window.location = '#application.root#users/#request.userID#/Forms/Merged.pdf';
               </script>
          </cfoutput>
     </cfif>
</cfloop>    


</body>
</html>
0
Comment
Question by:whaleyk
  • 4
  • 3
7 Comments
 
LVL 35

Accepted Solution

by:
mrichmon earned 1000 total points
ID: 12717953
Yes - you have a lot of lines that do nothing, lines that are inefficient and lines that have errors  (for example an opening # but no closing one after the variable)

Do not use # inside cf tags unless in double quotes - and only use double quotes when required

for example:
<cfset FormsList="#FORM.FormsList""> should be <cfset FormsList=FORM.FormsList>
<cfset Count=#Count#>  should be <cfset Count=Count>

but this is pointless as you are assigning the variable to itself - this line should be removed

There are a lot of lines that assign a variable to itself which is useless....
0
 

Author Comment

by:whaleyk
ID: 12719438
Hi mrichmon, thanks for your feedback.  I actually know better regarding the # sounds inside the cf tags... (thanks for the reminder!!! Does it really make that much of a difference as far as performance???)  (I doctored the code up a little when i posted, so the missing # sign just came about as a result of that... the code wouldn't work if i actually was missing it.)

Not too sure what you mean about assigning the variable to itself being pointless.  The "count" is set outside the first loop and then needs to be "fed" within the second loop... i'll take a look tho and see if i can understand what you mean...

thanks very much for your time :-)
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 12719564

>>Does it really make that much of a difference as far as performance???

In most cases no - but in places where you have a large number of hits and/or efficiency is important because your code will be run a lot (like large loops) then yes.

But in most cases the difference is very minor and so visitors to your site wouldn't notice.


>>Not too sure what you mean about assigning the variable to itself being pointless.

This line:
<cfset Count = Count>  doesn't do anything.  If count was 1 it is still 1.

This line is useful
<cfset Count = Count + 1>


Now if your query has a variable called count then you really need to scope it such as:

<cfset Count = Queryname.Count>
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:whaleyk
ID: 12719985
ha ha... sorry i missed the top line off :-)
<cfset Count="#RandRange(1000000,9999999)#">

Thanks for taking a look, i really appreciate your advice and happily award you the points for taking a look.

Kind Regards,
K
0
 

Author Comment

by:whaleyk
ID: 12719996
Actually, i didn't miss it off, i just copied in the wrong template!!!!#@! :-)
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 12720609
That makes a difference :o)

But do this instead:

<cfset Count=RandRange(1000000,9999999)>

No double quotes and no # needed
0
 

Author Comment

by:whaleyk
ID: 12720869
oops... yess... thanks again :-)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A web service (http://en.wikipedia.org/wiki/Web_service) is a software related technology that facilitates machine-to-machine interaction over a network. This article helps beginners in creating and consuming a web service using the ColdFusion Ma…
Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month21 days, 1 hour left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question