Pomale fgetc()?

Jan Kasprzak kas na fi.muni.cz
Úterý Červenec 28 21:40:16 CEST 2009


	Dobry den,

netusite nekdo, co dela standardni knihovna za kouzla, ze je to tak
pomale? Kdyz chci zpracovavat soubor po bajtech, (pres fgetc()),
nactu 360 MB velky soubor za 3.10s user a 0.12s sys. Kdyz misto toho
fgets() podstrcim svoji trivialni implementaci (zkompilovanou zvlast
do samostatneho .o, aby to kompilator nezkousel prilis inlinovat
nebo jinak "nemistne" optimalizovat), je totez za 1.2s user a 0.12s sys.
Zamozrejme, neni to kompletni implementace (misto statickych
promennych bych musel mit svoji variantu struktury FILE, ale i tak:

#include <stdio.h>
#include <unistd.h>

#define BUFLEN 4096

int fgc(FILE *f)
{
        static unsigned char buffer[BUFLEN];
        static unsigned pos = 0;
        static unsigned len = 0;

        if (!len) {
                if ((len = read(fileno(f), buffer, BUFLEN)) <= 0) {
                        return -1;
                }
                pos = 0;
        }

        len--;
        return buffer[pos++];
}

-Yenya

-- 
| Jan "Yenya" Kasprzak  <kas at {fi.muni.cz - work | yenya.net - private}> |
| GPG: ID 1024/D3498839      Fingerprint 0D99A7FB206605D7 8B35FCDE05B18A5E |
| http://www.fi.muni.cz/~kas/    Journal: http://www.fi.muni.cz/~kas/blog/ |
Please don't top post and in particular don't attach entire digests to your
mail or we'll all soon be using bittorrent to read the list.     --Alan Cox



Další informace o konferenci Linux