source: src/examples/Bench.c @ a2a77af

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 a2a77af was 550a338, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Added quick benchmark for coroutines

  • Property mode set to 100644
File size: 1.9 KB
Line 
1
2
3#include <fstream>
4#include <threads>
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 coroutine
28//=======================================
29
30struct CoroutineResume {
31    int N;
32    coroutine c;
33};
34
35coroutine* get_coroutine(CoroutineResume* this);
36void co_main(CoroutineResume* this);
37
38void ?{}(CoroutineResume* this, int N) {
39      this->N = N;
40        prime(this);
41}
42
43coroutine* get_coroutine(CoroutineResume* this) {
44      return &this->c;
45}
46
47void co_main(CoroutineResume* this) {
48        for ( int i = 1; i <= this->N; i += 1 ) {
49                suspend();
50        } // for
51} // CoroutineResume::main
52
53void resumer(CoroutineResume* this) {
54        long long int StartTime, EndTime;
55
56        StartTime = Time();
57        for ( int i = 1; i <= this->N; i += 1 ) {
58                resume(this);
59        } // for
60        EndTime = Time();
61        sout | "\t " | ( EndTime - StartTime ) / this->N;
62} // CoroutineResume::resumer
63
64
65int main() {
66        const int NoOfTimes =
67#if defined( __U_DEBUG__ )                              // takes longer so run fewer iterations
68        100000;
69#else
70        1000000;
71#endif // __U_DEBUG__
72
73        sout | "\t\tcreate\tcreate\t16i/4o\t16i/4o\tresume/\tsignal/" | endl;
74        sout | "(nsecs)";
75        sout | "\t\tdelete/\tdelete/\tbytes\tbytes\tsuspend\twait" | endl;
76        sout | "\t\tblock\tdynamic\tdirect\taccept\tcycle\tcycle" | endl;
77
78        sout | "class\t";
79        sout | "\t N/A\t N/A\t N/A";
80        sout | "\t N/A\t N/A\t N/A";
81        sout | "\t" | endl;
82
83        sout | "coroutine";
84        sout | "\t N/A\t N/A\t N/A";
85        sout | "\t N/A";
86        {
87                CoroutineResume resumer = { NoOfTimes };
88                resumer(&resumer);
89        }
90        sout | "\t N/A";
91
92        sout | "\t" | endl;
93}
Note: See TracBrowser for help on using the repository browser.