Skip to content

Commit fb25948

Browse files
committed
Add pypy example
1 parent d7cdcdb commit fb25948

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

source-code/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ to create it. There is some material not covered in the presentation as well.
3636
1. `pyspark`: illustrations of using PySpark.
3737
1. `hdf5`: examples of parallel I/O using HDF5.
3838
1. `numpy-scipy`: some numpy/scipy codes for benchmakring.
39+
1. `pypy`: code to experiment with the Pypy interpreter.

source-code/pypy/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Pypy
2+
3+
Pypy is an alternative interpreter that can in some circumtatnces be
4+
up to four times faster than CPython.
5+
6+
7+
## What is it?
8+
9+
1. `julia_set.py`: compute the Julia set.
10+
11+
12+
## How to use?
13+
14+
To run with Pypy, use:
15+
```bash
16+
$ pypy julia_set.py 1000 > julia.txt
17+
```

source-code/pypy/environment.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name: pypy
2+
channels:
3+
- defaults
4+
dependencies:
5+
- pypy
6+
- numpy
7+
prefix: /home/gjb/miniconda3/envs/pypy

source-code/pypy/julia_set.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env python
2+
3+
import argparse
4+
import numpy as np
5+
import sys
6+
import time
7+
8+
9+
def julia_set_iteration(z, c):
10+
i = 0
11+
while i < 255 and abs(z) < 2.0:
12+
z = z**2 + c
13+
i += 1
14+
return i
15+
16+
def julia_set(n, c):
17+
result = np.empty((n, n), dtype=np.uint8)
18+
delta = 2.0*1.8/n
19+
for i in range(n):
20+
z_re = -1.8 + i*delta
21+
for j in range(n):
22+
z_im = -1.8 + j*delta
23+
result[i, j] = julia_set_iteration(complex(z_re, z_im), c)
24+
return result
25+
26+
27+
if __name__ == '__main__':
28+
arg_parser = argparse.ArgumentParser(description='compute JUlia set')
29+
arg_parser.add_argument('n', type=int, help='number of points')
30+
options = arg_parser.parse_args()
31+
c = complex(0.4, 0.6)
32+
start = time.time()
33+
julia = julia_set(options.n, c)
34+
end = time.time()
35+
print(f'compute time: {end - start} s', file=sys.stderr)
36+
np.savetxt(sys.stdout, julia)

0 commit comments

Comments
 (0)