source: src/benchmark/bench.c @ 29137d3

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 29137d3 was b510ac2, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Updated the benchmarks to new concurrency syntax

  • Property mode set to 100644
File size: 4.9 KB
Line 
1
2#include <fstream>
3#include <stdlib>
4#include <thread>
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
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
84//=======================================
85// time coroutine
86//=======================================
87
88coroutine CoroutineDummy {};
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
118coroutine CoroutineResume {
119    int N;
120};
121
122void ?{}(CoroutineResume* this, int N) {
123      this->N = N;
124        prime(this);
125}
126
127void main(CoroutineResume* this) {
128        for ( int i = 1; i <= this->N; i += 1 ) {
129                suspend();
130        }
131}
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);
139        }
140        EndTime = Time();
141        sout | "\t " | ( EndTime - StartTime ) / this->N;
142}
143
144//=======================================
145// time task
146//=======================================
147
148thread ThreadDummy {};
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
174thread ContextSwitch {
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//=======================================
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
211        sout | "\t\tcreate\tcreate\tcall/" | endl;
212        sout | "(nsecs)";
213        sout | "\t\tdelete/\tdelete/\tctx" | endl;
214        sout | "\t\tblock\tdynamic\tcycle" | endl;
215
216        sout | "object\t";
217        BlockStructCreateDelete( NoOfTimes );
218        DynamicStructCreateDelete( NoOfTimes );
219        StructBidirectional( NoOfTimes );
220        sout | endl;
221
222        sout | "coroutine";
223        BlockCoroutineCreateDelete( NoOfTimes );
224        DynamicCoroutineCreateDelete( NoOfTimes );
225        {
226                CoroutineResume resumer = { NoOfTimes };
227                resumer(&resumer);
228        }
229        sout | endl;
230
231        sout | "task\t";
232        BlockTaskCreateDelete( NoOfTimes );
233        DynamicTaskCreateDelete( NoOfTimes );
234        {
235                ContextSwitch dummy = { (int)NoOfTimes };               // context switch
236        }
237        sout | "\t" | endl;
238}
Note: See TracBrowser for help on using the repository browser.