|
| 1 | +# Benchmarks |
| 2 | + |
| 3 | +## Benchmark 1: fill a dict |
| 4 | + |
| 5 | +| **Version** | **Factor** | **Time (microseconds)** | **Allocations** | |
| 6 | +| Python | 1.0x | 280 | ? | |
| 7 | +| PythonCall | 2.4x | 680 | 5008 | |
| 8 | +| PythonCall + `pydel!` | 1.1x | 300 | 1008 | |
| 9 | +| PythonCall `@py` | 1.4x | 420 | 1002 | |
| 10 | +| PythonCall `@py` + `@pydel!` | 1.1x | 300 | 2 | |
| 11 | +| PyCall | 5.4x | 1620 | 10987 | |
| 12 | +| PyCall (readable but wrong) | 5.9x | 1784 | 11456 | |
| 13 | + |
| 14 | +Python code: |
| 15 | +```python-repl |
| 16 | +>>> from timeit import timeit |
| 17 | +>>> def test(): |
| 18 | +... from random import random |
| 19 | +... x = {} |
| 20 | +... for i in range(1000): |
| 21 | +... x[str(i)] = i + random() |
| 22 | +... return x |
| 23 | +... |
| 24 | +>>> timeit("test()", N=1000, globals=globals()) |
| 25 | +``` |
| 26 | + |
| 27 | +PythonCall code: |
| 28 | +```julia-repl |
| 29 | +julia> using PythonCall, BenchmarkTools |
| 30 | +
|
| 31 | +julia> function test() |
| 32 | + random = pyimport("random").random |
| 33 | + x = pydict() |
| 34 | + for i in pyrange(1000) |
| 35 | + x[pystr(i)] = i + random() |
| 36 | + end |
| 37 | + return x |
| 38 | + end |
| 39 | +test (generic function with 1 method) |
| 40 | +
|
| 41 | +julia> @benchmark test() |
| 42 | +``` |
| 43 | + |
| 44 | +PythonCall + `pydel!` code: |
| 45 | +```julia-repl |
| 46 | +julia> using PythonCall, BenchmarkTools |
| 47 | +
|
| 48 | +julia> function test() |
| 49 | + random = pyimport("random").random |
| 50 | + x = pydict() |
| 51 | + for i in pyrange(1000) |
| 52 | + k = pystr(i) |
| 53 | + r = random() |
| 54 | + v = i + r |
| 55 | + x[k] = v |
| 56 | + pydel!(k) |
| 57 | + pydel!(r) |
| 58 | + pydel!(v) |
| 59 | + pydel!(i) |
| 60 | + end |
| 61 | + return x |
| 62 | + end |
| 63 | +test (generic function with 1 method) |
| 64 | +
|
| 65 | +julia> @benchmark test() |
| 66 | +``` |
| 67 | + |
| 68 | +PythonCall `@py` code: |
| 69 | +```julia-repl |
| 70 | +julia> using PythonCall, BenchmarkTools |
| 71 | +
|
| 72 | +julia> test() = @py begin |
| 73 | + import random: random |
| 74 | + x = {} |
| 75 | + for i in range(1000) |
| 76 | + x[str(i)] = i + random() |
| 77 | + # Uncomment for pydel! version: |
| 78 | + # @jl PythonCall.pydel!(i) |
| 79 | + end |
| 80 | + x |
| 81 | + end |
| 82 | +test (generic function with 1 method) |
| 83 | +
|
| 84 | +julia> @benchmark test() |
| 85 | +``` |
| 86 | + |
| 87 | +PyCall code: |
| 88 | +```julia-repl |
| 89 | +julia> using PyCall, BenchmarkTools |
| 90 | +
|
| 91 | +julia> function test() |
| 92 | + random = pyimport("random")."random" |
| 93 | + x = pycall(pybuiltin("dict"), PyObject) |
| 94 | + str = pybuiltin("str") |
| 95 | + for i in pycall(pybuiltin("range"), PyObject, 1000) |
| 96 | + set!(x, pycall(str, PyObject, i), i + pycall(random, PyObject)) |
| 97 | + end |
| 98 | + return x |
| 99 | + end |
| 100 | +test (generic function with 1 method) |
| 101 | +
|
| 102 | +julia> @benchmark test() |
| 103 | +``` |
| 104 | + |
| 105 | +PyCall (readable but wrong) code: |
| 106 | +```julia-repl |
| 107 | +julia> using PyCall, BenchmarkTools |
| 108 | +
|
| 109 | +julia> function test() |
| 110 | + random = pyimport("random").random |
| 111 | + x = pybuiltin("dict")() |
| 112 | + str = pybuiltin("str") |
| 113 | + for i in pybuiltin("range")(1000) |
| 114 | + x[str(i)] = i + random() |
| 115 | + end |
| 116 | + return x |
| 117 | + end |
| 118 | +test (generic function with 1 method) |
| 119 | +
|
| 120 | +julia> @benchmark test() |
| 121 | +``` |
0 commit comments