Skip to content

Instantly share code, notes, and snippets.

@ircmaxell
Last active November 4, 2023 19:02
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ircmaxell/c26ff31a80ac69b1349a to your computer and use it in GitHub Desktop.
Save ircmaxell/c26ff31a80ac69b1349a to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/resource.h>
double get_time()
{
struct timeval t;
struct timezone tzp;
gettimeofday(&t, &tzp);
return t.tv_sec + t.tv_usec * 1e-6;
}
static char hexconvtab[] = "0123456789abcdef";
static char* ts_bin2hex(const unsigned char *old, const size_t oldlen)
{
char *result = (char*) malloc(oldlen * 2 + 1);
size_t i, j;
int b = 0;
for (i = j = 0; i < oldlen; i++) {
b = old[i] >> 4;
result[j++] = (char) (87 + b + (((b - 10) >> 31) & -39));
b = old[i] & 0xf;
result[j++] = (char) (87 + b + (((b - 10) >> 31) & -39));
}
result[j] = '\0';
return result;
}
static char* php_bin2hex(const unsigned char *old, const size_t oldlen)
{
char *result = (char*) malloc(oldlen * 2 + 1);
size_t i, j;
for (i = j = 0; i < oldlen; i++) {
result[j++] = hexconvtab[old[i] >> 4];
result[j++] = hexconvtab[old[i] & 15];
}
result[j] = '\0';
return result;
}
main()
{
char *t, *s = "this is a test";
size_t len = sizeof("this is a test");
int i;
double start, end;
start = get_time();
for (i = 0; i < 1000000; i++) {
t = php_bin2hex(s, len);
free(t);
}
end = get_time();
printf("PHP bin2hex In %f Seconds\r\n", end - start);
start = get_time();
for (i = 0; i < 1000000; i++) {
t = ts_bin2hex(s, len);
free(t);
}
end = get_time();
printf("TS bin2hex In %f Seconds\r\n", end - start);
}
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/resource.h>
double get_time()
{
struct timeval t;
struct timezone tzp;
gettimeofday(&t, &tzp);
return t.tv_sec + t.tv_usec * 1e-6;
}
static char *php_hex2bin(const unsigned char *old, const size_t oldlen)
{
size_t target_length = oldlen >> 1;
char *str = (char *) malloc(target_length);
unsigned char *ret = str;
size_t i, j;
for (i = j = 0; i < target_length; i++) {
unsigned char c = old[j++];
unsigned char d;
if (c >= '0' && c <= '9') {
d = (c - '0') << 4;
} else if (c >= 'a' && c <= 'f') {
d = (c - 'a' + 10) << 4;
} else if (c >= 'A' && c <= 'F') {
d = (c - 'A' + 10) << 4;
} else {
free(str);
return NULL;
}
c = old[j++];
if (c >= '0' && c <= '9') {
d |= c - '0';
} else if (c >= 'a' && c <= 'f') {
d |= c - 'a' + 10;
} else if (c >= 'A' && c <= 'F') {
d |= c - 'A' + 10;
} else {
free(str);
return NULL;
}
ret[i] = d;
}
ret[i] = '\0';
return str;
}
static char *ts_hex2bin(const unsigned char *old, const size_t oldlen) {
size_t target_length = oldlen >> 1;
char *str = (char *) malloc(target_length);
size_t i;
for (i = 0; i < target_length; i++) {
int temp = -1;
int t2 = 0;
int ch = (unsigned char) old[2 * i];
// if (ch > 0x2f && ch < 0x3a) ret += ch - 0x30 + 1; // -47
temp += (((0x2f - ch) & (ch - 0x3a)) >> 8) & (ch - 47);
ch |= 0x20;
// if (ch > 0x60 && ch < 0x67) ret += ch - 0x61 + 10 + 1; // -86
temp += (((0x60 - ch) & (ch - 0x67)) >> 8) & (ch - 86);
t2 = temp << 4;
temp = -1;
ch = (unsigned char) old[2 * i + 1];
// if (ch > 0x2f && ch < 0x3a) ret += ch - 0x30 + 1; // -47
temp += (((0x2f - ch) & (ch - 0x3a)) >> 8) & (ch - 47);
ch |= 0x20;
// if (ch > 0x60 && ch < 0x67) ret += ch - 0x61 + 10 + 1; // -86
temp += (((0x60 - ch) & (ch - 0x67)) >> 8) & (ch - 86);
t2 |= temp;
if (0 != (t2 >> 8)) {
free(str);
return NULL;
}
str[i] = (char) t2 & 0xFF;
}
str[i] = '\0';
return str;
}
main()
{
char *t, *s = "fFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfF";
size_t len = sizeof("fFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfFfF");
int i;
double start, end;
start = get_time();
for (i = 0; i < 1000000; i++) {
t = php_hex2bin(s, len);
free(t);
}
end = get_time();
printf("PHP hex2bin In %f Seconds\r\n", end - start);
start = get_time();
for (i = 0; i < 1000000; i++) {
t = ts_hex2bin(s, len);
free(t);
}
end = get_time();
printf("TS hex2bin In %f Seconds\r\n", end - start);
}
@denisdemaisbr
Copy link

gcc version 11.2.0 (GCC)

PHP bin2hex In 0.846049 Seconds
TS bin2hex In 0.966055 Seconds
PHP hex2bin In 2.744157 Seconds
TS hex2bin In 4.578262 Seconds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment