ThalesGroup / kessler-game

Kessler is a simulation environment loosely modeled after our internal project PsiBee and the external project Fuzzy Asteroids. The game has ships that shoot bullets at asteroids to gain score. Ships can collide with asteroids and other ships and lose lives.
https://github.com/ThalesGroup/kessler-game
Apache License 2.0
8 stars 5 forks source link

Numpy functions used instead of math functions #36

Closed Jie-F closed 6 months ago

Jie-F commented 7 months ago

Numpy functions (np.radians, np.sin, np.cos, np.sqrt, etc.) are optimized for numpy arrays. They also work on scalar numbers, however the equivalent math.radians, math.sin, math.cos, math.sqrt etc. are faster than the numpy functions.

Here's a simple benchmarking script I ran to test np.sqrt against math.sqrt, and the output shows that np.sqrt is 6.47 times slower than math.sqrt for scalar values.

import numpy as np
import math
import time

# Generate a list of a million numbers
numbers = np.arange(1, 1000001)

# Benchmark math.sqrt
start_time_math = time.time()
for number in numbers:
    _ = math.sqrt(number)
time_math = time.time() - start_time_math

# Benchmark numpy.sqrt
start_time_numpy = time.time()
for number in numbers:
    _ = np.sqrt(number)
time_numpy = time.time() - start_time_numpy

print(time_math, time_numpy)
print(time_numpy/time_math)