diff --git a/MIPS-Benchmark/MIPSBenchmark.py b/MIPS-Benchmark/MIPSBenchmark.py new file mode 100644 index 0000000..f18db79 --- /dev/null +++ b/MIPS-Benchmark/MIPSBenchmark.py @@ -0,0 +1,49 @@ +import numpy as np +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 + _ = np.random.randint(10) + np.random.randint(10) + _ = np.random.randint(10) - np.random.randint(10) + _ = np.random.randint(10) * np.random.randint(10) + _ = np.random.randint(10) // (np.random.randint(10) + 1) # Prevent Division by Zero + _ = np.random.choice([True, False]) & np.random.choice([True, False]) + _ = np.random.choice([True, False]) | np.random.choice([True, False]) + _ = np.random.choice([True, False]) ^ np.random.choice([True, False]) + + def calculate(self): + self.elapsed_time = self.end_time - self.start_time + self.total_instructions = self.iters*7 + self.mips = (self.total_instructions / self.elapsed_time) / 1000000 + print(f"=> Executed {self.total_instructions} within {self.elapsed_time}\n=> Result : {self.mips:.2f} MIPS") + + def run(self): + print("# MIPS CPU Benchmark #") + print("# Running 7 Basic ALU Ops #") + + self.start_time = time() + for i in range(1, self.iters+1): + print(f"=> Running Iteration #{str(i)}", end="\r") + self.ips_operation() + print("\n") + self.end_time = time() + + self.calculate() + + +def main(): + iters = 1000000 # Minimum 1,000,000 times + bench = MIPSBenchmark(iters=iters) + bench.run() + + +main() diff --git a/README.md b/README.md index a91a11e..6b7c4f6 100644 --- a/README.md +++ b/README.md @@ -7,4 +7,5 @@ This repository contains useful programs written in **Python 3.9**. Each folder * **Pickle Compress Program** : compress variable dumps using **gzip**, **bzip2**, **lzma**, or **no compression at all** * **FastAPI Simple Example** : contains API standard operations (Normal Request, GET Request, HTML Request) * **Matrix-based Bencmark** : contains a class to recreate Linpack (Matrix-based) Floating-point Per Second (FLOPS) Benchmark. Based on **numpy** or **scipy** libs +* **MIPS-Benchmark** : contains a script to benchmark your CPU's Intrusion Execution Speed (Million Instructions Per Second). The result might be lower but consistent on numerous runs. * *adding more soon*