-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark_threads.c
65 lines (46 loc) · 1.61 KB
/
benchmark_threads.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "sthread.h"
#include "benchmark.h"
#include "vector.h"
#define NUM_LOOPS_TRIG 100000
#define NUM_LOOPS_VECTOR 100000
#define BENCHMARK_LOOPS 10
#define THREAD_COUNT 16
void modify_values(int id, int thread_count, int count, void* data) {
float* actual_data = (float*) data;
for(int i = 0; i < NUM_LOOPS_TRIG; i++) {
actual_data[id] += cosf(i);
actual_data[id] += sinf(i);
}
}
void vector_operations(int id, int thread_count, int count, void* data) {
float* actual_data = (float*) data;
struct Vector3 one = vector3_create(1,1,1);
struct Vector3 two = vector3_create(2,2,2);
for(int i = 0; i < NUM_LOOPS_VECTOR; i++) {
one = vector3_cross_product(one, two);
one = vector3_to_length(one, 7.0f);
actual_data[id] += vector3_dot_product(one, two);
}
}
void main() {
srand(time(NULL));
benchmark_setupLocale();
int data[THREAD_COUNT];
for(int i = 0; i < THREAD_COUNT; i++) {
data[i] = 0;
}
float runs_per_second;
for(int i = 0; i < 5; i++) {
BENCHMARK_LOOPS_CODE( run_in_threads(modify_values, (void*) data, THREAD_COUNT, THREAD_COUNT), runs_per_second, BENCHMARK_LOOPS);
printf("Total trig ops per second: %'f\n", runs_per_second * (float) NUM_LOOPS_TRIG * (float) THREAD_COUNT);
}
for(int i = 0; i < 5; i++) {
BENCHMARK_LOOPS_CODE( run_in_threads(vector_operations, (void*) data, THREAD_COUNT, THREAD_COUNT), runs_per_second, BENCHMARK_LOOPS);
printf("Total vector ops per second: %'f\n", runs_per_second * (float) NUM_LOOPS_VECTOR * (float) THREAD_COUNT);
}
exit(0);
}