Skip to content

Instantly share code, notes, and snippets.

View benwills's full-sized avatar

Ben Wills benwills

View GitHub Profile
unsigned short timestamp = <some value>; // Bits: DDDDDHHHHHMMMMMM
int day = (timestamp >> 11) & 0x1F;
int hour = (timestamp >> 6) & 0x1F;
int min = (timestamp) & 0x3F;
unsigned short dup_timestamp = (short)((day << 11) | (hour << 6) | min);
// Alternative using macros
@benwills
benwills / chomp-line.c
Created December 27, 2014 09:29
Chomp Line in C
//https://github.com/bitly/dablooms/blob/master/src/test_dablooms.c
static void chomp_line(char *word)
{
char *p;
if ((p = strchr(word, '\r'))) {
*p = '\0';
}
if ((p = strchr(word, '\n'))) {
*p = '\0';
@benwills
benwills / timer.c
Created December 29, 2014 16:38
Simple Timer in C
int i;
float tStart, tDiff;
uint32_t runs = 1000;
tStart = (float)clock()/CLOCKS_PER_SEC;
for ( i=0; i<runs; ++i ) {
func();
}
tDiff = (float)clock()/CLOCKS_PER_SEC - tStart;
@benwills
benwills / faster_tolower.c
Last active October 11, 2019 07:33
Faster Letter Comparison and Lowercasing in C
#include <time.h>
#include <stdint.h>
#include <sys/types.h>
#include <stdio.h>
#include <ctype.h>
/*
Character-by-character comparisons for equality and lowercasing.
Custom functions vs tolower.
Spoiler: lookup table is the fastest.
@benwills
benwills / create_lkp_bool.c
Last active August 29, 2015 14:12
Quickly Create a Binary Lookup Table
#include <stdio.h>
#include <string.h>
//
// Create entries for a boolean lookup table.
//
// Example output from list[], below:
// 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
// 0,1,0,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,
// 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,
@benwills
benwills / create_cnv_ascii.c
Created December 30, 2014 12:18
Create a Lookup Table for ascii Conversions
#include <stdio.h>
#include <string.h>
//
// Create entries for a boolean lookup table to be used for character conversions.
//
// Example output from list[], below:
// 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
// 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
// 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , '-', '.', 0 ,
// core_id = 0, 1, ... n-1, where n is the system's number of cores
int stick_this_thread_to_core(int core_id) {
int num_cores = sysconf(_SC_NPROCESSORS_ONLN);
if (core_id < 0 || core_id >= num_cores)
return EINVAL;
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(core_id, &cpuset);
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
unsigned
input = 0b0111u,
n_bits = 4u,
*bits = (unsigned*)malloc(sizeof(unsigned) * n_bits),
bit = 0;
@benwills
benwills / latency.txt
Last active August 29, 2015 14:15 — forked from jboner/latency.txt
Latency Comparison Numbers
--------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns
Send 1K bytes over 1 Gbps network 10,000 ns 0.01 ms
Read 4K randomly from SSD* 150,000 ns 0.15 ms
char buf[BUFSIZ];
FILE *fp;
if ((fp= popen("/bin/php -f /webserver/pages/test.php", "r")) != NULL)
{
/* Read from the PHP command output */
while (fgets(buf, BUFSIZ, fp) != NULL)
{
/* Process the output from PHP as desired */
...