0

this is my first post here, so i'm sorry if i didn't follow the rules

i recently learned python, i know the basics and i like writing famous sets and plot them, i've wrote codes for the hofstadter sequence, a logistic sequence and succeeded in both

now i've tried writing mandelbrot's sequence without any complex parameters, but actually doing it "by hand"

for exemple if Z(n) is my complexe(x+iy) variable and C(n) my complexe number (c+ik)

i write the sequence as {x(n)=x(n-1)^2-y(n-1)^2+c ; y(n)=2.x(n-1).y(n-1)+c}

from math import *
import matplotlib.pyplot as plt

def mandel(p,u):
    c=5
    k=5
    for i in range(p):
        c=5
        k=k-10/p
        for n in range(p):
            c=c-10/p
            x=0
            y=0
            for m in range (u):
                x=x*x-y*y + c
                y=2*x*y + k
                if sqrt(x*x+y*y)>2:
                    break
            if sqrt(x*x+y*y)<2:
                X=X+[c]
                Y=Y+[k]
        print (round((i/p)*100),"%")
    return (plt.plot(X,Y,'.')),(plt.show())

p is the width and number of complexe parameters i want, u is the number of iterations

this is what i get as a result :

https://i.stack.imgur.com/aPq1k.png

i think it's just a bit close to what i want.

now for my questions, how can i make the function faster? and how can i make it better ?

thanks a lot !

ivan.mylyanyk
  • 2,051
  • 4
  • 30
  • 37
SlashBow
  • 1
  • 1

1 Answers1

0

A good place to start would be to profile your code.

https://docs.python.org/2/library/profile.html

Using the cProfile module or the command line profiler, you can find the inefficient parts of your code and try to optimize them. If I had to guess without personally profiling it, your array appending is probably inefficient.

You can either use a numpy array that is premade at an appropriate size, or in pure python you can make an array with a given size (like 50) and work through that entire array. When it fills up, append that array to your main array. This reduces the number of times the array has to be rebuilt. The same could be done with a numpy array.

Quick things you could do though

if sqrt(x*x+y*y)>2:

should become this

if x*x+y*y>4:

Remove calls to sqrt if you can, its faster to just exponentiate the other side by 2. Multiplication is cheaper than finding roots.

Another thing you could do is this.

print (round((i/p)*100),"%")

should become this

# print (round((i/p)*100),"%")

You want faster code?...remove things not related to actually plotting it.

Also, you break a for loop after a comparison then make the same comparison...Do what you want to after the comparison and then break it...No need to compute that twice.

Tristan Maxson
  • 229
  • 4
  • 15
  • thanks for you answer. i actually break the first loop because i don't want to hear anything about sequences which modules go beyond 2, so i break it immediately instead of waiting for it to finish then i add the parameter to the plot – SlashBow Mar 28 '15 at 18:14