The code is well-typed and allocates only the expected `ham` array, so the
performance tips won't help here, I'm afraid. Almost all of the time in the
Julia version is being spent in the zeros call:

[image: Inline image 2]
We currently allocate an uninitialized array and then call the bzero
function to fill it with zeros – this takes a long time for large arrays.
We should instead mmap /dev/zero since that allows the kernel to lazily
allocate pages. I suspect this is what NumPy is doing, which in this
particular benchmark is an increasingly large benefit since huge chunks of
this matrix remain completely zeroed out and can all share a single actual
memory page. There's a very old (closed) issue about this that I've
reopened: #130 <https://github.com/JuliaLang/julia/issues/130>.

Also: I don't think this matters much but in the Julia version, the line

ham[bra+1,bra+1] = diag_term


is outside the `for s in 0:N-2` loop whereas in the Python version, the
corresponding line is inside the loop. That favors the Julia version,
however.

On Tue, Mar 8, 2016 at 4:08 PM, Kevin Squire <[email protected]> wrote:

> Hi Jérémy,
>
> Have you had the chance to read through the Julia performance tips yet?
>
> http://docs.julialang.org/en/release-0.4/manual/performance-tips/
>
> Cheers,
>   Kevin
>
>
> On Tuesday, March 8, 2016, Jérémy Béjanin <[email protected]>
> wrote:
>
>> Hello,
>>
>> By curiosity, I have translated a simple algorithm from python to julia
>> in order to compare their performance (gist here
>> <https://gist.github.com/jebej/307cba73c5c1025399d3>). I am still a
>> relative newcomer and so am not sure why I am seeing worse performance from
>> julia.
>>
>> The code was not optimized for python (or julia for that matters).
>>
>> The code generates a Hamiltonian for a 1D transverse field ising model,
>> and I am varying the number of sites which makes the size of the matrix
>> grow exponentially. Here are the timings I get:
>>
>>
>> <https://lh3.googleusercontent.com/-Zia04bwFUtM/Vt8829M6VtI/AAAAAAAAFao/MF3HuEhP5w8/s1600/tfimbench.png>
>>
>> As can be seen, Julia is faster for lower number of sites, but that
>> changes at N=13. Also interesting is the CPU and memory use during those
>> runs. It seems that Julia uses memory much more aggressively:
>>
>>
>> <https://lh3.googleusercontent.com/-JkKSGRcoeYY/Vt89OvZr-HI/AAAAAAAAFas/ZgiuJSnNo6Q/s1600/CPUandRAM.PNG>
>>
>

Reply via email to