summaryrefslogtreecommitdiff
path: root/structvsarray.c
blob: 2f2ddaa2f1ed30cd962e5e73012256be69f17de8 (plain)
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
#include <u.h>
#include <libc.h>

enum {
	Ts,
	Tp,
	Ta,
	Tend
};

typedef struct Vec Vec;
struct Vec {
	double x, y, z;
};

typedef double Veca[3];

vlong t[Tend], t0;

Vec
addvec(Vec a, Vec b)
{
	return (Vec){a.x+b.x, a.y+b.y, a.z+b.z};
}

void
addvecp(Vec *a, Vec *b)
{
	a->x += b->x;
	a->y += b->y;
	a->z += b->z;
}

void
addveca(Veca a, Veca b)
{
	a[0] += b[0];
	a[1] += b[1];
	a[2] += b[2];
}

void
main()
{
	Vec a = {5, 2, 19};
	Vec b = {213, 30, -12};
	Veca aa = {2, 9, -1};
	Veca ab = {-10, 20, 30};

	t0 = nsec();
	a = addvec(a, b);
	t[Ts] = nsec()-t0;
	t0 = nsec();
	addvecp(&a, &b);
	t[Tp] = nsec()-t0;
	t0 = nsec();
	addveca(aa, ab);
	t[Ta] = nsec()-t0;
	print("struct\t%lldns\nstruct*\t%lldns\narray\t%lldns\n", t[Ts], t[Tp], t[Ta]);
	exits(nil);
}