Python-Useful-Scripts/MIPS-Benchmark/MIPSBenchmark.py

49 lines
1.2 KiB
Python
Raw Permalink Normal View History

2024-06-17 08:51:09 +02:00
from time import time
class MIPSBenchmark:
def __init__(self, iters: int):
self.iters = iters
self.start_time = 0
self.end_time = 0
self.elapsed_time = 0
self.total_instructions = 0
self.mips = 0
def ips_operation(self):
# Run basic 7 ALU operations
2024-06-17 23:02:17 +02:00
_ = 5 + 5
_ = 5 - 5
_ = 5 * 5
_ = 5 // 5
_ = True & True
_ = True | False
_ = False ^ False
2024-06-17 08:51:09 +02:00
def calculate(self):
self.elapsed_time = self.end_time - self.start_time
self.total_instructions = self.iters*7
2024-06-17 23:02:17 +02:00
self.mips = (self.total_instructions / self.elapsed_time)
print(f"=> Executed {self.total_instructions} within {self.elapsed_time:.2f}")
print(f"=> Result : {self.mips:.2f} IPS / {self.mips/1e6:.2f} MIPS")
2024-06-17 08:51:09 +02:00
def run(self):
print("# MIPS CPU Benchmark #")
print("# Running 7 Basic ALU Ops #")
2024-06-19 00:19:51 +02:00
print("# Please Wait #")
2024-06-17 08:51:09 +02:00
self.start_time = time()
for i in range(1, self.iters+1):
self.ips_operation()
self.end_time = time()
self.calculate()
def main():
iters = 1000000 # Minimum 1,000,000 times
bench = MIPSBenchmark(iters=iters)
bench.run()
main()