/
array_creation.py
50 lines (41 loc) · 1.64 KB
/
array_creation.py
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
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from __future__ import absolute_import
"""
Benchmark for comparing different methods of creating arrays in Python
"""
__authors__ = ["Keith Hughitt, Steven Christe, Albert Shih"]
__email__ = "keith.hughitt@nasa.gov"
import numpy as np
import sys
import benchmark
def main():
"""Main application"""
timer = benchmark.BenchmarkTimer()
options = timer.parse_arguments()
timer.print_header("ARRAY CREATION")
run_tests(timer, options.scale_factor)
timer.print_summary()
def run_tests(timer, scale_factor):
'''Go through each test and print out the results'''
#initialize timer
timer.reset()
#Test 1 - Distance matrix creation (method: list comprehension)
#
#Creates an square matrix where each value in the matrix represents the
#distance from that point to the center of the matrix.
size = 4096 * scale_factor
a = np.array([[x for x in range(size)] for y in range(size)], dtype='f')
offset = (size - 1) / 2.
x = (a - offset).flatten()
y = (a.T - offset).flatten()
result = np.array(zip(x, y)).reshape(size, size, 2)
timer.log("%d x %d distance matrix creation (list comp)" % (size, size))
#Test 2 - Distance matrix creation (method: numpy methods)
#Note: format of result differs from above. effect on performance?
tempa = (np.arange(size ** 2) % size) - (size - 1) / 2.
tempb = tempa.reshape(size, size).transpose().reshape(size ** 2)
result = np.array(zip(tempa, tempb)) * 1.
timer.log("%d x %d distance matrix creation (numpy)" % (size, size))
if __name__ == '__main__':
sys.exit(main())