The Computer Language
22.05 Benchmarks Game

simple Python 3 #2 program

source code

# The Computer Language Benchmarks Game
# https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
# Written for Python 2
# contributed by Brent Fulgham

import sys

def main():
    bit_num = 0
    byte_acc = 0
    iter = 50
    limit2 = 4.
    w = h = int(sys.argv[1])

    sys.stdout.write(f'P4\n{w} {h}\n'); sys.stdout.flush()

    for y in range(h):
        fy = 2j * y / h - 1j
        for x in range(w):
            z = 0j
            c = 2. * x / w - 1.5 + fy

            byte_acc <<= 1
            bit_num += 1

            for i in range(iter):
                z = z * z + c
                if z.real * z.real + z.imag * z.imag > limit2:
                    break
            else:
                byte_acc += 1

            if bit_num == 8:
                # Python 2.7 sys.stdout.write(chr(byte_acc))
                sys.stdout.buffer.write(bytes([byte_acc])) 
                byte_acc = bit_num = 0
            elif x == w - 1:
                byte_acc = byte_acc << (8 - w % 8)
                # Python 2.7 sys.stdout.write(chr(byte_acc))
                sys.stdout.buffer.write(bytes([byte_acc])) 
                byte_acc = bit_num = 0

main()
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
Python 3.10.4


Mon, 09 May 2022 18:50:37 GMT

MAKE:
mv simple.python3-2.python3 simple.python3-2.py
pytype .
Traceback (most recent call last):
  File "/home/dunham/.local/bin/pytype", line 5, in <module>
    from pytype.tools.analyze_project.main import main
ModuleNotFoundError: No module named 'pytype'
make: [/home/dunham/all-benchmarksgame/2000-benchmarksgame/nanobench/makefiles/u64q.programs.Makefile:387: simple.python3-2.python3_run] Error 1 (ignored)

0.16s to complete and log all make actions

COMMAND LINE:
/opt/src/Python-3.10.4/bin/python3 -OO simple.python3-2.py 16000

(BINARY) PROGRAM OUTPUT NOT SHOWN