set-of-julia-in-python

演示分形几何(fractal geometry)对比Python和Julia的性能

Python

In [1]:
def julia(z, c, maxiter=200):
    for n in xrange(maxiter):
        if abs(z) > 4:
            return n-1
        
        z = z*z + c
    
    return maxiter
In [2]:
import numpy as np
In [3]:
%%time
for i in np.r_[1:-1:-.002]:
    for r in np.r_[-1.5:1.5:0.002]:
        julia(complex(r,i), complex(-.06, .67))
        
CPU times: user 10.6 s, sys: 59.6 ms, total: 10.6 s
Wall time: 10.6 s

Julia

In [2]:
# julia set
# (the familiar mandelbrot set is obtained by setting c==z initially)
# function julia(z, c; maxiter=1000)
#     for n = 1:maxiter
#         if abs2(z) > 4
#             return n-1
#         end
#         z = z*z + c
#     end
#     return maxiter
# end
In [5]:
#@time m = [ uint8(julia(complex(r,i), complex(-.06,.67))) for i=1:-.002:-1, r=-1.5:.002:1.5 ];
In [ ]:
#X, Y = np.ogrid[1:-1:-.002, -1.5:1.5:.002]
#m = [[ int(julia(complex(r,i), complex(-.06,.67))) for i in np.arange(1, 1,-.002)] 
#     for r in np.arange(-1.5, 1.5, 0.002)];
In [6]:
# elapsed time: 0.247828677 seconds (1502560 bytes allocated)

Time cost

In [1]:
10.6 / 0.247828677
Out[1]:
42.771482817543344