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]> 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. > >
