Skip to content

rot_mat_2d: scipy vs numpy #761

@Daoming-Chen

Description

@Daoming-Chen

I write a short script to test the speed of two versions of rot_mat_2d() function. The one used in PythonRobotics is rot_mat_2d(). It turns out numpy version is faster than scipy. So, I wander why using scipy to write this function? It's not as explicit, and fast as numpy.

from scipy.spatial.transform import Rotation as Rot
import numpy as np

def rot_mat_2d(angle):
    return Rot.from_euler('z', angle).as_matrix()[0:2, 0:2]

def rot_mat_2d2(angle):
    return np.array([[np.cos(pi), -np.sin(pi)],
                     [np.sin(pi), np.cos(pi)]])

import time
t = 100000
cur_time = time.time()
for _ in range(t):
    rot_mat_2d(pi)
print(cur_time - time.time())

cur_time = time.time()
for _ in range(t):
    rot_mat_2d2(pi)
print(cur_time - time.time())

The output is:

-0.6680886745452881
-0.3319261074066162

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions