Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I have this foreach loop that I am trying to optimize for a large flat file database.
The database is a table with rows stored in the @project_module_list array and columns separated by commas.
When running on a small 50 row database, it spits the html out pretty fast. However, when running on an 800 row database, it takes an average of 9 seconds to finish running.
Can someone help me optimize this loop?

P.S. Each column has its data separated by a colon. Column element 0 is the column data, element 1 is the user who last edited it, and element 2 is the time stamp of the last edit.
Maybe there is just too much data to process it faster?

Here is the loop code:

foreach $project_module_list (@project_module_list) { if($moduleCount != 0) { @moduleData = split /,/, $project_module_list; print "<tr><td align=center><div class=table_data>$moduleCount +</div></td>"; $dataCount = 0; foreach $moduleData (@moduleData) { @moduleField = split /:/, $moduleData; $recentChange = ""; $_ = $headers[$dataCount]; $localtime = FormatTime($moduleField[2]) if $moduleField[2 +]; $localtime = "n/a" unless $moduleField[2]; if( /approv/i ) { $status = "N/A"; if($moduleField[0]) { $default = "checked"; $status = "$moduleField[1]"; $color = " bgcolor=\"#CCFF99\""; } else { $default = ""; $color = " bgcolor=\"#FFCC66\""; } if($moduleField[2] > time - $changeBuffer) { $recentChange = " bgcolor=\"#99CCFF\""; } print "<td$color align=center><div class=table_data><i +nput type=checkbox name=\"$headers[$dataCount]$moduleCount\" $default +></div></td><td$recentChange title=\"Changed by $moduleField[1] at $l +ocaltime\"><div class=table_data>$status</div></td>"; } else { if($moduleField[2] > time - $changeBuffer) { $recentChange = " bgcolor=\"#99CCFF\""; } print "<td$recentChange><div title=\"Changed by $modul +eField[1] at $localtime\" class=table_data onClick=\"ToggleShowHide(' +label_$headers[$dataCount]$moduleCount','$headers[$dataCount]$moduleC +ount')\" id=\"label_$headers[$dataCount]$moduleCount\" style='display +: block;'>$moduleField[0]</div> <div id=\"$headers[$dataCount]$module +Count\" style='display: none;'><input type=text name=\"$headers[$data +Count]$moduleCount\" value=\"$moduleField[0]\"><input type=button onC +lick=\"ToggleShowHide('label_$headers[$dataCount]$moduleCount','$head +ers[$dataCount]$moduleCount')\" value=OK></div></td>"; } $dataCount++; } print "</tr>\n"; } $moduleCount++; $default = $color = ""; }

In reply to Foreach Loop Optimization by upallnight

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-26 00:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found