Skip to content

Instantly share code, notes, and snippets.

@marcusmueller
Created September 3, 2025 13:47
Show Gist options
  • Select an option

  • Save marcusmueller/412faa340ebdddf2489bbf2e9f222ad3 to your computer and use it in GitHub Desktop.

Select an option

Save marcusmueller/412faa340ebdddf2489bbf2e9f222ad3 to your computer and use it in GitHub Desktop.
/*
* Copyright 2025 Marcus Müller
* SPDX-license-identifier: EUPL-1.2
*
* Needs C++17 or later, and fmt, which you should easily be able to install from your
* package manager.
*/
#include <fcntl.h>
#include <fmt/chrono.h>
#include <fmt/format.h>
#include <unistd.h>
#include <chrono>
constexpr unsigned int HOWMANY = 1'000'000;
constexpr unsigned int MAX_READ_SIZE = 16;
int main(int argc, char* argv[])
{
std::string fname = "/dev/zero";
if (argc > 1) {
fname = argv[1];
}
int fd = open(fname.c_str(), O_RDONLY);
auto start = std::chrono::high_resolution_clock::now();
char readbuffer[MAX_READ_SIZE];
int readlength = 0;
for (auto counter = HOWMANY; counter; --counter) {
readlength = read(fd, &readbuffer, MAX_READ_SIZE);
if (readlength < 1) {
return -1;
}
lseek(fd, 0, SEEK_SET);
}
auto end = std::chrono::high_resolution_clock::now();
auto diff = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
fmt::print("{} per read()\n", diff / HOWMANY);
fmt::print("{} last output\n", std::string(&readbuffer[0], readlength));
}
@hueychen27
Copy link

First person to see use the EUPL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment