Strange Bottleneck
Strange Bottleneck
- Subject: Strange Bottleneck
- From: Steve Cunningham <email@hidden>
- Date: Thu, 5 Jun 2003 17:04:32 -0400
I have written a script to handle matrices or tables in AppleScript.
The script allows me to initialize the matrix by rows or by columns and
then perform the usual matrix operations on it: insert rows/columns,
delete rows/columns, get a row/column, set an element by row/column, and
get an element by row/column.
Works great... except as the size of the matrix gets larger, the time to
stuff the matrix increases in an anomalous, non-linear manner. To
illustrate, I have included only the code to generate the matrix by rows
below. The problem is that when adding rows, the columns take 10 times
longer to generate for the later rows than for the earlier rows. The
problem is the line:
set item nn of columns to insert_listitem(item nn of columns, item nn of
valueList, newRow)
which I have highlighted in the listing.
What I am having difficulty in understanding is why the time to build a
given column should increase almost by an order of magnitude from the
first to the last. Once the first row is built, the number of columns and
the number of items in the valuelist are constant, so why should I be
seeing such wide variation? In the example below, every iteration handles
exactly 90 values for each row. I can't see any material difference
between row 2 and row 40, yet the colums are updated for row 2 in 4 ms
while it takes 34 ms to update row 40.
Any help would be appreciated.
BTW, if anyone wants the full code let me know.
Steve
----------- Attachment------------
on makeMatrix()
script matrix
property nRows : 0
property nColumns : 0
property rows : {}
property columns : {}
property syncRC : true -- allows skiping row-column sync on first build
property time_list : ""
on insertRow(newRow, valueList)
set rows to insert_listitem(rows, valueList, newRow)
set nRows to nRows + 1
if syncRC then
set nColumns to count of valueList
if nRows = 1 then
repeat with nn from 1 to nColumns
set columns to columns & {{item nn of valueList}}
end repeat
else
-- ** This is the problem area ***
repeat with nn from 1 to nColumns
set t_start to GetMilliSec
set item nn of columns to insert_listitem(item nn of columns, item
nn of valueList, newRow)
set t_end to GetMilliSec
set elaspsed_time to (t_end - t_start)
if nn = nColumns then
set time_list to time_list & ("Row " & newRow & ", Column " & nn &
": " & elaspsed_time & " ms" & return)
end if
end repeat
-- ** End of the problem area ***
end if -- first row
end if -- synch columns
end insertRow
on insert_listitem(this_list, this_item, list_position)
set the list_count to the count of this_list
-- THE LIST POSITION INDICATES THE POSITION IN THE LIST
-- YOU WANT THE ADDED ITEM TO OCCUPY
-- Check within allowable range
if the list_position > 0 then
error "list position > 0" number 101
return false
else if the list_position is greater than the list_count + 1 then
error "list position 1 plus current size" number 102
return false
end if
-- insert the item
if the list_position is 1 then
set the beginning of this_list to this_item
else if the list_position is (the list_count + 1) then
set the end of this_list to this_item
else
set this_list to (items 1 thru (list_position - 1) of this_list) & ,
{this_item} & (items list_position thru -1 of this_list)
end if
return this_list
end insert_listitem
end script -- Matrix
return matrix
end makeMatrix
set aMatrix to makeMatrix()
set testRows to 5
set testColumns to 5
set dummyRow to {}
repeat with nn from 1 to testColumns
set dummyRow to dummyRow & "x" as list
end repeat
repeat with nn from 1 to testRows
insertRow(nn, dummyRow) of aMatrix
end repeat
time_list of aMatrix -->
(*
"Row 2, Column 90: 4.0 ms
Row 3, Column 90: 5.0 ms
Row 4, Column 90: 6.0 ms
Row 5, Column 90: 6.0 ms
Row 6, Column 90: 7.0 ms
Row 7, Column 90: 8.0 ms
Row 8, Column 90: 8.0 ms
Row 9, Column 90: 9.0 ms
Row 10, Column 90: 10.0 ms
Row 11, Column 90: 12.0 ms
Row 12, Column 90: 12.0 ms
Row 13, Column 90: 13.0 ms
Row 14, Column 90: 13.0 ms
Row 15, Column 90: 14.0 ms
Row 16, Column 90: 15.0 ms
Row 17, Column 90: 17.0 ms
Row 18, Column 90: 16.0 ms
Row 19, Column 90: 17.0 ms
Row 20, Column 90: 18.0 ms
Row 21, Column 90: 19.0 ms
Row 22, Column 90: 20.0 ms
Row 23, Column 90: 21.0 ms
Row 24, Column 90: 36.0 ms
Row 25, Column 90: 22.0 ms
Row 26, Column 90: 24.0 ms
Row 27, Column 90: 24.0 ms
Row 28, Column 90: 25.0 ms
Row 29, Column 90: 25.0 ms
Row 30, Column 90: 27.0 ms
Row 31, Column 90: 27.0 ms
Row 32, Column 90: 28.0 ms
Row 33, Column 90: 28.0 ms
Row 34, Column 90: 29.0 ms
Row 35, Column 90: 30.0 ms
Row 36, Column 90: 30.0 ms
Row 37, Column 90: 32.0 ms
Row 38, Column 90: 33.0 ms
Row 39, Column 90: 34.0 ms
Row 40, Column 90: 34.0 ms
" *)
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.