Created
September 3, 2025 13:47
-
-
Save marcusmueller/412faa340ebdddf2489bbf2e9f222ad3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * 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)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First person to see use the EUPL.