cmark

My personal build of CMark ✏️

Commit
7861d82c6fcfb3f813e642c0f59318eb4f9f5332
Parent
3c9bdf645958a1c5b71cc9b96a5b711cca14224f
Author
John MacFarlane <fiddlosopher@gmail.com>
Date

Added bench.h and inserted timing macros in main.

`make TIMER=1` to build with timings.

Diffstat

3 files changed, 42 insertions, 6 deletions

Status File Name N° Changes Insertions Deletions
Modified src/CMakeLists.txt 4 4 0
Added src/bench.h 22 22 0
Modified src/main.c 22 16 6
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
@@ -7,6 +7,7 @@ set(HEADERS
   chunk.h
   references.h
   debug.h
+  bench.h
   utf8.h
   scanners.h
   inlines.h
@@ -91,3 +92,6 @@ elseif(CMAKE_COMPILER_IS_GNUCC OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -std=c99 -pedantic")
 endif()
 
+if($ENV{TIMER})
+  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DTIMER=1")
+endif($ENV{TIMER})
diff --git a/src/bench.h b/src/bench.h
@@ -0,0 +1,22 @@
+#ifndef __BENCH_H__
+#define __BENCH_H__
+#include <stdio.h>
+#include <sys/time.h>
+
+#ifdef TIMER
+float _cmark_start_time;
+float _cmark_end_time;
+
+#define start_timer() \
+	_cmark_start_time = (float)clock()/CLOCKS_PER_SEC
+
+#define end_timer(M) \
+	_cmark_end_time = (float)clock()/CLOCKS_PER_SEC; \
+	fprintf(stderr, "[TIME] (%s:%d) %8.f ns " M "\n", __FILE__, \
+		__LINE__, (_cmark_end_time - _cmark_start_time) * 1000000)
+
+#else
+#define start_timer()
+#define end_timer(M)
+#endif
+#endif
diff --git a/src/main.c b/src/main.c
@@ -3,6 +3,7 @@
 #include <string.h>
 #include <errno.h>
 #include "cmark.h"
+#include "bench.h"
 
 void print_usage()
 {
@@ -25,6 +26,19 @@ static void print_document(node_block *document, bool ast)
 	}
 }
 
+void parse_and_render(node_block *document, FILE *fp, bool ast)
+{
+	start_timer();
+	document = cmark_parse_file(fp);
+	end_timer("cmark_parse_file");
+	start_timer();
+	print_document(document, ast);
+	end_timer("print_document");
+	start_timer();
+	cmark_free_blocks(document);
+	end_timer("free_blocks");
+}
+
 int main(int argc, char *argv[])
 {
 	int i, numfps = 0;
@@ -52,9 +66,7 @@ int main(int argc, char *argv[])
 	}
 
 	if (numfps == 0) {
-		document = cmark_parse_file(stdin);
-		print_document(document, ast);
-		cmark_free_blocks(document);
+		parse_and_render(document, stdin, ast);
 	} else {
 		for (i = 0; i < numfps; i++) {
 			FILE *fp = fopen(argv[files[i]], "r");
@@ -65,9 +77,7 @@ int main(int argc, char *argv[])
 				exit(1);
 			}
 
-			document = cmark_parse_file(fp);
-			print_document(document, ast);
-			cmark_free_blocks(document);
+			parse_and_render(document, fp, ast);
 			fclose(fp);
 		}
 	}