Created
March 17, 2013 14:05
-
-
Save rolisz/5181643 to your computer and use it in GitHub Desktop.
For Catalin with multiprocessing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from multiprocessing import Pool | |
__author__ = 'Roland' | |
from PIL import Image | |
#size of image | |
imgx = 600 | |
imgy = 400 | |
#make image buffer | |
image = Image.new("RGB", (imgx, imgy)) | |
# area of fractal | |
xa = -2.0 | |
xb = 2.0 | |
ya = -2.0 | |
yb = 2.0 | |
#define constants | |
max_iterations = 10 # max iterations allowed | |
step_derivat = 0.002e-1 # step size for numerical derivative | |
error = 5e-19 # max error allowed | |
# function will generate the newton fractal | |
def f(z): | |
return z * z + complex(-0.31, 0.031) | |
def calc_i(t): | |
x, y, zy = t[0], t[1], t[2] | |
zx = x * (xb - xa)/(imgx - 1) + xa | |
z = complex(zx, zy) | |
i = 0 | |
while i < max_iterations: | |
# make complex numerical derivative | |
dz = (f(z + complex(step_derivat, step_derivat)) - f(z)) / complex(step_derivat, step_derivat) | |
# Newton iteration see wikipedia | |
z0 = z - f(z) / dz | |
# stop to the error | |
if abs(z0 - z) < error: | |
break | |
z = z0 | |
#I use modulo operation expression to do RGB colors of the pixels | |
i += 1 | |
return x,y,i | |
if __name__ == '__main__': | |
points = [] | |
# draw derivate fractal for each y and x | |
for y in range(imgy): | |
zy = y * (yb - ya) / (imgy - 1) + ya | |
for x in range(imgx): | |
points.append((x, y, zy)) | |
pool = Pool(processes=7) | |
print(calc_i(points[0])) | |
temp = pool.map(calc_i, points) | |
results = [] | |
for t in temp: | |
results.append(t) | |
for x,y,i in results: | |
image.putpixel((x, y), (i % 8 * 16, i % 4 * 32,i % 2 * 64)) | |
#save the result | |
image.save("fractal.png", "PNG") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ time python img.py
real 0m33.833s
user 0m30.744s
sys 0m0.650s
$ time python frac.py
real 0m31.622s
user 0m29.473s
sys 0m0.192s
Se pare ca nu are efect pentru ca asa cum presupuneam si eu , transferul datelor catre multiprocessing cere resurse. uite rezultatul scriptul tau incetineste mai mult randarea finala . Poate iti faci si tu o parere si poate devine un studiu de caz. Eu ma asteptam la ceva gen randare din Blender 3D cu portiuni separate si apoi asamblare. Oare exista o solutie mai eficienta daca poate s-ar folosi threaduri compinat cu multiprocessing . multumesc pentru ajutor.
vezi ca exista si un grup python : http://groups.google.com/group/python-romania unde se discuta probleme de python.