diff --git a/interpreters/.gitignore b/interpreters/.gitignore new file mode 100644 index 0000000..fae33bc --- /dev/null +++ b/interpreters/.gitignore @@ -0,0 +1,162 @@ +builds +# Byte-compiled / optimized / DLL files +__pycache__/ +.jython_cache +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ diff --git a/interpreters/README.md b/interpreters/README.md new file mode 100644 index 0000000..6500908 --- /dev/null +++ b/interpreters/README.md @@ -0,0 +1,24 @@ +# Interpreters + +## Testing Code +Three functions tested +```python:pure_with_types.py + +``` + +```python:pure_no_types.py + +``` + +```python:np_extended.py + +``` + +## Tested interpreters +- CPython 3.9 +- CPython 3.11 +- PyPy 3.9 +- Jython + +**Table of evaluation times in seconds** +![enter image description here](table.png) diff --git a/interpreters/linspace.py b/interpreters/linspace.py new file mode 100644 index 0000000..74fd047 --- /dev/null +++ b/interpreters/linspace.py @@ -0,0 +1,7 @@ +def linspace(start, stop, n): + if n == 1: + yield stop + return + h = (stop - start) / (n - 1) + for i in range(n): + yield start + h * i diff --git a/interpreters/np_extended.py b/interpreters/np_extended.py new file mode 100644 index 0000000..2e96538 --- /dev/null +++ b/interpreters/np_extended.py @@ -0,0 +1,24 @@ +import numpy as np + +def mandelbrot_np( + pmin=-2.5, + pmax=1.5, + qmin=-2, + qmax=2, + ppoints=200, + qpoints=200, + max_iterations=300, + infinity_border=100): + image = np.zeros((ppoints, qpoints)) + + for ip, p in enumerate(np.linspace(pmin, pmax, ppoints)): + for iq, q in enumerate(np.linspace(qmin, qmax, qpoints)): + c = p + 1j * q + z = 0 + for k in range(max_iterations): + z = z ** 2 + c + if abs(z) > infinity_border: + + image[ip, iq] = 1 + break + return image diff --git a/interpreters/pure_no_types.py b/interpreters/pure_no_types.py new file mode 100644 index 0000000..d919337 --- /dev/null +++ b/interpreters/pure_no_types.py @@ -0,0 +1,22 @@ +from linspace import linspace + +def mandelbrot_no_types( + pmin=-2.5, + pmax=1.5, + qmin=-2, + qmax=2, + ppoints=200, + qpoints=200, + max_iterations=300, + infinity_border=100): + image = [[0 for i in range(qpoints)] for j in range(ppoints)] + for ip, p in enumerate(linspace(pmin, pmax, ppoints)): + for iq, q in enumerate(linspace(qmin, qmax, qpoints)): + c = p + 1j * q + z = 0 + for k in range(max_iterations): + z = z ** 2 + c + if abs(z) > infinity_border: + image[ip][iq] = 1 + break + return image diff --git a/interpreters/pure_with_types.py b/interpreters/pure_with_types.py new file mode 100644 index 0000000..9e667df --- /dev/null +++ b/interpreters/pure_with_types.py @@ -0,0 +1,23 @@ +from linspace import linspace + +def mandelbrot_with_types( + pmin: float = -2.5, + pmax: float = 1.5, + qmin: float = -2, + qmax: float = 2, + ppoints: int = 200, + qpoints: int = 200, + max_iterations: int = 300, + infinity_border: float = 100) -> list[list[int]]: + + image: list[list[int]] = [[0 for i in range(qpoints)] for j in range(ppoints)] + for ip, p in enumerate(linspace(pmin, pmax, ppoints)): + for iq, q in enumerate(linspace(qmin, qmax, qpoints)): + c: complex = p + 1j * q + z: complex = 0 + for k in range(max_iterations): + z = z ** 2 + c + if abs(z) > infinity_border: + image[ip][iq] = 1 + break + return image diff --git a/interpreters/results/CPython-3.11.txt b/interpreters/results/CPython-3.11.txt new file mode 100644 index 0000000..0480efe --- /dev/null +++ b/interpreters/results/CPython-3.11.txt @@ -0,0 +1,4 @@ +interpreter: CPython-3.11 +time in seconds: +with types no types with numpy +1.001602 1.047993 1.322597 \ No newline at end of file diff --git a/interpreters/results/CPython-3.11_full.txt b/interpreters/results/CPython-3.11_full.txt new file mode 100644 index 0000000..03cef69 --- /dev/null +++ b/interpreters/results/CPython-3.11_full.txt @@ -0,0 +1,12 @@ + +real 0m1.669s +user 0m0.012s +sys 0m0.055s + +real 0m1.197s +user 0m0.004s +sys 0m0.009s + +real 0m2.274s +user 0m0.014s +sys 0m0.000s diff --git a/interpreters/results/CPython-3.9.txt b/interpreters/results/CPython-3.9.txt new file mode 100644 index 0000000..c464600 --- /dev/null +++ b/interpreters/results/CPython-3.9.txt @@ -0,0 +1,4 @@ +interpreter: CPython-3.9 +time in seconds: +with types no types with numpy +1.377455 1.345244 1.437867 \ No newline at end of file diff --git a/interpreters/results/CPython-3.9_full.txt b/interpreters/results/CPython-3.9_full.txt new file mode 100644 index 0000000..978522a --- /dev/null +++ b/interpreters/results/CPython-3.9_full.txt @@ -0,0 +1,12 @@ + +real 0m2.180s +user 0m0.008s +sys 0m0.061s + +real 0m1.696s +user 0m0.013s +sys 0m0.000s + +real 0m2.447s +user 0m0.008s +sys 0m0.004s diff --git a/interpreters/results/Jython_error.txt b/interpreters/results/Jython_error.txt new file mode 100644 index 0000000..b9367ac --- /dev/null +++ b/interpreters/results/Jython_error.txt @@ -0,0 +1,11 @@ +Traceback (most recent call last): + File "test_full_with_types.py", line 1, in + from pure_with_types import mandelbrot_with_types + File "/mnt/c/Users/rurur/Desktop/p/python/advanced_python/advanced-python-homework-2023/interpreters/pure_with_types.py", line 4 + pmin: float = -2.5, + ^ +SyntaxError: mismatched input ':' expecting RPAREN + +real 0m5.653s +user 0m15.313s +sys 0m0.617s diff --git a/interpreters/results/Jython_full.txt b/interpreters/results/Jython_full.txt new file mode 100644 index 0000000..dff3fdf --- /dev/null +++ b/interpreters/results/Jython_full.txt @@ -0,0 +1,11 @@ +Traceback (most recent call last): + File "test_full_with_types.py", line 1, in + from pure_with_types import mandelbrot_with_types + File "/mnt/c/Users/rurur/Desktop/p/python/advanced_python/advanced-python-homework-2023/interpreters/pure_with_types.py", line 4 + pmin: float = -2.5, + ^ +SyntaxError: mismatched input ':' expecting RPAREN + +real 0m4.948s +user 0m14.826s +sys 0m0.715s diff --git a/interpreters/results/PyPy-3.9-v7.3.13.txt b/interpreters/results/PyPy-3.9-v7.3.13.txt new file mode 100644 index 0000000..b7dde11 --- /dev/null +++ b/interpreters/results/PyPy-3.9-v7.3.13.txt @@ -0,0 +1,4 @@ +interpreter: PyPy-3.9-v7.3.13 +time in seconds: +with types no types with numpy +0.099699 0.093644 3.830524 \ No newline at end of file diff --git a/interpreters/results/PyPy-3.9-v7.3.13_full.txt b/interpreters/results/PyPy-3.9-v7.3.13_full.txt new file mode 100644 index 0000000..bb72907 --- /dev/null +++ b/interpreters/results/PyPy-3.9-v7.3.13_full.txt @@ -0,0 +1,12 @@ + +real 0m5.456s +user 0m0.128s +sys 0m0.459s + +real 0m3.527s +user 0m0.107s +sys 0m0.492s + +real 0m15.416s +user 0m4.836s +sys 0m1.698s diff --git a/interpreters/table.png b/interpreters/table.png new file mode 100644 index 0000000..018a999 Binary files /dev/null and b/interpreters/table.png differ diff --git a/interpreters/test.py b/interpreters/test.py new file mode 100644 index 0000000..602579b --- /dev/null +++ b/interpreters/test.py @@ -0,0 +1,43 @@ +import time +import sys +from pure_with_types import mandelbrot_with_types +from pure_no_types import mandelbrot_no_types +from np_extended import mandelbrot_np + + +def test(function): + tic = time.perf_counter_ns() + image = function() + toc = time.perf_counter_ns() + return (toc - tic) / 1e9 + +def try_func(function): + try: + time = test(function) + except Exception as e: + raise e + return time + +def main(arg): + interpreter = arg[1] + + time_with_types = "err" + time_no_types = "err" + time_np = "err" + + time_with_types = try_func(mandelbrot_with_types) + time_no_types = try_func(mandelbrot_no_types) + time_np = try_func(mandelbrot_np) + + response = f"interpreter: {interpreter}\n"\ + "time in seconds:\n"\ + "with types\tno types\twith numpy\n"\ + f"err\t{time_no_types:.6f}\t{time_np:.6f}" + # response = "{:.6f} {:.6f}".format(time_no_types, time_np) + print(response) + with open("results/{}.txt".format(interpreter), "w", encoding="utf8") as f: + f.write(response) + + +if __name__ == "__main__": + main(sys.argv) diff --git a/interpreters/test_full.sh b/interpreters/test_full.sh new file mode 100644 index 0000000..9ec5dda --- /dev/null +++ b/interpreters/test_full.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +interpreterCmd=$1; +interpreterTitle=$2; + +{ + time $interpreterCmd test_full_with_types.py && + time $interpreterCmd test_full_no_types.py && + time $interpreterCmd test_full_np.py; +} 2> results/${interpreterTitle}_full.txt; + +$interpreterCmd test.py $interpreterTitle; diff --git a/interpreters/test_full_no_types.py b/interpreters/test_full_no_types.py new file mode 100644 index 0000000..5ab5d83 --- /dev/null +++ b/interpreters/test_full_no_types.py @@ -0,0 +1,3 @@ +from pure_no_types import mandelbrot_no_types + +mandelbrot_no_types() diff --git a/interpreters/test_full_np.py b/interpreters/test_full_np.py new file mode 100644 index 0000000..16277b6 --- /dev/null +++ b/interpreters/test_full_np.py @@ -0,0 +1,3 @@ +from np_extended import mandelbrot_np + +mandelbrot_np() diff --git a/interpreters/test_full_with_types.py b/interpreters/test_full_with_types.py new file mode 100644 index 0000000..1757580 --- /dev/null +++ b/interpreters/test_full_with_types.py @@ -0,0 +1,3 @@ +from pure_with_types import mandelbrot_with_types + +mandelbrot_with_types()