#include #include #include typedef unsigned long long u64; u64 GetTimeNS() { struct timespec time; clock_gettime(CLOCK_REALTIME, &time); return time.tv_sec*1000000000 + time.tv_nsec; } //returns amount of characters or -1 for error. Digits only, this does not handle - or + at the start int ParseInt(const char*sz, u64*out) { if (!sz) return -1; u64 val=0; //overflow is legal with unsigned int i=0; while (1) { char c = sz[i]; if ((unsigned)(c-'0') > 9u) break; u64 prevVal = val; val = val*10+c-'0'; if (val <= prevVal && val != 0) { return -1; } i++; } if (i == 0) return -1; *out = val; return i; } int main(int argc, char *argv[]) { int arraySize=4096; char*array = (char*)realloc(0, arraySize); FILE*f = fopen("/tmp/numbers", "rb"); if (f == 0) { return EXIT_FAILURE; } char buf[32]; int i=0; u64 getLineTime=0; for(; ; i++) { getLineTime -= GetTimeNS(); char*result = fgets(buf, sizeof(buf), f); getLineTime += GetTimeNS(); if (result == 0) { break; } if (i+1 == arraySize) { arraySize *= 2; array = (char*)realloc(array, arraySize); } //array[i] = (atoi(buf) & 7) + '0'; u64 value; ParseInt(buf, &value); array[i] = (char)(value & 7) + '0'; } array[i++] = '\n'; fwrite(array, 1, i, stdout); fprintf(stderr, "fgets time is %lldms\n", getLineTime/1000000); }