Shocking waste of time, and you've just ruined the portability. Even within the same architecture with another compiler. Why are you even suggesting assembly? This is a C forum, the ability to inline assembly makes no difference. Even if it does, it's for the worse.
"How could I possibly slow down a program with >40 bytes."
Easy, you could be using registers that the compiler was going to use for optimisations. Just one of many ways. Surely you mean less than 40 bytes. And considering the slowest part of this problem is reading the file from disk, why are you bothering?
There is nothing wrong with:
Code:
#include <stdio.h>
#include <string.h>
struct position
{
int row,
column;
};
int find_character(FILE * fp, const char ch, struct position * pos);
int main(void)
{
struct position pos;
FILE * fp = fopen("test.dat", "r");
if(find_character(fp, 'E', &pos) == 0)
printf("Found 'E' at %d:%d\n", pos.row, pos.column);
else
printf("'E' not found.\n");
fclose(fp);
return 0;
}
/* returns non-zero on error */
int find_character(FILE * fp, const char ch, struct position * pos)
{
char buffer[BUFSIZ];
char * search = NULL;
int line = 0;
while(!search && fgets(buffer, sizeof buffer, fp))
{
search = strchr(buffer, ch);
++line;
}
/* found it */
if(search)
{
pos->row = line;
pos->column = (search - buffer) + 1;
return 0;
}
return 1;
}
And of course, the test:
Code:
zac@breeze:benchmark (0) $ gcc -O2 standard.c -o standard
zac@breeze:benchmark (0) $ time ./standard
Found 'E' at 15238266:3
real 0m3.961s
user 0m3.783s
sys 0m0.120s
zac@breeze:benchmark (0) $ time ./standard
Found 'E' at 15238266:3
real 0m4.033s
user 0m3.846s
sys 0m0.147s
zac@breeze:benchmark (0) $ time ./standard
Found 'E' at 15238266:3
real 0m3.865s
user 0m3.770s
sys 0m0.097s
zac@breeze:benchmark (0) $ uname -a
Linux breeze 2.6.31-ARCH #1 SMP PREEMPT Tue Nov 10 19:48:17 CET 2009 i686 Intel(R) Atom(TM) CPU N270 @ 1.60GHz GenuineIntel GNU/Linux
Run on a test.dat:
Code:
zac@breeze:benchmark (0) $ du -s test.dat
104280 test.dat
zac@breeze:benchmark (0) $ head -n 10 test.dat
ABC135
ABC135
ABC135
ABC135
ABC135
ABC135
ABC135
ABC135
ABC135
ABC135
...
With line 15238266 containing an 'E' at column 3.
And this computer isn't especially fast (being a netbook with a 5200RPM hard drive). I only have gcc, so I can't bench yours
An idea of the disk speed:
Code:
root@breeze:benchmark # hdparm -tT /dev/sda
/dev/sda:
Timing cached reads: 1106 MB in 2.00 seconds = 552.89 MB/sec
Timing buffered disk reads: 160 MB in 3.03 seconds = 52.77 MB/sec