Thank you, Stefan.

It is true that my programming style is heavily influenced by years of 
Matlab (and I'm not a good programmer in anything). However I'm staying 
with Julia, so the learning will come.

For the time being I will try to find out why my function allocates so much 
memory (228,884,140 bytes to generate all the permutations of 1:8, while 
the algorithm, as I understand it, should always be treading with the same 
few memory positions...)

Regards.

P.S. I have tried using in-place
   sort!(p[fp:end])
rather than
   p[fp:end] = sort(p[fp:end])
but it does not do what I expected.

I also think my function should be type-stable (don't see where a new type 
could arise) and tried modifying some type declarations, to no avail.


On Monday, October 27, 2014 3:41:00 PM UTC+1, Stefan Karpinski wrote:
>
> There are a number of idioms here that Matlab has heavily optimized that 
> we haven't because there are more straightforward (if not always easier to 
> use) alternatives in Julia. In particular, the Matlabism of passing an 
> array and then assigning back to it is identified and translated into an 
> in-place operation in Matlab. This optimizations is relatively easy in 
> Matlab because of its pass-by-value semantics – you know your "copy" is not 
> going to affect anyone else's, so you can modify it as long as that 
> modification doesn't change the meaning of *this* code. Of course, 
> copy-on-write causes major performance issues in many other situations, but 
> this is one of those situations where the pass-by-value semantics are 
> helpful. In Julia, these are going to do literally what they appear to do, 
> which is often quite slow. Examples of such idioms in this code:
>
> w = find(diff(p).>0)    # positions that are still Well sorted
>
> p[w:end] = sort(p[w:end])   # sort elements that have to be rearranged 
> (sort() should be fast)
>
> p[w:end] = p[[i, w:i-1, i+1:n]]
>
> If I get a chance later, I'll take a crack at writing some of these in a 
> more Julian style. In general, if there's C code implementing an algorithm 
> like this, you'll find it's much easier to port that to Julia and get 
> similar performance.
>
> On Mon, Oct 27, 2014 at 8:50 AM, Felipe Jiménez l <[email protected] 
> <javascript:>> wrote:
>
>> Hi!
>>
>> I have programmed a very simple function that takes any permutation p of 
>> [1:n] and finds the next permutation in dictionary order. I know this is 
>> well-programmed already, but I am learning.
>>
>> I have programmed the same algorithm both in Julia and Matlab. It is not 
>> a sophisticated algorithm, but at least I expected it to run faster in 
>> Julia than Matlab. Quite the contrary.
>>
>> I attach the .jl file, as well as the corresponding Matlab mfile, if 
>> someone wants to test it.
>>
>> Thank you in advance.
>>
>> P.S. Since collect(permutations(1:n)) seems to return them in dictionary 
>> order, I guess permutations() somehow should be able to give me the next 
>> permutation, but I don't know how to do it.
>>
>>
>

Reply via email to