Skip to content

Instantly share code, notes, and snippets.

@Bamgm14
Created August 7, 2024 05:35
Show Gist options
  • Save Bamgm14/4e605d2977681f079fef2318db656722 to your computer and use it in GitHub Desktop.
Save Bamgm14/4e605d2977681f079fef2318db656722 to your computer and use it in GitHub Desktop.
Helper Function For Lab 2 to compile code and run with multiple parameters
import subprocess
import os
files = ['task1', 'task2', 'task3']
print(os.listdir())
strs = ""
for size in range(1, 7):
size = str(10**size)
strs += "Size: " + size + "\n"
for name in files:
print(["gcc", f'{name}.c', '-lm', '-lpthread', '-fopenmp', '-o', f'{name}', '&&', f'{name}', size])
a = subprocess.Popen(["gcc", f'{name}.c', '-lm', '-lpthread', '-fopenmp', '-o', f'{name}'], cwd = os.getcwd(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
a.wait()
array = []
for _ in range(10):
result = subprocess.Popen([f'./{name}', size], cwd = os.getcwd(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
array.append(float(result.stdout.read().split('.\n')[1].split('s')[0]))
val = sum(array)/len(array)
std = (sum([(x - val)**2 for x in array])/len(array))**0.5
strs += f"{name}, {val:.6f} +/- {std:.6f}s "
strs = strs + "\n"
print(strs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment