0
0
SciPydata~5 mins

Performance tips and vectorization in SciPy

Choose your learning style9 modes available
Introduction

Vectorization helps your code run faster by doing many calculations at once instead of one by one.

When you want to speed up math operations on large lists or arrays.
When you need to avoid slow loops in your data calculations.
When working with big datasets and want to use efficient built-in functions.
When you want cleaner and shorter code that is easier to read.
When you want to use SciPy or NumPy functions that work on whole arrays.
Syntax
SciPy
import numpy as np

# Vectorized operation example
result = np.array1 + np.array2
Vectorized operations work on whole arrays without explicit loops.
Use NumPy or SciPy functions that support vector inputs for best speed.
Examples
This adds each element of arr1 to the corresponding element of arr2 all at once.
SciPy
import numpy as np

# Adding two arrays element-wise
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
sum_arr = arr1 + arr2
print(sum_arr)
Calculates sine for all angles in one step without loops.
SciPy
import numpy as np

# Using vectorized sine function
angles = np.array([0, np.pi/2, np.pi])
sines = np.sin(angles)
print(sines)
Shows how vectorized power operation is simpler and faster than a loop.
SciPy
import numpy as np

# Slow loop version
arr = np.array([1, 2, 3, 4])
squares = []
for x in arr:
    squares.append(x**2)
print(squares)

# Fast vectorized version
squares_vec = arr**2
print(squares_vec)
Sample Program

This program compares adding two large arrays element-wise using a slow Python loop versus a fast vectorized operation with NumPy. It prints the time taken by each method and confirms the results match.

SciPy
import numpy as np
import time

# Create large arrays
size = 1000000
arr1 = np.random.rand(size)
arr2 = np.random.rand(size)

# Slow loop addition
def slow_add(a, b):
    result = []
    for i in range(len(a)):
        result.append(a[i] + b[i])
    return result

start = time.time()
slow_result = slow_add(arr1, arr2)
end = time.time()
print(f"Slow loop time: {end - start:.4f} seconds")

# Fast vectorized addition
start = time.time()
fast_result = arr1 + arr2
end = time.time()
print(f"Vectorized time: {end - start:.4f} seconds")

# Check results are close
print(f"Results close: {np.allclose(slow_result, fast_result)}")
OutputSuccess
Important Notes

Vectorized code uses optimized C code inside NumPy and SciPy for speed.

Avoid Python loops on large arrays when possible for better performance.

Use functions like np.add, np.multiply, np.sin, np.exp for vectorized math.

Summary

Vectorization makes math on arrays faster and simpler.

Use NumPy and SciPy functions that work on whole arrays.

Avoid explicit loops for big data calculations.