3 * Keyword benchmark, frontend
5 * (c) 2019 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the Sensible Object Design, an object system for C.
12 * SOD is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
17 * SOD is distributed in the hope that it will be useful, but WITHOUT ANY
18 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License
23 * along with SOD. If not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33 #if defined(__GNUC__) && (defined(__i386__) || defined(__amd64__))
35 typedef unsigned long long timer;
36 static void init_timing(void) { ; }
37 static timer when(void)
41 __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
42 return (((timer)hi << 32) | lo);
44 #elif defined(__linux__)
45 #include <linux/perf_event.h>
46 #include <asm/unistd.h>
50 typedef uint64_t timer;
51 static int perf_fd = -1;
52 static void init_timing(void)
54 struct perf_event_attr attr = { 0 };
56 attr.type = PERF_TYPE_HARDWARE;
57 attr.size = sizeof(attr);
58 attr.config = PERF_COUNT_HW_CPU_CYCLES;
60 attr.exclude_kernel = 1;
63 if ((perf_fd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0)) < 0)
64 perror("failed to open perf event");
66 static timer when(void)
71 if (perf_fd == -1) goto fail;
72 n = read(perf_fd, &cy, sizeof(cy));
73 if (n != sizeof(cy)) {
74 if (n < 0) perror("failed to read perf event");
75 else fprintf(stderr, "unexpected short read from perf event\n");
76 close(perf_fd); perf_fd = -1;
87 static void init_timing(void) { ; }
88 static timer when(void)
91 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tv);
92 return (1e9*(timer)tv.tv_sec + tv.tv_nsec);
96 #define NITER 100000000
108 for (i = 0; i < NITER; i++) kwbench_trivial(&obj, 4, 0, 0);
110 t = t1 - t0; printf("trivial: %g "TIMEWHAT"\n", t/NITER);
113 for (i = 0; i < NITER; i++)
114 kwbench_keywords(&obj, KWARGS(K(z, 19)));
116 t = t1 - t0; printf("keywords: %g "TIMEWHAT"\n", t/NITER);
121 /*----- That's all, folks -------------------------------------------------*/