-
Notifications
You must be signed in to change notification settings - Fork 1
/
Benchmark2.py
106 lines (96 loc) · 3.78 KB
/
Benchmark2.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python3
# ULLONG_MAX +18 446 744 073 709 551 615
# import libprimes
import timeit
import sys
import datetime
import prettyplotlib as ppl
import numpy as np
import matplotlib.pyplot as plt
from prettyplotlib import brewer2mpl
import warnings
import matplotlib.cbook
warnings.filterwarnings("ignore", category=matplotlib.cbook.mplDeprecation)
if __name__ == '__main__':
# Parse command line options
if len(sys.argv) <= 1:
print("Please set loop numbers")
sys.exit()
N = int(sys.argv[1])
n = 10**N
nbs = np.array([10**i for i in range(3, N + 1)])
nb_benchloop = 3
datetimeformat = '%Y-%m-%d %H:%M:%S.%f'
config = 'from __main__ import n; import pyximport; pyximport.install(); \
import libprimes; import cprimes; import TestFortran'
primenumbers_gen = {
# 'libprimes.sundaram3': {'color': 'b'},
# 'cprimes.sundaram3_4': {'color': 'b'},
# 'cprimes.sundaram3_1': {'color': 'b'},
# 'cprimes.sundaram3_2': {'color': 'b'},
# 'cprimes.sundaram3_2_1': {'color': 'b'},
# 'cprimes.sundaram3_3_1': {'color': 'b'},
# 'cprimes.sundaram3_3_2': {'color': 'b'},
# 'cprimes.sundaram3_3_3': {'color': 'b'},
#
# 'cprimes.cprimes_ajs': {'color': 'b'},
#
# 'cprimes.ambi_sieve_1': {'color': 'b'},
# 'cprimes.ambi_sieve_2': {'color': 'b'},
# 'libprimes.ambi_sieve': {'color': 'b'},
# Our Benchmark
'libprimes.primesfrom3to': {'color': 'b'},
# 'cprimes.primesfrom3to_1': {'color': 'b'},
# 'cprimes.primesfrom3to_2': {'color': 'b'},
# 'cprimes.primesfrom3to_3': {'color': 'b'},
# 'cprimes.primesfrom3to_4': {'color': 'b'},
# 'cprimes.primesfrom3to_5': {'color': 'b'},
# 'cprimes.cprimes_primesfrom3to': {'color': 'b'},
# Breaks with error
# malloc: *** error for object 0x7feb99a2ac00: incorrect checksum for
# freed object - object was probably modified after being freed
# https://stackoverflow.com/questions/35944478
# https://stackoverflow.com/questions/12309161/malloc-error-in-f2py
# 'TestFortran.primes': {'color': 'b'},
'TestFortran.primes2': {'color': 'b'},
# 'TestFortran.primes3': {'color': 'b'},
}
print("Compute prime number to {0:G}".format(n))
print(datetime.datetime.now().strftime(datetimeformat))
results = dict()
for pgen in primenumbers_gen:
results[pgen] = dict()
benchtimes = list()
for n in nbs:
t = timeit.Timer("%(pgen)s(n)" % locals(), setup=config)
execute = timeit.Timer("%(pgen)s(n)" % locals(), setup=config)
execute_times = t.repeat(repeat=nb_benchloop, number=1)
benchtime = np.mean(execute_times)
benchtimes.append(benchtime)
print('.', end='', flush=True)
results[pgen] = {'benchtimes': np.array(benchtimes)}
print('')
print(datetime.datetime.now().strftime(datetimeformat))
fig, ax = plt.subplots(1)
plt.ylabel('Computation time (in milisec) - log scale.')
plt.xlabel('N - log scale')
plt.xscale('log')
plt.yscale('log')
set2 = brewer2mpl.get_map('Paired', 'qualitative', 12).mpl_colors
i = 0
np.set_printoptions(suppress=True)
np.set_printoptions(precision=2)
print("Results in miliseconds")
for pgen in primenumbers_gen:
bench = results[pgen]['benchtimes'] * 1000
print("%(pgen)s avg" % locals(), bench)
i += 1
label = "%(pgen)s avg" % locals()
ppl.plot(nbs, bench, label=label, lw=2, color=set2[i % 12])
ppl.legend(ax, loc='upper left', ncol=4)
fig.canvas.draw()
ax = plt.gca()
plt.grid()
plt.show()
print("Don't waist your time improving ineffective algorithm!\n \
Use better algo!")