diff options
author | Calvin Morrison <mutantturkey@gmail.com> | 2013-08-30 14:24:23 -0400 |
---|---|---|
committer | Calvin Morrison <mutantturkey@gmail.com> | 2013-08-30 14:24:23 -0400 |
commit | 613bbe2d3e0717c0e2d89fb2a67395205a88b6e1 (patch) | |
tree | 8b2589c398cfaa4847747c8b11c65a27787d73a2 | |
parent | adaddd745b9753a83b4eab82b9e4ef3baf731002 (diff) |
avoid any switches, better performance
-rwxr-xr-x | count_nucleobases.c | 33 |
1 files 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 <stdint.h> 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; } |