File: benchmark-qtcurrent.py

package info (click to toggle)
python-qmix 1.0.6-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,460 kB
  • sloc: python: 4,312; makefile: 215
file content (167 lines) | stat: -rw-r--r-- 5,190 bytes parent folder | download
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""Benchmark the qtcurrent function."""

import argparse
import datetime
import socket
import timeit
import pickle 
import numpy

import qmix
from qmix.qtcurrent import qtcurrent

# Arguments
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--save', action="store_true", help="save speed run?")
parser.add_argument('-t', '--tone', type=int, help="tone to run")
parser.add_argument('-o', '--overwrite', action="store_true", help="overwrite stored value")
args = parser.parse_args()

# Setup ----------------------------------------------------------------------

resp = qmix.respfn.RespFnPolynomial(50, verbose=False)

num_b = (10, 5, 5, 5)

print("\n\tRUNNING SPEED TEST:\n")

# 1 tone ---------------------------------------------------------------------

if args.tone is None or args.tone == 1:

    cct = qmix.circuit.EmbeddingCircuit(1, 1)
    cct.freq[1] = 0.3

    vj = cct.initialize_vj()
    vj[1,1,:] = 0.3

    def one_tone():

        current = qtcurrent(vj, cct, resp, 0., num_b=num_b, verbose=False)

    t_1tone = min(timeit.Timer(one_tone).repeat(3000, 1))
    print("1 tone:\t\t{:7.2f} ms".format(t_1tone * 1000))

    # Compare to previously calculated values
    current_test = qtcurrent(vj, cct, resp, 0., num_b=num_b, verbose=False)
    if args.overwrite:
        print(" -> Saving current")
        with open('data/qtcurrent1.data', 'wb') as f:
            pickle.dump(current_test, f)
    else:
        print(" -> Checking current")
        with open('data/qtcurrent1.data', 'rb') as f:
            current_known = pickle.load(f)
        numpy.testing.assert_almost_equal(current_test, current_known, decimal=15)

# 2 tone ---------------------------------------------------------------------

if args.tone is None or args.tone == 2:

    cct = qmix.circuit.EmbeddingCircuit(2, 1)
    cct.freq[1] = 0.30
    cct.freq[2] = 0.33

    vj = cct.initialize_vj()
    vj[1,1,:] = 0.3
    vj[2,1,:] = 0.1

    def two_tone():

        qtcurrent(vj, cct, resp, 0., num_b=num_b, verbose=False)

    t_2tone = min(timeit.Timer(two_tone).repeat(200, 1))
    print("2 tones:\t{:7.2f} ms".format(t_2tone * 1000))

    # Compare to previously calculated values
    current_test = qtcurrent(vj, cct, resp, 0., num_b=num_b, verbose=False)
    if args.overwrite:
        print(" -> Saving current")
        with open('data/qtcurrent2.data', 'wb') as f:
            pickle.dump(current_test, f)
    else:
        print(" -> Checking current")
        with open('data/qtcurrent2.data', 'rb') as f:
            current_known = pickle.load(f)
        numpy.testing.assert_almost_equal(current_test, current_known, decimal=15)

# 3 tone ---------------------------------------------------------------------

if args.tone is None or args.tone == 3:

    cct = qmix.circuit.EmbeddingCircuit(3, 1)
    cct.freq[1] = 0.30
    cct.freq[2] = 0.33
    cct.freq[3] = 0.27

    vj = cct.initialize_vj()
    vj[1,1,:] = 0.3
    vj[2,1,:] = 0.1
    vj[3,1,:] = 0.1

    def three_tone():

        qtcurrent(vj, cct, resp, 0., num_b=num_b, verbose=False)

    t_3tone = min(timeit.Timer(three_tone).repeat(50, 1))
    print("3 tones:\t{:7.2f} ms".format(t_3tone * 1000))

    # Compare to previously calculated values
    current_test = qtcurrent(vj, cct, resp, 0., num_b=num_b, verbose=False)
    if args.overwrite:
        print(" -> Saving current")
        with open('data/qtcurrent3.data', 'wb') as f:
            pickle.dump(current_test, f)
    else:
        print(" -> Checking current")
        with open('data/qtcurrent3.data', 'rb') as f:
            current_known = pickle.load(f)
        numpy.testing.assert_almost_equal(current_test, current_known, decimal=15)

# 4 tone ---------------------------------------------------------------------

if args.tone is None or args.tone == 4:

    cct = qmix.circuit.EmbeddingCircuit(4, 1)
    cct.freq[1] = 0.30
    cct.freq[2] = 0.33
    cct.freq[3] = 0.27
    cct.freq[4] = 0.03

    vj = cct.initialize_vj()
    vj[1,1,:] = 0.3
    vj[2,1,:] = 0.1
    vj[3,1,:] = 0.1
    vj[4,1,:] = 0.0

    def four_tone():

        qtcurrent(vj, cct, resp, 0., num_b=num_b, verbose=False)

    t_4tone = min(timeit.Timer(four_tone).repeat(10, 1))
    print("4 tones:\t{:7.2f} ms".format(t_4tone * 1000))

    # Compare to previously calculated values
    current_test = qtcurrent(vj, cct, resp, 0., num_b=num_b, verbose=False)
    if args.overwrite:
        print(" -> Saving current")
        with open('data/qtcurrent4.data', 'wb') as f:
            pickle.dump(current_test, f)
    else:
        print(" -> Checking current")
        with open('data/qtcurrent4.data', 'rb') as f:
            current_known = pickle.load(f)
        numpy.testing.assert_almost_equal(current_test, current_known, decimal=15)

print("")

# WRITE TO FILE --------------------------------------------------------------

if args.tone is None:
    if args.save:
        with open('results/qtcurrent.txt', 'a') as f:
            now = datetime.datetime.now()
            machine = socket.gethostname()
            msg = "{}\t{:.6f}\t{:.6f}\t{:.6f}\t{:.6f}\t{}\n"
            msg = msg.format(now, t_1tone, t_2tone, t_3tone, t_4tone, machine)
            f.write(msg)