hw2 interpreters

This commit is contained in:
ilia 2023-10-06 13:14:14 +03:00
parent 1fbb754bde
commit f3cf3b975e
20 changed files with 396 additions and 0 deletions

162
interpreters/.gitignore vendored Normal file
View File

@ -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/

24
interpreters/README.md Normal file
View File

@ -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)

7
interpreters/linspace.py Normal file
View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,4 @@
interpreter: CPython-3.11
time in seconds:
with types no types with numpy
1.001602 1.047993 1.322597

View File

@ -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

View File

@ -0,0 +1,4 @@
interpreter: CPython-3.9
time in seconds:
with types no types with numpy
1.377455 1.345244 1.437867

View File

@ -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

View File

@ -0,0 +1,11 @@
Traceback (most recent call last):
File "test_full_with_types.py", line 1, in <module>
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

View File

@ -0,0 +1,11 @@
Traceback (most recent call last):
File "test_full_with_types.py", line 1, in <module>
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

View File

@ -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

View File

@ -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

BIN
interpreters/table.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.4 KiB

43
interpreters/test.py Normal file
View File

@ -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)

12
interpreters/test_full.sh Normal file
View File

@ -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;

View File

@ -0,0 +1,3 @@
from pure_no_types import mandelbrot_no_types
mandelbrot_no_types()

View File

@ -0,0 +1,3 @@
from np_extended import mandelbrot_np
mandelbrot_np()

View File

@ -0,0 +1,3 @@
from pure_with_types import mandelbrot_with_types
mandelbrot_with_types()