From 04648b08d1697afea7bdaefa503196973505f900 Mon Sep 17 00:00:00 2001 From: cozis Date: Mon, 9 May 2022 03:32:23 +0200 Subject: [PATCH] speedup in iskword --- README.md | 2 +- c2html.c | 31 +++++++++++++++++++------------ cli.c | 36 ++++++++++++++++++++++++++++++++++-- makefile | 2 +- 4 files changed, 55 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 413c5b0..62ae3db 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,7 @@ when executed, the output will be: The code is very portable so it's possible to run it everywhere, although the build proces was only tested on Linux. ## Install the library -There is no particular way to install the library. The code is so small that you can just drop `c2html.c` and `c2html.h` in your project and use then as they were your own. +There is no particular way to install the library. The code is so small that you can just drop `c2html.c` and `c2html.h` in your project and use them as they were your own. ## Install the command-line interface To build the CLI, run diff --git a/c2html.c b/c2html.c index ee07866..c93860b 100644 --- a/c2html.c +++ b/c2html.c @@ -78,21 +78,28 @@ static bool isoperat(char c) static bool iskword(const char *str, long len) { - static const char *keywords[] = { - "auto", "break", "case", "char", - "const", "continue", "default", - "do", "double", "else", "enum", - "extern", "float", "for", "goto", - "if", "int", "long", "register", - "return", "short", "signed", - "sizeof", "static", "struct", - "switch", "typedef", "union", - "unsigned", "void", "volatile", - "while" + static const struct { + int len; + char str[9]; // Maximum length of a keyword. + } keywords[] = { + #define KWORD(lit) { sizeof(lit)-1, lit } + KWORD("auto"), KWORD("break"), KWORD("case"), + KWORD("char"), KWORD("const"), KWORD("continue"), + KWORD("default"), KWORD("do"), KWORD("double"), + KWORD("else"), KWORD("enum"), KWORD("extern"), + KWORD("float"), KWORD("for"), KWORD("goto"), + KWORD("if"), KWORD("int"), KWORD("long"), + KWORD("register"), KWORD("return"), KWORD("short"), + KWORD("signed"), KWORD("sizeof"), KWORD("static"), + KWORD("struct"), KWORD("switch"), KWORD("typedef"), + KWORD("union"), KWORD("unsigned"),KWORD("void"), + KWORD("volatile"), KWORD("while"), + #undef KWORD }; + const int num_keywords = sizeof(keywords)/sizeof(keywords[0]); for(int i = 0; i < num_keywords; i += 1) - if((int) strlen(keywords[i]) == len && !strncmp(keywords[i], str, len)) + if(keywords[i].len == len && !strncmp(keywords[i].str, str, len)) return 1; return 0; } diff --git a/cli.c b/cli.c index b9c7b2a..10890d9 100644 --- a/cli.c +++ b/cli.c @@ -4,6 +4,39 @@ #include #include "c2html.h" +#ifdef C2H_TIMING +#include +char *timed_c2html(const char *str, long len, + const char *prefix, const char **error) +{ + if(str == NULL) + str = ""; + + if(len < 0) + len = strlen(str); + + clock_t beg, end; + char *res; + + beg = clock(); + res = c2html(str, len, prefix, error); + end = clock(); + + double time_spent = (double) (end - beg) + / CLOCKS_PER_SEC; + fprintf(stdout, + "-- Timing results ----\n" + "Size: %ldb\n" + "Time: %fs\n" + "Rate: %.2f Mb/s\n" + "----------------------\n", + len, time_spent, + (double) len/(time_spent * 1000000)); + return res; +} +#define c2html timed_c2html +#endif + static char *load_file(const char *file, long *size) { FILE *fp = fopen(file, "rb"); @@ -88,7 +121,6 @@ static int fileconv(const char *input_file, const char *output_file, /* ..and write the converted string to it */ fwrite(output, 1, strlen(output), fp); fclose(fp); - fprintf(stderr, "OK\n"); /* All done! :^) */ free(output); @@ -107,7 +139,7 @@ static void print_help(FILE *fp, char *name) { " to the generated output, you get the highliting!\n" "\n" " The usage is:\n" - " $ %s -i file.c -o file.html [--no-table] [--style file.css] [-p ]\n" + " $ %s -i file.c -o file.html [--style file.css] [-p ]\n" "\n" " ..and here's a table of all available options:\n" "\n" diff --git a/makefile b/makefile index f613320..acad788 100644 --- a/makefile +++ b/makefile @@ -1,5 +1,5 @@ CC = gcc -CFLAGS = -Wall -Wextra -DNDEBUG -O3 +CFLAGS = -Wall -Wextra -DNDEBUG -O3 -DC2H_TIMING # CFLAGS = -Wall -Wextra -g # When debugging .PHONY: all install clean