# Speeding up my code

**URL:** <https://discourse.julialang.org/t/speeding-up-my-code/76915>\
**Category:** Performance\
**Created:** [February 22, 2022, 3:48pm UTC](https://discourse.julialang.org/t/speeding-up-my-code/76915 "2022-02-22T15:48:43Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Post date:** [February 22, 2022, 5:37pm UTC](https://discourse.julialang.org/t/speeding-up-my-code/76915/3 "2022-02-22T17:37:59Z")

</div>

In python, vectorization speeds things up because it allows the use of numpy. In julia, this isn’t necessary.

Python:

```julia
import numpy as np
from time import time

x = np.random.rand(10**7)
y = np.random.rand(10**7)

def vectorized(x, y):
    return x*x + y*y + x*y

def loop(x, y):
    [x*x + y*y + x*y for (x,y) in zip(x,y)]

t0 = time()
vectorized(x,y) # 0.24799013137817383
t1 = time()
vectorized(x,y) # 0.09563016891479492
t2 = time()

print(t1-t0)
print(t2-t1)

t0 = time()
loop(x,y) # 4.1851279735565186
t1 = time()
loop(x,y) # 4.027431964874268
t2 = time()

print(t1-t0)
print(t2-t1)

```

Juila:

```julia
x = rand(10^7)
y = rand(10^7)

vectorized(x, y) = x.*x .+ y.*y .+ x.*y

loop(x, y) = [x*x + y*y + x*y for (x,y) in zip(x,y)]

@time vectorized(x,y)
# 0.216033 seconds (462.89 k allocations: 99.817 MiB, 2.25% gc time, 50.76% compilation time)
@time vectorized(x,y)
# 0.189564 seconds (2 allocations: 76.294 MiB)

@time loop(x,y)
# 0.139699 seconds (300.01 k allocations: 92.649 MiB, 46.57% compilation time)
@time loop(x,y)
# 0.122678 seconds (2 allocations: 76.294 MiB)

```

---

_[View the full topic](https://discourse.julialang.org/t/speeding-up-my-code/76915)._
