forked from Advanced_Python/advanced-python-homework-2023
hw2 on interpreters done
This commit is contained in:
parent
f26a16dc8c
commit
e4f2bee355
@ -1,21 +1,40 @@
|
|||||||
# Interpreters
|
# Interpreters
|
||||||
|
|
||||||
|
## Results
|
||||||
|
![results](result.png)
|
||||||
|
|
||||||
|
### Observations
|
||||||
|
- Functions w/o Numpy in PyPy run **much faster**. JIT compilation optimizes the function's code and runs compiled versions of them $\Rightarrow$ improvement in time
|
||||||
|
- Entire file evaluation for PyPy takes **a bit longer**. This may be a drawback of JIT optimization
|
||||||
|
- [PyPy with Numpy is slow](https://stackoverflow.com/questions/42536308/why-is-pypy-slower-for-adding-numpy-arrays). Reason:
|
||||||
|
- Numpy is written in C
|
||||||
|
- PyPy's JIT compilation is not compatible with C
|
||||||
|
- Extra conversion is required (takes time)
|
||||||
|
- Numpy functions don't give a good improvement in perfomance. Possibly because the code uses classic Python loops instead of numpy vectorisation.
|
||||||
|
|
||||||
## Tested interpreters
|
## Tested interpreters
|
||||||
- CPython 3.9
|
- CPython 3.9
|
||||||
- CPython 3.11
|
- CPython 3.11
|
||||||
- PyPy 3.9
|
- PyPy 3.9 latest
|
||||||
|
- Pyodide (did not test full file because it only runs in a browser)
|
||||||
**Table of evaluation times in seconds**
|
- Xeus (again no full file)
|
||||||
|
## Failed to test
|
||||||
![enter image description here](table.png)
|
- PyPy 3.9 v5.7
|
||||||
|
- couldn't install numpy `Ignoring ensurepip failure: pip 9.0.1 requires SSL/TLS`
|
||||||
|
- error when testing with types:
|
||||||
|
```python
|
||||||
|
invalid syntax --> image: list[list[int]] = [[0 for i in range(qpoints)] for j in range(ppoints)]
|
||||||
|
```
|
||||||
|
- Jython (dependencies issues)
|
||||||
|
|
||||||
## Testing Code
|
## Testing Code
|
||||||
- File for testing function times: `test.py`
|
- Tested functions code is in `./test_funcs` package
|
||||||
|
- File for testing function times: `test_func_only.py`
|
||||||
- Files for entire evaluation testing: `test_full_{func}.py`
|
- Files for entire evaluation testing: `test_full_{func}.py`
|
||||||
- Bash script `test_full.sh`
|
- Bash script: `test_full.sh`
|
||||||
|
- Analysis file: `analyse.ipynb`
|
||||||
|
|
||||||
Three realisations of mandelbrot functions tested:
|
### Realisations of mandelbrot functions tested:
|
||||||
```python
|
```python
|
||||||
def linspace(start, stop, n):
|
def linspace(start, stop, n):
|
||||||
if n == 1:
|
if n == 1:
|
||||||
|
1620
interpreters/analyse.ipynb
Normal file
1620
interpreters/analyse.ipynb
Normal file
File diff suppressed because it is too large
Load Diff
BIN
interpreters/result.png
Normal file
BIN
interpreters/result.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 229 KiB |
@ -1,4 +1,6 @@
|
|||||||
interpreter: CPython-3.11
|
{
|
||||||
time in seconds:
|
"interpreter": "CPython-3.11",
|
||||||
with types no types with numpy
|
"time_with_types": 0.8206779,
|
||||||
1.001602 1.047993 1.322597
|
"time_no_types": 0.8562159,
|
||||||
|
"time_np": 1.2020039
|
||||||
|
}
|
||||||
|
@ -1,12 +1,3 @@
|
|||||||
|
1.630
|
||||||
real 0m1.669s
|
1.166
|
||||||
user 0m0.012s
|
1.675
|
||||||
sys 0m0.055s
|
|
||||||
|
|
||||||
real 0m1.197s
|
|
||||||
user 0m0.004s
|
|
||||||
sys 0m0.009s
|
|
||||||
|
|
||||||
real 0m2.274s
|
|
||||||
user 0m0.014s
|
|
||||||
sys 0m0.000s
|
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
interpreter: CPython-3.9
|
{
|
||||||
time in seconds:
|
"interpreter": "CPython-3.9",
|
||||||
with types no types with numpy
|
"time_with_types": 1.445288,
|
||||||
1.377455 1.345244 1.437867
|
"time_no_types": 1.4769202,
|
||||||
|
"time_np": 1.4072699
|
||||||
|
}
|
||||||
|
@ -1,12 +1,3 @@
|
|||||||
|
2.242
|
||||||
real 0m2.180s
|
1.502
|
||||||
user 0m0.008s
|
1.986
|
||||||
sys 0m0.061s
|
|
||||||
|
|
||||||
real 0m1.696s
|
|
||||||
user 0m0.013s
|
|
||||||
sys 0m0.000s
|
|
||||||
|
|
||||||
real 0m2.447s
|
|
||||||
user 0m0.008s
|
|
||||||
sys 0m0.004s
|
|
||||||
|
@ -1,11 +0,0 @@
|
|||||||
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
|
|
@ -1,11 +0,0 @@
|
|||||||
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
|
|
@ -1,4 +1,6 @@
|
|||||||
interpreter: PyPy-3.9-v7.3.13
|
{
|
||||||
time in seconds:
|
"interpreter": "PyPy-3.9-v7.3.13",
|
||||||
with types no types with numpy
|
"time_with_types": 0.099363702,
|
||||||
0.099699 0.093644 3.830524
|
"time_no_types": 0.109923603,
|
||||||
|
"time_np": 3.676712179
|
||||||
|
}
|
||||||
|
@ -1,12 +1,3 @@
|
|||||||
|
5.319
|
||||||
real 0m5.456s
|
3.638
|
||||||
user 0m0.128s
|
14.639
|
||||||
sys 0m0.459s
|
|
||||||
|
|
||||||
real 0m3.527s
|
|
||||||
user 0m0.107s
|
|
||||||
sys 0m0.492s
|
|
||||||
|
|
||||||
real 0m15.416s
|
|
||||||
user 0m4.836s
|
|
||||||
sys 0m1.698s
|
|
||||||
|
6
interpreters/results/Pyodide.txt
Normal file
6
interpreters/results/Pyodide.txt
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"interpreter": "piodide",
|
||||||
|
"time_with_types": 1.785,
|
||||||
|
"time_no_types": 1.681,
|
||||||
|
"time_np": 2.833
|
||||||
|
}
|
6
interpreters/results/Xeus.txt
Normal file
6
interpreters/results/Xeus.txt
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"interpreter": "Xeus",
|
||||||
|
"time_with_types": 1.1555191,
|
||||||
|
"time_no_types": 1.0386694,
|
||||||
|
"time_np": 1.2727932
|
||||||
|
}
|
@ -1,43 +0,0 @@
|
|||||||
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)
|
|
@ -2,11 +2,11 @@
|
|||||||
|
|
||||||
interpreterCmd=$1;
|
interpreterCmd=$1;
|
||||||
interpreterTitle=$2;
|
interpreterTitle=$2;
|
||||||
|
TIMEFORMAT=%R
|
||||||
{
|
{
|
||||||
time $interpreterCmd test_full_with_types.py &&
|
time $interpreterCmd test_full_with_types.py &&
|
||||||
time $interpreterCmd test_full_no_types.py &&
|
time $interpreterCmd test_full_no_types.py &&
|
||||||
time $interpreterCmd test_full_np.py;
|
time $interpreterCmd test_full_np.py;
|
||||||
} 2> results/${interpreterTitle}_full.txt;
|
} 2> results/${interpreterTitle}_full.txt;
|
||||||
|
|
||||||
$interpreterCmd test.py $interpreterTitle;
|
$interpreterCmd test_func_only.py $interpreterTitle;
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
from pure_no_types import mandelbrot_no_types
|
from test_funcs.pure_no_types import mandelbrot_no_types
|
||||||
|
|
||||||
mandelbrot_no_types()
|
mandelbrot_no_types()
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
from np_extended import mandelbrot_np
|
from test_funcs.np_extended import mandelbrot_np
|
||||||
|
|
||||||
mandelbrot_np()
|
mandelbrot_np()
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
from pure_with_types import mandelbrot_with_types
|
from test_funcs.pure_with_types import mandelbrot_with_types
|
||||||
|
|
||||||
mandelbrot_with_types()
|
mandelbrot_with_types()
|
||||||
|
49
interpreters/test_func_only.py
Normal file
49
interpreters/test_func_only.py
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
import time
|
||||||
|
import json
|
||||||
|
import sys, os
|
||||||
|
# sys.path.append(os.path.abspath("./test_files"))
|
||||||
|
|
||||||
|
from test_funcs.pure_with_types import mandelbrot_with_types
|
||||||
|
from test_funcs.pure_no_types import mandelbrot_no_types
|
||||||
|
# from test_funcs.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 = dict(
|
||||||
|
interpreter=interpreter,
|
||||||
|
time_with_types=time_with_types,
|
||||||
|
time_no_types=time_no_types,
|
||||||
|
# time_np=time_np
|
||||||
|
)
|
||||||
|
result_string = json.dumps(response, indent=4, ensure_ascii=False)
|
||||||
|
# f = open("results/{}.txt".format(interpreter), "w", encoding="utf8")
|
||||||
|
# print(result_string, file=f)
|
||||||
|
# f.close()
|
||||||
|
|
||||||
|
print(result_string)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main(sys.argv)
|
0
interpreters/test_funcs/__init__.py
Normal file
0
interpreters/test_funcs/__init__.py
Normal file
@ -1,4 +1,4 @@
|
|||||||
from linspace import linspace
|
from .linspace import linspace
|
||||||
|
|
||||||
def mandelbrot_no_types(
|
def mandelbrot_no_types(
|
||||||
pmin=-2.5,
|
pmin=-2.5,
|
@ -1,4 +1,4 @@
|
|||||||
from linspace import linspace
|
from .linspace import linspace
|
||||||
|
|
||||||
def mandelbrot_with_types(
|
def mandelbrot_with_types(
|
||||||
pmin: float = -2.5,
|
pmin: float = -2.5,
|
Loading…
Reference in New Issue
Block a user