Math module vs manual calculation, question.

A question rises…

I know two ways to rotate an object(see below). Which one is better/faster to use?
Use math module to convert to radians and the to_matrix or manual calculation without the module and set the orientation directly?

With the math module:


def math_rotate(cont):
    
    import math
    # <-----
    own                     = cont.owner
    rot_step_degree         = 5.0
    rotation_radians        = own.worldOrientation.to_euler() 
    rotation_radians[2]     += math.radians(rot_step_degree)
    matrix                  = rotation_radians.to_matrix()
    
    own.worldOrientation    = matrix 

Manual calculation:


def rotate(cont):
    
    # ----->
    own                     = cont.owner
    rot_step_degree         = -5.0 
    rotation_radians        = own.worldOrientation.to_euler() 
    degrees                 = radians_to_degrees(rotation_radians[2])     
    radians                 = degrees_to_radians(degrees + rot_step_degree)
    
    own.worldOrientation    = [0,0,radians] 
 
        
def radians_to_degrees(radians): 
    
    pi      = 3.141592653589793   
    degrees = radians / (pi / 180)  
    
    return degrees


def degrees_to_radians(degrees):
    
    pi = 3.141592653589793
    radians = degrees * (pi / 180)  
    
    return radians


Both do the exact same thing, and both returning the same calculations(well manual is a bit more precise).
Who knows the answer to my question?

oh no. you’re one of those types who lines up all their ‘=’

This is pretty easy to answer:

First define “better”.
Then think about the different topics. What do you think, which one should be faster/better (What is your expectation)?
Now do a benchmark test to see what it is (e.g. calculate 10000 times with one method compared to the other method).

Then compare what you expected with what you get.

https://docs.python.org/3/library/math.html

Python’s math module is running c functions under the hood, so despite the trivial processing, using the standard lib will be faster 99% of the time.

I assume the same, this is 99% of the time true calling a predefined function is faster…but I am curious if you test the ‘time’ it takes…please share your results.


>>> import math
>>> import timeit
>>> def radians(n):
...     pi = 3.141592653589793
...     radians = n * (pi / 180)
...     return radians
...
>>> timeit.timeit('radians(180)', globals=globals())
0.20196638235393555
>>> timeit.timeit('math.radians(180)', globals=globals())
0.10451571850141761

I don’t know what you expected: math.radians is almost 2x faster than a Python equivalent. C for the win !

Yup, and don’t complain if you don’t know why i’m doing it. :wink:

First define “better”.

Python’s math module is running c functions under the hood, so despite the trivial processing, using the standard lib will be faster 99% of the time

Well, better would not be a thing due to they both do the same, i just wondered if it would be faster to manual calculate or by importing the math module, i always thought that modules also took time to process.

I also read somewhere that if you imported modules (probably bge) script would become gpl. So that’s why i also was thinking about manual vs module.

I don’t know what you expected: math.radians is almost 2x faster than a Python equivalent. C for the win !

Hmm, wanted to do some testing today, so i’m glad that you already did it. That is actually quite a lot, did not expect it to be 2 times faster.

Guess i/we need to stick to the modules then.

Thank you.