On 18/05/2018 18:27, bartc wrote:

(BTW here's a port of that benchmark based on the Lua code:

   https://pastebin.com/raw/ivDaKudX

And here's the payoff: I was able to use this version to port it to Python. One which works better the the originals, as they wrote output to the screen (/binary/ output) which I found difficult to capture into an actual ppm file in order to test it worked.

The translation was straightforward, EXCEPT that I wasted an hour trying to figure out to write /a single byte/ to a file. The following eventually worked, using a binary file as a text one had Unicode problems, but it's still hacky.

Note this version doesn't use any imports at all.

----------------------------------------------------------------

# For Python 3 (it'll work on Python 2 but give the wrong results)

n = 200                # adjust this for output size: n * n pixels
outfile = "test.ppm"   # adjust for output file name
end = 0                # lines containing 'end' can be removed

def start():
    m = 2/n
    ba = 1<<(n%8+1)
    bb = 1<<(8-n%8)

    f = open(outfile,"wb")

    f.write(b"P4\n")
    f.write((str(n)+" "+str(n)+"\n").encode())

    for y in range(n):
        ci = y*m-1
        b = 1

        for x in range(n):
            cr = x*m-1.5
            zr = cr
            zi = ci
            zrq = cr*cr
            ziq = ci*ci
            b <<= 1
            for i in range(1,50):
                zi = zr*zi*2+ci
                zr = zrq-ziq+cr
                ziq = zi*zi
                zrq = zr*zr
                if zrq+ziq>4:
                    b +=1
                    break
                end
            end
            if b>=256:
                f.write(bytes([511-b]))
                b = 1
            end
        end
        if b != 1:
            f.write(bytes([(ba-b)*bb]))
        end
    end
    f.close()
end

start()

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to