Skip to content

Instantly share code, notes, and snippets.

@karthick18
Created May 30, 2010 23:04
Show Gist options
  • Save karthick18/419378 to your computer and use it in GitHub Desktop.
Save karthick18/419378 to your computer and use it in GitHub Desktop.
#define _GNU_SOURCE /*for CPU affinity macros/routine*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
#include <sched.h>
/*
* poor mans profiler. cpu tsc for x86 though would be skewed with cpufreq sched,etc.
* but still better for approximations.
*/
#define rdtsc(x) do { asm __volatile__("rdtsc":"=A"(x)::); }while(0)
/*
* flush instr. cacheline before rdtsc
*/
#define barrier() do { asm __volatile__("cpuid":::"memory"); }while(0)
#ifdef STR
#define vowel_count(s) vowel_count_slow(s)
int vowel_count_slow(const char *s)
{
const char *vowel = "aeiou";
int c = 0;
while(*s)
c += strchr(vowel, tolower(*s++)) ? 1 : 0;
return c;
}
#else
#define vowel_count(s) vowel_count_fast(s)
/*
* non-static to make it thread-safe, otherwise even faster
* its faster to load complimentary set with 0's coz of a rep; stosb. zeroing off 256 elements below.
* otherwise we can set the map[ 0 ... 255 ] = &unused and have the map increment unconditionally
*/
int vowel_count_fast(const char *s)
{
int c = 0,t;
int *map[256] = { [0 ... 255]=0,['a']=&c,['e']=&c,['i']=&c,['o']=&c,['u']=&c,
['A']=&c,['E']=&c,['I']=&c,['O']=&c,['U']=&c};
while((t=*s++)) map[t] ? ++*map[t] : 0;
return c;
}
#endif
int main(int x,char **y) {
cpu_set_t set;
long long A=0,B=0;
x == 2 ? 0 : exit(0);
/* lock proc. to cpu 0 for reasonably accurate rdtsc though cpufreq could skew it in which case
* clock_gettime(CLOCK_MONOTONIC, &ts); should be okay
*/
CPU_ZERO(&set);
CPU_SET(0, &set);
assert(sched_setaffinity(0, sizeof set, &set) == 0);
barrier();
rdtsc(A);
vowel_count(y[1]);
rdtsc(B);
printf("s = [%s], vowels = [%d], cpu ticks = [%lld]\n", y[1], vowel_count(y[1]), B - A);
return 0;
}
/*
* Local variables:
* c-file-style: "linux"
* c-basic-offset: 4
* tab-width: 4
* compile-command: "gcc -Wall -g -o vowel vowel.c"
* End:
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment