From 613bbe2d3e0717c0e2d89fb2a67395205a88b6e1 Mon Sep 17 00:00:00 2001 From: Calvin Morrison Date: Fri, 30 Aug 2013 14:24:23 -0400 Subject: avoid any switches, better performance --- count_nucleobases.c | 33 ++++++++------------------------- 1 file changed, 8 insertions(+), 25 deletions(-) diff --git a/count_nucleobases.c b/count_nucleobases.c index 0396ea6..d1a4f1c 100755 --- a/count_nucleobases.c +++ b/count_nucleobases.c @@ -5,10 +5,6 @@ #include int main(int argc, char **argv) { - long unsigned long a = 0; - long unsigned long c = 0; - long unsigned long g = 0; - long unsigned long t = 0; if(argc != 2) { printf("Please supply a filename, and only a filename\n"); @@ -21,33 +17,20 @@ int main(int argc, char **argv) { } char line[8192]; + long unsigned long counts[256]; while (fgets(line, 8192, fh) != NULL) { - int i = 0; - + unsigned int i = 0; for(i = 0; i < strlen(line); i++) { - switch(line[i]) { - case 'A': - case 'a': - a++; - break; - case 'C': - case 'c': - c++; - break; - case 'G': - case 'g': - g++; - break; - case 'T': - case 't': - t++; - break; - } + counts[line[i]]++; } } - printf("A:%llu\nC:%llu\nG:%llu\nT:%llu\n", a, c, g, t); + printf("A:%llu\nC:%llu\nG:%llu\nT:%llu\n", + counts['a'] + counts['A'], + counts['c'] + counts['C'], + counts['g'] + counts['G'], + counts['t'] + counts['T']); return EXIT_SUCCESS; } -- cgit v1.2.3