Skip to content

Commit 8a6bffb

Browse files
committed
trying to optimize for performance by precomputing strings
1 parent 0855607 commit 8a6bffb

2 files changed

Lines changed: 26 additions & 21 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@ Run with `--addsep=chars` to add `chars` as extra word separators and with
1313
Go to releases for a 32-bit Windows exe (keep in mind 24-bit ANSI colors in
1414
console require quite a recent Windows 10 version:
1515
[link](https://devblogs.microsoft.com/commandline/24-bit-color-in-the-windows-console/)).
16+
17+
For optimal performance (the above exe is from a niche C compiler and not
18+
optimized) you should compile it yourself with
19+
`gcc -O3 -march=native colors.c -o colors` or similar. Please let me know if it
20+
doesn't compile or doesn't work on your OS, Distro, GCC default settings, etc.

colors.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,25 +62,28 @@ static int enableConsoleColor(void)
6262
#endif /* COLORS_ON_WINDOWS */
6363
}
6464

65-
const unsigned kColors[] = {
66-
0xffffff, 0xff0000, 0x00ff00, 0x7f0000,
67-
0x007f00, 0x00ffff, 0xff00ff, 0xffff00,
68-
0x007fff, 0x3fff3f, 0x007f3f, 0x7f7f00,
65+
const const char * kColors[] = {
66+
#define FORMAT_COLOR(r, g, b) "\033[38;2;"#r";"#g";"#b"m"
67+
FORMAT_COLOR(255, 255, 255),
68+
FORMAT_COLOR(255, 0, 0),
69+
FORMAT_COLOR(0, 255, 0),
70+
FORMAT_COLOR(127, 0, 0),
71+
FORMAT_COLOR(0, 127, 0),
72+
FORMAT_COLOR(0, 255, 255),
73+
FORMAT_COLOR(255, 0, 255),
74+
FORMAT_COLOR(255, 255, 0),
75+
FORMAT_COLOR(0, 127, 255),
76+
FORMAT_COLOR(63, 255, 63),
77+
FORMAT_COLOR(0, 127, 63),
78+
FORMAT_COLOR(127, 127, 0),
79+
#undef FORMAT_COLOR
6980
};
7081

7182
const int kColorCount = sizeof(kColors) / sizeof(kColors[0]);
7283

73-
static void setColor(unsigned rgb)
74-
{
75-
const int r = (rgb >> 16) & 0xff;
76-
const int g = (rgb >> 8) & 0xff;
77-
const int b = (rgb >> 0) & 0xff;
78-
printf("\033[38;2;%d;%d;%dm", r, g, b);
79-
}
80-
8184
static void resetColor(void)
8285
{
83-
printf("\033[0m"); /* is this correct? */
86+
fputs("\033[0m", stdout); /* is this correct? */
8487
}
8588

8689
/* 32-bit fnv1, not 1a */
@@ -100,7 +103,7 @@ static unsigned fnv(const char * str)
100103
static void printColoredByHash(const char * str)
101104
{
102105
const int idx = fnv(str) % kColorCount;
103-
setColor(kColors[idx]);
106+
fputs(kColors[idx], stdout);
104107
fputs(str, stdout); /* not puts to not get a newline */
105108
resetColor();
106109
}
@@ -185,17 +188,14 @@ static int printhelp(const char * argv0)
185188

186189
/* print colors in their color, if possible, else in default color */
187190
ok = enableConsoleColor();
188-
printf("Available colors (%s):\n", ok ? "in that color each" : "values only");
191+
printf("Available color format strings (%s):\n", ok ? "in that color each" : "values only");
189192
for(i = 0; i < kColorCount; ++i)
190193
{
191-
const int c = kColors[i];
194+
const char * c = kColors[i];
192195
if(ok)
193-
setColor(c);
196+
fputs(c, stdout);
194197

195-
printf("#%06x rgb(%d, %d, %d)\n",
196-
kColors[i],
197-
(c >> 16) & 0xff, (c >> 8) & 0xff, (c >> 0) & 0xff
198-
);
198+
puts(c + 1); /* skip the ESC char to not interpret this as control sequence */
199199
} /* for each color */
200200

201201
resetColor();

0 commit comments

Comments
 (0)