Skip to content

Instantly share code, notes, and snippets.

@nmsderp
Last active August 16, 2024 03:00
Show Gist options
  • Save nmsderp/34bd4242dd2369d0a6c22c6312ed1165 to your computer and use it in GitHub Desktop.
Save nmsderp/34bd4242dd2369d0a6c22c6312ed1165 to your computer and use it in GitHub Desktop.
import machine
import time
import math
# 270 mhz your Pico MAY go to 300mhz!
# Not tested on Pico 2!
# If I get a Pico 2 I will make one that uses 1 Arm core and 1 RISC-V core
machine.freq(270000000)
def calculate_pi(n):
pi = 0
for k in range(n):
pi += (1 / (16 ** k)) * (
(4 / (8 * k + 1)) -
(2 / (8 * k + 4)) -
(1 / (8 * k + 5)) -
(1 / (8 * k + 6))
)
return pi
def complex_math_calculations():
factorial_result = math.factorial(1025)
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
fibonacci_result = fibonacci(1025)
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
return False
return True
prime_result = [i for i in range(10000) if is_prime(i)]
matrix_size = 65
matrix_a = [[i * j for j in range(matrix_size)] for i in range(matrix_size)]
matrix_b = [[i + j for j in range(matrix_size)] for i in range(matrix_size)]
matrix_c = [[sum(a * b for a, b in zip(matrix_a_row, matrix_b_col)) for matrix_b_col in zip(*matrix_b)] for matrix_a_row in matrix_a]
return factorial_result, fibonacci_result, prime_result, matrix_c
def read_temperature():
sensor = machine.ADC(4)
conversion_factor = 3.3 / (65535)
reading = sensor.read_u16() * conversion_factor
temperature = 27 - (reading - 0.706) / 0.001721
return temperature
def stress_test_iteration():
print("Calculating digits of Pi...")
pi_digits = calculate_pi(800)
print(f"Pi: {pi_digits}")
print("Performing complex mathematical calculations...")
factorial_result, fibonacci_result, prime_result, matrix_c = complex_math_calculations()
print(f"Factorial Result: {str(factorial_result)[:50]}...")
print(f"Fibonacci Result: {fibonacci_result}")
print(f"Prime Numbers: {prime_result[:50]}")
print(f"Matrix C[0][0]: {matrix_c[0][0]}")
print("Reading temperature...")
temperature = read_temperature()
print(f"Temperature: {temperature:.2f} C")
print(machine.freq())
def stress_test(duration_minutes=30):
start_time = time.ticks_ms()
end_time = start_time + duration_minutes * 60 * 1000
iteration_count = 0
while time.ticks_ms() < end_time:
iteration_start_time = time.ticks_ms()
stress_test_iteration()
iteration_end_time = time.ticks_ms()
iteration_duration = time.ticks_diff(iteration_end_time, iteration_start_time)
iteration_count += 1
elapsed_time = time.ticks_diff(iteration_end_time, start_time) // 1000
print(f"Iteration {iteration_count} completed in {iteration_duration} ms")
print(f"Elapsed time: {elapsed_time} seconds")
time.sleep(0.05)
print("Stress test completed")
stress_test(30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment