summaryrefslogtreecommitdiff
path: root/structvsarray.c
diff options
context:
space:
mode:
authorrodri <rgl@antares-labs.eu>2020-02-22 09:56:09 +0000
committerrodri <rgl@antares-labs.eu>2020-02-22 09:56:09 +0000
commita39951d8f69209cfea2b7051832b851914e662ef (patch)
treee4cd1c32e5d6f531b523f6fda558cc3a5f603547 /structvsarray.c
downloadbrokentoys-a39951d8f69209cfea2b7051832b851914e662ef.tar.gz
brokentoys-a39951d8f69209cfea2b7051832b851914e662ef.tar.bz2
brokentoys-a39951d8f69209cfea2b7051832b851914e662ef.zip
now version controlled.
Diffstat (limited to 'structvsarray.c')
-rw-r--r--structvsarray.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/structvsarray.c b/structvsarray.c
new file mode 100644
index 0000000..2f2ddaa
--- /dev/null
+++ b/structvsarray.c
@@ -0,0 +1,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);
+}