source: src/benchmark/bench.c@ 39c5ea3

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 39c5ea3 was b510ac2, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Updated the benchmarks to new concurrency syntax

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