source: benchmark/bench.h @ 41870a5

ADTast-experimentalenumpthread-emulationqualifiedEnum
Last change on this file since 41870a5 was ce9f9d4, checked in by caparsons <caparson@…>, 3 years ago

added mutexstmt benchmark to make

  • Property mode set to 100644
File size: 1.3 KB
RevLine 
[8cb6fcd]1#pragma once
2
[0cf5b79]3#if defined(__cforall)
[8cb6fcd]4extern "C" {
[d67cdb7]5#endif
[034165a]6        #include <stdlib.h>
[846c026]7        #include <stdint.h>                             // uint64_t
8        #include <unistd.h>                             // sysconf
[9d32bc8]9#if ! defined(__cforall)
10        #include <time.h>
11        #include <sys/time.h>
12#else
[8cb6fcd]13}
[73abe95]14#include <time.hfa>
[d67cdb7]15#endif
[8cb6fcd]16
[d8548e2]17
[846c026]18static inline uint64_t bench_time() {
19        struct timespec ts;
20        clock_gettime( CLOCK_THREAD_CPUTIME_ID, &ts );
21        return 1000000000LL * ts.tv_sec + ts.tv_nsec;
22} // bench_time
[8cb6fcd]23
[ce9f9d4]24
25#if defined(__cforall)
26struct test_spinlock {
27        volatile bool lock;
28};
29
30static inline void lock( test_spinlock & this ) {
31        for ( ;; ) {
32                if ( (this.lock == 0) && (__atomic_test_and_set( &this.lock, __ATOMIC_ACQUIRE ) == 0) ) break;
33        }
34}
35
36static inline void unlock( test_spinlock & this ) {
37        __atomic_clear( &this.lock, __ATOMIC_RELEASE );
38}
39#endif
40
[034165a]41#ifndef BENCH_N
[846c026]42#define BENCH_N 10000000
[7286a40]43#endif
44
[846c026]45size_t times = BENCH_N;
46
47#define BENCH_START()                           \
48        if ( argc > 2 ) exit( EXIT_FAILURE );   \
49        if ( argc == 2 ) {                      \
50                times = atoi( argv[1] );        \
51        }
52
[034165a]53#define BENCH(statement, output)                \
[846c026]54        uint64_t StartTime, EndTime;            \
[9d32bc8]55        StartTime = bench_time();               \
[846c026]56        statement;                              \
[9d32bc8]57        EndTime = bench_time();                 \
[846c026]58        double output = (double)( EndTime - StartTime ) / times;
59
[034165a]60
[9d32bc8]61#if defined(__cforall)
[399a908]62Duration default_preemption() {
[7286a40]63        return 0;
[8ad6533]64}
[9d32bc8]65#endif
[846c026]66#if defined(__U_CPLUSPLUS__)
67unsigned int uDefaultPreemption() {
68        return 0;
69}
70#endif
Note: See TracBrowser for help on using the repository browser.