diff options
author | Julian T <julian@jtle.dk> | 2019-11-22 11:06:21 +0100 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2019-11-22 11:06:21 +0100 |
commit | 90999d01eff3b197001739ee3ee9ff6dd64217ce (patch) | |
tree | 0b407668c76cd5d06d3e233c631ff2105e41b4d4 /sem1/algo/workshop2/dfs/dfs.c | |
parent | 4fd77927337b0243dac88ccb128af8268ea423f7 (diff) |
Added bench
Diffstat (limited to 'sem1/algo/workshop2/dfs/dfs.c')
-rw-r--r-- | sem1/algo/workshop2/dfs/dfs.c | 47 |
1 files changed, 39 insertions, 8 deletions
diff --git a/sem1/algo/workshop2/dfs/dfs.c b/sem1/algo/workshop2/dfs/dfs.c index 8c4faf6..1244895 100644 --- a/sem1/algo/workshop2/dfs/dfs.c +++ b/sem1/algo/workshop2/dfs/dfs.c @@ -1,6 +1,8 @@ #include <stdio.h> #include <stdbool.h> #include <time.h> +#include <string.h> +#include <stdlib.h> #include "graph.h" @@ -10,7 +12,7 @@ int step; void dfs_visit(graph_t *g, vertex_t *u, bool doprint) { if( doprint ) { - printf("dfs_visit( %c )\n", u->ref); + printf("dfs_visit( %s )\n", u->ref); } step++; u->dtime = step; @@ -22,7 +24,6 @@ void dfs_visit(graph_t *g, vertex_t *u, bool doprint) { vertex_t *v = e->to; if( v->color == COLOR_WHITE ) { graph_edge(&newg, v->ref, u->ref, 1); - v->p = u; dfs_visit(g, v, doprint); } e = e->next; @@ -47,16 +48,46 @@ void dfs(graph_t *g, bool doprint) { } } +#define BENCH 9999 + int main() { + + // benchmark + + // Start by generating + for(int i = 0; i < BENCH; i++) { + char from[5]; + char to[5]; + sprintf(from, "%d", i); + sprintf(to, "%d", rand() % BENCH); + graph_edge(&g, from, to, 1); + } + FILE *f = fopen("hej.dot", "w"); + graph_to_dot(f, &g); + + { + // Timing start + clock_t t; + t = clock(); + + dfs(&g, true); + + // Timing stop + t = clock() - t; + double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds + printf("Dfs took %f seconds\n", time_taken); + } + + return 0; - graph_edge(&g, 'M', 'O', 1); - graph_edge(&g, 'O', 'P', 1); - graph_edge(&g, 'P', 'F', 1); + graph_edge(&g, "hej", "med", 1); + graph_edge(&g, "med", "dig", 1); + graph_edge(&g, "dig", "hvordan", 1); - graph_edge(&g, 'C', 'F', 1); - graph_edge(&g, 'F', 'c', 1); + graph_edge(&g, "det", "hvordan", 1); + graph_edge(&g, "hvordan", "det", 1); - //graph_to_dot(stdout, &g); + graph_to_dot(stdout, &g); // Timing start clock_t t; |