""" circuitpython on pico
Time taken to compute sin 100 times: 0.00600052 seconds
Time taken to compute int sin 100 times: 0.00300026 seconds
Time taken to partially compute int sin 100 times: 0.00200081 seconds
Time taken to sum two ints 100 times: 0.0 seconds
Time taken to iterate call to native sin 40000 times: 0.705 seconds
Time taken to iterate call to sin 3 40000 times: 0.983 seconds
Time taken to assign and iterate range 40000: 0.173 seconds
Time taken to lookup and iterate range 40000: 0.355 seconds
Time taken to iterate range 40000: 0.155001 seconds
Time taken to call to native sin 40000 times: 0.827999 seconds
Time taken to lookup 40000 times: 0.199999 seconds
Time taken to iterate while 40000: 0.164001 seconds
"""
import time
import math
from sine2 import sine3
a_coeffs = (-0.039, -0.117, -0.191, -0.261, -0.325, -0.380, -0.426, -0.462, -0.486, -0.498)
b_coeffs = (1.002, 1.027, 1.074, 1.139, 1.219, 1.306, 1.392, 1.470, 1.531, 1.565)
c_coeffs = (-0.000031, -0.002, -0.0094, -0.025, -0.050, -0.084, -0.125, -0.168, -0.205, -0.230)
a_values = (1, 2, 3, 4, 5, 6, 7, 8, 9)
def test():
# Initialize timer
start_time = time.monotonic()
for x in range(100):
x = x % (6.28) # Approximation of 2 * PI
should_be_negative = False
if x > 3.14:
x = 6.28 - x
should_be_negative = True
if x > 1.57:
x = 3.14 - x
interval_index = int(x / 0.157) # Simplified for PI/2 approximation
interval_index = min(interval_index, 9)
# Access each coefficient directly from its respective tuple
a = a_coeffs[interval_index]
b = b_coeffs[interval_index]
c = c_coeffs[interval_index]
#result = a * x**2 + b * x + c
result = x#(x + b) * x * a + c
elapsed_time = time.monotonic() - start_time
print("Time taken to compute sin 100 times:", elapsed_time, "seconds")
start_time = time.monotonic()
for x in range(100):
x = x % 6
should_be_negative = False
if x > 3:
x = 6 - x
should_be_negative = True
if x > 1:
x = 3 - x
# Access each coefficient directly from its respective tuple
a = a_coeffs[x]
b = b_coeffs[x]
c = c_coeffs[x]
#result = a * x**2 + b * x + c
result = a + b + c
elapsed_time = time.monotonic() - start_time
print("Time taken to compute int sin 100 times:", elapsed_time, "seconds")
a = 2.767
b = 2.8863
c = 2.65
start_time = time.monotonic()
for x in range(100):
x = x % 6
should_be_negative = False
if x > 3:
x = 6 - x
should_be_negative = True
if x > 1:
x = 3 - x
# Access each coefficient directly from its respective tuple
#result = a * x**2 + b * x + c
result = a + b + c
elapsed_time = time.monotonic() - start_time
print("Time taken to partially compute int sin 100 times:", elapsed_time, "seconds")
start_time = time.monotonic()
for x in range(100):
result = x + x
elapsed_time = time.monotonic() - start_time
print("Time taken to sum two ints 100 times:", elapsed_time, "seconds")
start_time = time.monotonic()
for i in range(40000):
x = 1
y = math.sin(i)
elapsed_time = time.monotonic() - start_time
print("Time taken to iterate call to native sin 40000 times:", elapsed_time, "seconds")
start_time = time.monotonic()
for i in range(40000):
x = 1
y = sine3(i)
elapsed_time = time.monotonic() - start_time
print("Time taken to iterate call to sin 3 40000 times:", elapsed_time, "seconds")
sin_elapsed_time = elapsed_time
start_time = time.monotonic()
for i in range(40000):
x = 1
y = 43.43242
elapsed_time = time.monotonic() - start_time
print("Time taken to assign and iterate range 40000:", elapsed_time, "seconds")
start_time = time.monotonic()
for i in range(40000):
x = 1
y = b_coeffs[x % 10]
elapsed_time = time.monotonic() - start_time
print("Time taken to lookup and iterate range 40000:", elapsed_time, "seconds")
lookup_elapsed_time = elapsed_time
start_time = time.monotonic()
for i in range(40000):
x = 1
elapsed_time = time.monotonic() - start_time
print("Time taken to iterate range 40000:", elapsed_time, "seconds")
assignment_elapsed_time = elapsed_time
elapsed_time = sin_elapsed_time - assignment_elapsed_time
print("Time taken to call to native sin 40000 times:", elapsed_time, "seconds")
elapsed_time = lookup_elapsed_time - assignment_elapsed_time
print("Time taken to lookup 40000 times:", elapsed_time, "seconds")
start_time = time.monotonic()
i = 0
while i < 40000:
x = 1
i = i + 1
elapsed_time = time.monotonic() - start_time
print("Time taken to iterate while 40000:", elapsed_time, "seconds")
test()