Hello
I looked again at this exercise.
I discovered that version 9 was seriously buggued.
This loop :
--
repeat with c from 1 to nbCols
copy {item i of item 1 of my |old list|, item i of item 2 of my |old list|, item i of item 3 of my |old list|, item i of item 4 of my |old list|, item i of item 5 of my |old list|} to end of my temp_list
end repeat
--
wasn't doing what it was supposed to do. It didn't build a list of five values named my temp_list but a list of five lists of five values.
So I edited it as version 9a1 :
--
(* version 9a1 *)
on run
set startingTime to current date
repeat 1000 times
set reordered_values to my main(2, 45, 2, 5, 3, 5)
end repeat
display dialog "required : " & (current date) - startingTime & " secondes"
reordered_values
end run
on main(row1, row2, col1, nbCols, swap1, swap2)
script main
property |old list| : {}
property |new list| : {}
property temp_list : {}
set my |new list| to {}
tell application "Numbers" to tell document "American Express 3.numbers" to tell sheet "Folha 9" to tell table "Transações"
set my |old list| to value of cells row1 thru row2 of columns col1 thru (col1 + nbCols - 1)
end tell
repeat with i from 1 to (row2 + 1 - row1)
set my temp_list to {}
--repeat with c from 1 to nbCols
copy {item i of item 1 of my |old list|, item i of item 2 of my |old list|, item i of item 3 of my |old list|, item i of item 4 of my |old list|, item i of item 5 of my |old list|} to my temp_list
--end repeat
set temp_item to item swap2 of my temp_list
copy item swap1 of my temp_list to item swap2 of my temp_list
copy temp_item to item swap1 of my temp_list
copy my temp_list to end of my |new list|
end repeat
return my |new list|
end script
run main
end main
--
Those reading carefully will see that it no longer repead the treatment ten times but one thousand times. This way I hoped that speed differences will appear.
On my machine and my sample time.
It did the job in 37 seconds (0.37 second for 10 times)
I was not satisfied because as is, the script is limited to a range of cells embedding five columns. I wanted more flexibility so I edited it again as 9a2 :
--
(* version 9a2 *)
on run
set startingTime to current date
repeat 1000 times
set reordered_values to my main(2, 45, 2, 5, 3, 5)
end repeat
display dialog "required : " & (current date) - startingTime & " secondes"
reordered_values
end run
on main(row1, row2, col1, nbCols, swap1, swap2)
script main
property |old list| : {}
property |new list| : {}
property temp_list : {}
set my |new list| to {}
tell application "Numbers" to tell document "American Express 3.numbers" to tell sheet "Folha 9" to tell table "Transações"
set my |old list| to value of cells row1 thru row2 of columns col1 thru (col1 + nbCols - 1)
end tell
repeat with i from 1 to (row2 + 1 - row1)
set my temp_list to {}
repeat with c from 1 to nbCols
copy item i of item c of my |old list| to end of my temp_list
end repeat
set temp_item to item swap2 of my temp_list
copy item swap1 of my temp_list to item swap2 of my temp_list
copy temp_item to item swap1 of my temp_list
copy my temp_list to end of my |new list|
end repeat
return my |new list|
end script
run main
end main
--
As you see, this time the individual row is built in a loop so, it would be easy to use it to treat a wider range.
It did the trick in 36 seconds.
As the difference of one secon may be in fact a 0.51 second one, I guess that it's meaningful.
But I have a question in head : what if the swap of two columns was made befgore building individual rows ?
This led me to version 9b1 :
--
(* version 9b1 *)
on run
set startingTime to current date
repeat 1000 times
set reordered_values to my main(2, 45, 2, 5, 3, 5)
end repeat
display dialog "required : " & (current date) - startingTime & " secondes"
reordered_values
end run
on main(row1, row2, col1, nbCols, swap1, swap2)
script main
property |old list| : {}
property |new list| : {}
set my |new list| to {}
tell application "Numbers" to tell document "American Express 3.numbers" to tell sheet "Folha 9" to tell table "Transações"
set my |old list| to value of cells row1 thru row2 of columns col1 thru (col1 + nbCols - 1)
end tell
set tempList to item swap2 of my |old list|
copy item swap1 of my |old list| to item swap2 of |old list|
copy tempList to item swap1 of my |old list|
repeat with i from 1 to (row2 + 1 - row1)
copy {item i of item 1 of my |old list|, item i of item 2 of my |old list|, item i of item 3 of my |old list|, item i of item 4 of my |old list|, item i of item 5 of my |old list|} to end of my |new list|
end repeat
return my |new list|
end script
run main
end main
--
Execution time is 37 seconds.
One more time I edited it to use a loop and got version 9b2 :
--
(* version 9b2 *)
on run
set startingTime to current date
repeat 1000 times
set reordered_values to my main(2, 45, 2, 5, 3, 5)
end repeat
display dialog "required : " & (current date) - startingTime & " secondes"
reordered_values
end run
on main(row1, row2, col1, nbCols, swap1, swap2)
script main
property |old list| : {}
property |new list| : {}
property temp_list : {}
set my |new list| to {}
tell application "Numbers" to tell document "American Express 3.numbers" to tell sheet "Folha 9" to tell table "Transações"
set my |old list| to value of cells row1 thru row2 of columns col1 thru (col1 + nbCols - 1)
end tell
set tempList to item swap2 of my |old list|
copy item swap1 of |old list| to item swap2 of my |old list|
copy tempList to item swap1 of my |old list|
repeat with i from 1 to (row2 + 1 - row1)
set my temp_list to {}
repeat with c from 1 to nbCols
copy item i of item c of my |old list| to end of my temp_list
end repeat
copy my temp_list to end of my |new list|
end repeat
return my |new list|
end script
run main
end main
--
Execution time : 37 seconds.
At last, I ran version v10 applying the job 1000 times :
--
(* version 10 *)
on run
set startingTime to current date
repeat 1000 times
set reordered_values to my main(2, 45, 2, 5, 3, 5)
end repeat
display dialog "required : " & (current date) - startingTime & " secondes"
reordered_values
end run
on main(row1, row2, col1, nbCols, swap1, swap2)
script main
property |old list| : {}
tell application "Numbers" to tell document "American Express 3.numbers" to tell sheet "Folha 9" to tell table "Transações"
set my |old list| to value of cells col1 thru (col1 + nbCols - 1) of rows row1 thru row2
end tell
repeat with i from 1 to (row2 + 1 - row1)
tell item i of my |old list|
set temp_item to item swap2
copy item swap1 to item swap2
copy temp_item to item swap1
end tell
end repeat
return my |old list|
end script
run main
end main
--
Execution time : 39 seconds.
It's a bit longer than the others but it's the one which I will keep because it's the one which is the easyest to read and to edit if the required change become more complex than a simple swap of two columns.
What is now clear, is that extracting the datas with a single call give us the ability to do the job 100 times faster than extracting them one by one.
CAUTION, we will not be able to do that for all needs. Some times we may need to check if a cell contain a value or a formula or to check the format of the cell. In such cases, working cell by cell would be required but, it's useful to know that in some cases, we may fasten the code dramatically.
Yvan KOENIG (VALLAURIS, France) lundi 28 juin 2010 15:09:00