Speed
Beat Cornaz
B.Cornaz at gmx.net
Fri Aug 22 16:17:44 EDT 2014
Thanks Richard,
So it depends on a number of factors, which makes it a bit harder. I had hoped for some general rules, like always use repeat for if possible or words are generally slower that items. But I understand, it depends on more things.
I have some hundred fitness functions, which need to be recalculated many times. But I think the problem of making Permutations might be illuminating for me, as this one takes quite long to run (with 10 or more elements to Permutate).
So a good example would be how to make all possible permutations of say 10 different elements (0-9). This gives 3628800 different permutations.
The resulting permutations will be in lines I guess, but what would be the best way to do this. Using lines, items, chars or any other way as Input?
So the start would be f.i. a list with 10 lines with the ten elements, one in each line - That is what I am using now.
OR items "0,1,2,3,4,5,6,7,8,9" or chars "0123456789" or another way to represent 10 elements to make it possible to run the script in minutes, not hours.
the code I have now for this is follows - lines as input (one line per element) :
function BC4_PermMech_Lines ToPermutate -- ** ToPermutate : all the elements to permutate, one per line
put the number of lines of ToPermutate into NumInput
put empty into TempPerms5
-- *** First calc the upper half.
put line 1 of ToPermutate & comma & line 2 of ToPermutate & comma into TempPerms1
put line 3 to -1 of ToPermutate into tInput3
put 3 into Index
repeat for each line newElement in tInput3
set the cursor to busy
repeat for each line tLine in TempPerms1
repeat with y = Index down to 1 -- repeat with y = x down to 1
put tLine into Temp
put newElement & comma before item y of temp
put Temp & cr after TempPerms5
end repeat
end repeat
delete char -1 of TempPerms5 -- ** last cr
put TempPerms5 into TempPerms1
put empty into TempPerms5
add 1 to Index
end repeat
-- *** Now the second half
-- *** First calc the upper half. Later copy and inverse all (alternative idea)
put line 2 of ToPermutate & comma & line 1 of ToPermutate & comma into TempPerms2
put 3 into Index
repeat for each line newElement in tInput3
set the cursor to busy
repeat for each line tLine in TempPerms2
repeat with y = Index down to 1
put tLine into Temp
put newElement & comma before item y of temp
put Temp & cr after TempPerms5
end repeat
end repeat
delete char -1 of TempPerms5 -- last cr
put TempPerms5 into TempPerms2
put empty into TempPerms5
add 1 to Index
end repeat
put cr & TempPerms2 after TempPerms1
choose browse tool
Return TempPerms1
end BC4_PermMech_Lines
-----
Another example I am struggling with is to delete exact duplicate lines in a long, long list. Do I best use chars, items or words in the lines of the list?
As example can be thought the deletion of duplicate permutations if you start with 10 elements, but there are duplicate elements in the start, e.g. 0011123456 .
This would give 'only' 302400 permutations, so there are 60480 duplicate perms to be deleted.
There might be an algorithm, which makes the correct permutations right away (in the case of duplicate elements), without first making all the general permutations and then deleting the duplicates.
That would be really great and I'd love to hear it if someone knows an algorithm for those permutations !!!!
But even so, I am also interested in de fastest way to delete duplicate lines anyway for other parts of my program.
At the moment I use :
--
function BC2_RemoveDuplicate_Lines pData
repeat for each line rLine in pData
add 1 to TestArray[rLine]
end repeat
return the keys of TestArray
end BC2_RemoveDuplicate_Lines
--
I hope someone knows a faster way :-)
Thanks a lot, Beat
More information about the use-livecode
mailing list