-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
numpybench
executable file
·92 lines (76 loc) · 2.97 KB
/
numpybench
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python3
"""
This will give a high level overview of teh performance of various
matrix operations on an array and iterate throught he calculktions
a number of times. There is nothing useful in these calculations,
this is simply to run throug soem calcutions a number of times to
see the relative performance of the GPU or CPU on larce matrices.
"""
import os
import sys
import time
from argparse import ArgumentParser
from datetime import datetime
from io import StringIO
import numpy as np
# Initialize argparse
parser = ArgumentParser(description="Run NumPy benchmarks and output results.")
parser.add_argument("-d", "--datafile", type=str, nargs='?',
default=f"{os.getenv('CONDA_DEFAULT_ENV', 'default')}-timing.txt",
help="Specify the datafile to write the output to.")
parser.add_argument("-s", "--skip-tests", action="store_true", help="Skip time-consuming tests.")
parser.add_argument("-c", "--count", type=int, default=1, help="Number of iterations.")
# Parse the arguments
args = parser.parse_args()
datafile = None
if args.datafile:
datafile = open(args.datafile, 'w', encoding="utf-8")
def print_with_timestamp(message, file=None):
""" Function to print messages with a timestamp """
output = f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} {message}"
print(output)
if file:
file.write(output + '\n')
def do_tests():
""" Perform all the basic tests """
size = 2500
A = np.random.rand(size, size)
B = np.random.rand(size, size)
# Number of iterations
iterations = args.count
tests = [
("Matrix multiplication", lambda: np.dot(A, B)),
("Matrix transposition", lambda: np.transpose(A)),
("Eigenvalue computation", lambda: np.linalg.eigvals(A)),
("Fourier transformation", lambda: np.fft.fft(A)),
("Summation", lambda: np.sum(A))
]
for name, test_func in tests:
print_with_timestamp(f"BEGIN TEST: {name}", file=datafile)
start = time.time()
for _ in range(iterations):
test_func()
end = time.time()
print_with_timestamp(f"Time for {name.lower()}: {(end - start):.4f} seconds",
file=datafile)
print_with_timestamp("END TEST / BEGIN NEXT TEST", file=datafile)
def main():
""" Main script """
print_with_timestamp(f"Producing information for VENV ----> {os.getenv('CONDA_DEFAULT_ENV')}",
file=datafile)
# Capture np.show_config() output and print it line by line with timestamps
old_stdout = sys.stdout
new_stdout = StringIO()
sys.stdout = new_stdout
np.show_config()
sys.stdout = old_stdout
for line in new_stdout.getvalue().split("\n"):
print_with_timestamp(line, file=datafile)
if args.skip_tests:
print_with_timestamp("############### SKIPPING PERFORMANCE CHECKS", file=datafile)
else:
do_tests()
if datafile:
datafile.close()
if __name__ == "__main__":
main()