3 ms·
When I run that C code, it takes 67 ms. But the following C++ code: #include <sys/mman.h> #include <unistd.h> #include <fcntl.h> #include <cstdlib> i
by ncmncm 4y ago
When I run that C code, it takes 67 ms. But the following C++ code:
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#include <cstdlib>
int main()
{
int fd = ::open("/tmp/numbers", O_RDONLY);
auto end = ::lseek(fd, 0, SEEK_END);
char const* in = (char*) ::mmap(
nullptr, end, PROT_READ, MAP_PRIVATE, fd, 0);
char const* inpos = in;
char* out = (char*) std::malloc(end), *outp = out;
for (int n = 0; inp != in + end; ++inp) {
if (*inp != '\n') {
n = n * 10 + (*inp - '0');
} else *outp++ = ((n & 7) + '0'), n = 0;
}
*outp++ = '\n';
::write(1, out, outp - out);
}
takes 61 ms. Go figure. I guess the realloc calls are costing you.
I am kind of surprised you have a machine that can run as slow as yours does. Is it RPI or something?