aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexandre Jesus <adbjesus@gmail.com>2016-09-16 01:15:07 +0100
committerAlexandre Jesus <adbjesus@gmail.com>2016-09-16 01:15:07 +0100
commit80b7a8116d821aeae489086a403a6cdfd64d801a (patch)
treeb362305db5c3d0b91e0714cb7fa29c2de8570e43
parentd8ea8dc5fa670477cb6a3c64a9fe7dbbb0c44898 (diff)
downloadlibuknapsack-80b7a8116d821aeae489086a403a6cdfd64d801a.tar.gz
libuknapsack-80b7a8116d821aeae489086a403a6cdfd64d801a.zip
Add time measure
-rw-r--r--include/structs.h4
-rw-r--r--src/common.c6
-rw-r--r--src/dfs.c1
3 files changed, 10 insertions, 1 deletions
diff --git a/include/structs.h b/include/structs.h
index cb1bca9..3b5c550 100644
--- a/include/structs.h
+++ b/include/structs.h
@@ -1,5 +1,8 @@
#ifndef _STRUCTS_H
#define _STRUCTS_H
+
+#include <time.h>
+
struct item {
double p;
double w;
@@ -16,5 +19,6 @@ struct front_item {
struct item i;
struct front_item * next;
struct front_item * prev;
+ clock_t time;
};
#endif
diff --git a/src/common.c b/src/common.c
index 92bd467..d6f1eb3 100644
--- a/src/common.c
+++ b/src/common.c
@@ -1,6 +1,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
+#include <time.h>
#include "structs.h"
#include "common.h"
@@ -47,8 +48,10 @@ void print_data(struct data * d){
}
void print_front(struct front_item * b){
+ struct front_item * beg = b;
while(b!=NULL){
- printf("%.02f\t%.02f\n",b->i.p,b->i.w);
+ clock_t t = b->time - beg->time;
+ printf("%.02f,\t%.02f,\t%f\n", b->i.p, b->i.w, ((double)t)/CLOCKS_PER_SEC);
b = b->next;
}
}
@@ -73,6 +76,7 @@ struct front_item * new_front_item(double p, double w, struct front_item * prev,
f->i.w = w;
f->next = next;
f->prev = prev;
+ f->time = clock();
return f;
}
diff --git a/src/dfs.c b/src/dfs.c
index 3f34001..bdf2b7e 100644
--- a/src/dfs.c
+++ b/src/dfs.c
@@ -14,6 +14,7 @@ struct front_item * rec(struct data * d, int i, struct item * it) {
struct front_item * f1 = rec(d, i+1, it);
it->p -= d->items[i].p;
it->w -= d->items[i].w;
+
// Go right (do not select item)
struct front_item * f2 = rec(d, i+1, it);