1 | |
---|
2 | #include <fstream> |
---|
3 | #include <stdlib> |
---|
4 | #include <thread> |
---|
5 | |
---|
6 | #include "bench.h" |
---|
7 | |
---|
8 | //======================================= |
---|
9 | // time struct |
---|
10 | //======================================= |
---|
11 | |
---|
12 | // prevent dead-code removal |
---|
13 | struct StructDummy { |
---|
14 | volatile int i; |
---|
15 | }; |
---|
16 | |
---|
17 | void ?{}(StructDummy * this) __attribute__(( noinline )) { |
---|
18 | this->i = 2; |
---|
19 | } |
---|
20 | |
---|
21 | int get_i(StructDummy * this) { |
---|
22 | return this->i; |
---|
23 | } |
---|
24 | |
---|
25 | forall( dtype T | { int get_i(T*); } ) |
---|
26 | int bidirectional( StructDummy * this ) __attribute__(( noinline )) { |
---|
27 | return get_i( this ); |
---|
28 | } |
---|
29 | |
---|
30 | void BlockStructCreateDelete( int N ) { |
---|
31 | long long int StartTime, EndTime; |
---|
32 | |
---|
33 | StartTime = Time(); |
---|
34 | for ( int i = 0; i < N; i += 1 ) { |
---|
35 | StructDummy dummy; |
---|
36 | } |
---|
37 | EndTime = Time(); |
---|
38 | sout | "\t " | ( EndTime - StartTime ) / N; |
---|
39 | } |
---|
40 | |
---|
41 | void DynamicStructCreateDelete( int N ) { |
---|
42 | long long int StartTime, EndTime; |
---|
43 | |
---|
44 | StartTime = Time(); |
---|
45 | for ( int i = 0; i < N; i += 1 ) { |
---|
46 | StructDummy *dummy = new(); |
---|
47 | delete(dummy); |
---|
48 | } |
---|
49 | EndTime = Time(); |
---|
50 | sout | "\t " | ( EndTime - StartTime ) / N; |
---|
51 | } |
---|
52 | |
---|
53 | void StructBidirectional( int N ) { |
---|
54 | long long int StartTime, EndTime; |
---|
55 | StructDummy dummy; |
---|
56 | volatile int rv __attribute__(( unused )); |
---|
57 | |
---|
58 | StartTime = Time(); |
---|
59 | for ( int i = 0; i < N; i += 1 ) { |
---|
60 | rv = bidirectional( &dummy ); |
---|
61 | } |
---|
62 | EndTime = Time(); |
---|
63 | sout | "\t " | ( EndTime - StartTime ) / N; |
---|
64 | } |
---|
65 | |
---|
66 | //======================================= |
---|
67 | // time coroutine |
---|
68 | //======================================= |
---|
69 | |
---|
70 | coroutine CoroutineDummy {}; |
---|
71 | void main(CoroutineDummy * this) {} |
---|
72 | |
---|
73 | void ?{}(CoroutineDummy * this) { |
---|
74 | prime(this); |
---|
75 | } |
---|
76 | |
---|
77 | void BlockCoroutineCreateDelete( int N ) { |
---|
78 | long long int StartTime, EndTime; |
---|
79 | |
---|
80 | StartTime = Time(); |
---|
81 | for ( int i = 0; i < N; i += 1 ) { |
---|
82 | CoroutineDummy dummy; |
---|
83 | } |
---|
84 | EndTime = Time(); |
---|
85 | sout | "\t " | ( EndTime - StartTime ) / N; |
---|
86 | } |
---|
87 | |
---|
88 | void DynamicCoroutineCreateDelete( int N ) { |
---|
89 | long long int StartTime, EndTime; |
---|
90 | |
---|
91 | StartTime = Time(); |
---|
92 | for ( int i = 0; i < N; i += 1 ) { |
---|
93 | CoroutineDummy * dummy = new(); |
---|
94 | delete(dummy); |
---|
95 | } |
---|
96 | EndTime = Time(); |
---|
97 | sout | "\t " | ( EndTime - StartTime ) / N; |
---|
98 | } |
---|
99 | |
---|
100 | coroutine CoroutineResume { |
---|
101 | int N; |
---|
102 | }; |
---|
103 | |
---|
104 | void ?{}(CoroutineResume* this, int N) { |
---|
105 | this->N = N; |
---|
106 | prime(this); |
---|
107 | } |
---|
108 | |
---|
109 | void main(CoroutineResume* this) { |
---|
110 | for ( int i = 1; i <= this->N; i += 1 ) { |
---|
111 | suspend(); |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | void resumer(CoroutineResume* this) { |
---|
116 | long long int StartTime, EndTime; |
---|
117 | |
---|
118 | StartTime = Time(); |
---|
119 | for ( int i = 1; i <= this->N; i += 1 ) { |
---|
120 | resume(this); |
---|
121 | } |
---|
122 | EndTime = Time(); |
---|
123 | sout | "\t " | ( EndTime - StartTime ) / this->N; |
---|
124 | } |
---|
125 | |
---|
126 | //======================================= |
---|
127 | // time task |
---|
128 | //======================================= |
---|
129 | |
---|
130 | thread ThreadDummy {}; |
---|
131 | void main(ThreadDummy * this) {} |
---|
132 | |
---|
133 | void BlockTaskCreateDelete( int N ) { |
---|
134 | long long int StartTime, EndTime; |
---|
135 | |
---|
136 | StartTime = Time(); |
---|
137 | for ( int i = 0; i < N; i += 1 ) { |
---|
138 | scoped(ThreadDummy) dummy; |
---|
139 | } |
---|
140 | EndTime = Time(); |
---|
141 | sout | "\t " | ( EndTime - StartTime ) / N; |
---|
142 | } |
---|
143 | |
---|
144 | void DynamicTaskCreateDelete( int N ) { |
---|
145 | long long int StartTime, EndTime; |
---|
146 | |
---|
147 | StartTime = Time(); |
---|
148 | for ( int i = 0; i < N; i += 1 ) { |
---|
149 | scoped(ThreadDummy) * dummy = new(); |
---|
150 | delete(dummy); |
---|
151 | } |
---|
152 | EndTime = Time(); |
---|
153 | sout | "\t " | ( EndTime - StartTime ) / N; |
---|
154 | } |
---|
155 | |
---|
156 | thread ContextSwitch { |
---|
157 | int N; |
---|
158 | long long result; |
---|
159 | }; |
---|
160 | |
---|
161 | void main(ContextSwitch * this) { |
---|
162 | long long int StartTime, EndTime; |
---|
163 | |
---|
164 | StartTime = Time(); |
---|
165 | for ( int i = 1; i <= this->N; i += 1 ) { |
---|
166 | yield(); |
---|
167 | } // for |
---|
168 | EndTime = Time(); |
---|
169 | this->result = ( EndTime - StartTime ) / this->N; |
---|
170 | } |
---|
171 | |
---|
172 | void ?{}(ContextSwitch * this, int N) { |
---|
173 | this->N = N; |
---|
174 | } |
---|
175 | |
---|
176 | void ^?{}(ContextSwitch * this) { |
---|
177 | sout | "\t " | this->result; |
---|
178 | } |
---|
179 | |
---|
180 | //======================================= |
---|
181 | // benchmark driver |
---|
182 | //======================================= |
---|
183 | |
---|
184 | |
---|
185 | int main() { |
---|
186 | const int NoOfTimes = |
---|
187 | #if defined( __U_DEBUG__ ) // takes longer so run fewer iterations |
---|
188 | 100000; |
---|
189 | #else |
---|
190 | 1000000; |
---|
191 | #endif // __U_DEBUG__ |
---|
192 | |
---|
193 | sout | "\t\tcreate\tcreate\tcall/" | endl; |
---|
194 | sout | "(nsecs)"; |
---|
195 | sout | "\t\tdelete/\tdelete/\tctx" | endl; |
---|
196 | sout | "\t\tblock\tdynamic\tcycle" | endl; |
---|
197 | |
---|
198 | sout | "object\t"; |
---|
199 | BlockStructCreateDelete( NoOfTimes ); |
---|
200 | DynamicStructCreateDelete( NoOfTimes ); |
---|
201 | StructBidirectional( NoOfTimes ); |
---|
202 | sout | endl; |
---|
203 | |
---|
204 | sout | "coroutine"; |
---|
205 | BlockCoroutineCreateDelete( NoOfTimes ); |
---|
206 | DynamicCoroutineCreateDelete( NoOfTimes ); |
---|
207 | { |
---|
208 | CoroutineResume resumer = { NoOfTimes }; |
---|
209 | resumer(&resumer); |
---|
210 | } |
---|
211 | sout | endl; |
---|
212 | |
---|
213 | sout | "task\t"; |
---|
214 | BlockTaskCreateDelete( NoOfTimes ); |
---|
215 | DynamicTaskCreateDelete( NoOfTimes ); |
---|
216 | { |
---|
217 | ContextSwitch dummy = { (int)NoOfTimes }; // context switch |
---|
218 | } |
---|
219 | sout | "\t" | endl; |
---|
220 | } |
---|