speedup in iskword

This commit is contained in:
cozis
2022-05-09 03:32:23 +02:00
parent 96921385fa
commit 04648b08d1
4 changed files with 55 additions and 16 deletions
+1 -1
View File
@@ -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
+19 -12
View File
@@ -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;
}
+34 -2
View File
@@ -4,6 +4,39 @@
#include <stdio.h>
#include "c2html.h"
#ifdef C2H_TIMING
#include <time.h>
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 <prefix>]\n"
" $ %s -i file.c -o file.html [--style file.css] [-p <prefix>]\n"
"\n"
" ..and here's a table of all available options:\n"
"\n"
+1 -1
View File
@@ -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