# Comparing Python, Julia, and C++

**URL:** <https://discourse.julialang.org/t/comparing-python-julia-and-c/17019>\
**Category:** Performance\
**Tags:** broadcast, python\
**Created:** [October 31, 2018, 6:03pm UTC](https://discourse.julialang.org/t/comparing-python-julia-and-c/17019 "2018-10-31T18:03:54Z")\
**Posts on this page:** 2\
**Page:** 2

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [November 1, 2018, 12:45pm UTC](https://discourse.julialang.org/t/comparing-python-julia-and-c/17019/21 "2018-11-01T12:45:30Z")

</div>

> [@Maurizio\_Tomasi](#):
>
> Python functions are defined similarly, using NumPy arrays:
> 
> ```julia
> def f(x1, x2, x3, x4, x5, x6):
> return x1 + x2
>     
> # and so on
> 
> ```

Actually your python’s functions look like:

```python
def f(r, x1, x2, x3, x4, x5, x6, x7, x8):
    r = x1 + x   

```

which very probably doesn’t do what is intended. (it just rebind **local** variable `r` inside function)

But it seems you could fix that and it bring no big impact to benchmarks:

```python
%timeit l(result, *x)
11.5 ms ± 54.3 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

def ll(x1, x2, x3, x4, x5, x6, x7, x8):
    return x1 + x2 - x3 + x4 - x5 + x6 - x7 + x8

%timeit r = ll(*x)
11.5 ms ± 112 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

```

For comparison numba version:

```python
@numba.jit
def l2(x1, x2, x3, x4, x5, x6, x7, x8):
    return x1 + x2 - x3 + x4 - x5 + x6 - x7 + x8

%timeit r = l2(*x)
5.52 ms ± 33.3 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

```

I was trying to add parallelism but maybe I have too few (2 😉 ) cores now

```python
os.environ["NUMBA_DEBUG_ARRAY_OPT_STATS"]='1'

@numba.jit('double[:](double[:],double[:],double[:],double[:],double[:],double[:],double[:],double[:],)', nopython=True, parallel=True)
def l2p(x1, x2, x3, x4, x5, x6, x7, x8):
    return x1 + x2 - x3 + x4 - x5 + x6 - x7 + x8
Parallel for-loop #23 is produced from pattern '('arrayexpr (((((((_+_)-_)+_)-_)+_)-_)+_)',)' at <ipython-input-125-c699c2d45b59> (3)
After fusion, function l2p has 1 parallel for-loop(s) #{23}.

%timeit r = l2p(*x)
5.51 ms ± 128 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

```

---

<div class="post-metadata">

**Author:** ![At\_Houd](https://avatars.discourse-cdn.com/v4/letter/a/9e8a1a/32.png) [@At\_Houd](https://discourse.julialang.org/u/At_Houd)\
**Post date:** [November 1, 2018, 1:07pm UTC](https://discourse.julialang.org/t/comparing-python-julia-and-c/17019/22 "2018-11-01T13:07:53Z")

</div>

Just a minor comment: You might want to label the y-axis as Best Time since it has units of ms, whereas Best Speed might suggest that higher is better. It seems that lower is better in this example?

[Previous page](https://discourse.julialang.org/t/comparing-python-julia-and-c/17019.md?page=1)
