3 ms·
Depending on your platform (os, hardware, compiler) you can even do something silly like write packed structs and then write those to disk (or send them over so
by rdtsc 11y ago
Depending on your platform (os, hardware, compiler) you can even do something silly like write packed structs and then write those to disk (or send them over sockets) and cast back.
Mostly it works if you know the hardware and compiler will stay the same (as in packed struct representation will stay the same). It is basically a poor man's Cap'n'Proto or Flatbuffers.
As for speed, buffering strategy (size and how often fsync is done) will often dominate other stuff. I remember doing a benchmark between C++ and Python for loading and saving data. Python won hands down. The reason was because it had better buffer size defaults. There is also trickery and configuration involved in your page cache params are setup and the schedule of how dirty pages are flushed (if you don't fsync).
> If you’re interested in how fast using read(2) is, it’s essentially the same speed: 5437341 ns if I’m reading directly into the std::vector.
Yap, mmap is not always better, it is more complicated and depending on access pattern not always worth it.
- kabdib 11y agoAlso, the quality of the library providing stdio or streams can vary quite a bit. I know a few platforms where you're much better off never calling this stuff (unless it's for quick bringup or other throwaway code) because the folks writing that layer obviously hated their day jobs and wanted to write an OS instead . . . so they did.