Speed

Geoff Canyon gcanyon at gmail.com
Mon Aug 25 18:32:41 EDT 2014


This routine will permute to any depth that memory/time allows. It has the
added benefit of using characters starting from any ASCII value you like,
allowing you to work around what you want to eventually permute using the
PLines routine.

It also has the speed benefit you get from using items rather than numbers,
which I think is because it avoids casting numbers to strings (as much).

I'll think about the permutations with duplicates aspect.

function P2 N,B
   -- N is the depth to permute
   -- B is the ASCII value to start from
   -- so P2(1,49) returns 21 cr 12
   -- P2(2,53) returns 675 cr 765 cr 756 cr 576 cr 657 cr 567
   if N = 0 then return numToChar(B) & cr
   put P2(N - 1,B) into T
   add B to N
   repeat with i = B to N - 1
      put T into T2
      replace numToChar(i) with numToChar(N) in T2
      replace cr with numToChar(i) & cr in T2
      put T2 after R
   end repeat
   replace cr with numToChar(N) & cr in T
   return R & T
end P2




On Mon, Aug 25, 2014 at 3:14 PM, Beat Cornaz <B.Cornaz at gmx.net> wrote:

> Now for the permutations.
>
> Geoff, great. Your script is by far the fastest. Almost 10 times faster
> than my own script which comes second.
> I could improve a little bit even on your script with the suggestion of
> Kay.
>
> --
> Kay wrote :
>
> I obtained a 10% speed increase by changing this:
>
>  repeat with n = 3 to 10
>
> to this:
>
>     put "3,4,5,6,7,8,9,10" into nList
>     repeat for each item n in nList
> --
>
> Geoff, you mentioned that I would not be to hard to expand the script
> beyond 10 elements. I been trying, but no dice yet. Any pointers?
>
> I also tried to implement a fast way to permutate elements which contain
> duplicates (like 00112)
> This would give only 30 distinct permutations i.s.o. 120. Of course, I
> could run the script and replace all the 1s with 0s, all the 2's with 1's,
> etc. and then delete the duplicates.
> But if I could delete duplicates much earlier on, it would be much faster.
> I think I know where to delete the duplicates in the script, but cannot
> get yet your script running by inputting a list like "0,0,1,1,2" ( or 00112
> ) and use the trick Kay suggested.
> It works if I rework N to a  list like  "01234". I will sleep over it :-)
>
> Thanks a lot Geoff,
>
> Beat
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>



More information about the use-livecode mailing list