This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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 , |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 */ | |
| ... |