-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include <iostream> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <time.h> | ||
#include <vector> | ||
#include <cassert> | ||
#include <fstream> | ||
#include <fcntl.h> | ||
#include <unistd.h> | ||
#include <filesystem> | ||
|
||
#include "primitives.hpp" | ||
#include "scoped_timer.hpp" | ||
|
||
int main() { | ||
u64 width = 10'000; | ||
u64 height = 10'000; | ||
u64 num_pixels = width * height; | ||
|
||
std::vector<u8> pixels{}; | ||
|
||
srand(u32(time(NULL))); | ||
|
||
{ | ||
scoped_timer<scoped_timer_unit::MILLISECONDS> timer("generating random pixel data", std::cout); | ||
|
||
pixels.reserve(num_pixels); | ||
|
||
for (u64 i = 0; i < num_pixels; ++i) | ||
pixels.push_back((uint8_t)rand()); | ||
} | ||
|
||
// FILE | ||
{ | ||
FILE *file = fopen("stdio_FILE.bin", "wb"); | ||
assert(file); | ||
{ | ||
scoped_timer<scoped_timer_unit::MICROSECONDS> timer("stdio FILE", std::cout); | ||
assert(fwrite(pixels.data(), pixels.size(), sizeof(u8), file)); | ||
} | ||
fclose(file); | ||
std::filesystem::remove("stdio_FILE.bin"); | ||
} | ||
|
||
// std::fstream | ||
{ | ||
std::fstream file("std_fstream.bin", std::ios::binary | std::ios::out); | ||
assert(file); | ||
{ | ||
scoped_timer<scoped_timer_unit::MICROSECONDS> timer("std::fstream", std::cout); | ||
file.write((char const *)pixels.data(), std::streamsize(pixels.size())); | ||
} | ||
std::filesystem::remove("std_fstream.bin"); | ||
} | ||
|
||
// std::fstream | ||
{ | ||
std::ofstream file("std_ofstream.bin", std::ios::binary); | ||
assert(file); | ||
{ | ||
scoped_timer<scoped_timer_unit::MICROSECONDS> timer("std::ofstream", std::cout); | ||
file.write((char const *)pixels.data(), std::streamsize(pixels.size())); | ||
} | ||
std::filesystem::remove("std_ofstream.bin"); | ||
} | ||
|
||
// linux write | ||
{ | ||
int fd = open("linux_write.bin", O_WRONLY | O_CREAT | O_TRUNC, 0644); | ||
assert(fd != -1); | ||
{ | ||
scoped_timer<scoped_timer_unit::MICROSECONDS> timer("linux write", std::cout); | ||
assert(write(fd, pixels.data(), pixels.size()) == i64(pixels.size())); | ||
} | ||
close(fd); | ||
std::filesystem::remove("linux_write.bin"); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#ifndef NLUKA_SCOPEDTIMER_HPP | ||
#define NLUKA_SCOPEDTIMER_HPP | ||
|
||
#include <chrono> | ||
#include <cstdint> | ||
#include <ostream> | ||
#include <string_view> | ||
|
||
namespace scoped_timer_unit | ||
{ | ||
typedef uint32_t value_type; | ||
|
||
value_type constexpr | ||
SECONDS = 1'000'000'000, | ||
MILLISECONDS = 1'000'000, | ||
MICROSECONDS = 1'000, | ||
NANOSECONDS = 1; | ||
}; | ||
|
||
template <scoped_timer_unit::value_type TimerUnit> | ||
class scoped_timer | ||
{ | ||
public: | ||
scoped_timer(char const *const label, std::ostream &os) | ||
: m_label{label}, | ||
m_os{os}, | ||
m_start{std::chrono::steady_clock::now()} | ||
{} | ||
|
||
scoped_timer(scoped_timer const &) = delete; // copy constructor | ||
scoped_timer &operator=(scoped_timer const &) = delete; // copy assignment | ||
scoped_timer(scoped_timer &&) noexcept = delete; // move constructor | ||
scoped_timer &operator=(scoped_timer &&) noexcept = delete; // move assignment | ||
|
||
~scoped_timer() { | ||
auto const end = std::chrono::steady_clock::now(); | ||
auto const elapsed_nanos = end - m_start; | ||
auto const elapsed_time_in_units = static_cast<double>(elapsed_nanos.count() / TimerUnit); | ||
|
||
char const *unit_cstr; | ||
switch (TimerUnit) { | ||
case scoped_timer_unit::SECONDS: unit_cstr = "s"; break; | ||
case scoped_timer_unit::MILLISECONDS: unit_cstr = "ms"; break; | ||
case scoped_timer_unit::MICROSECONDS: unit_cstr = "us"; break; | ||
case scoped_timer_unit::NANOSECONDS: unit_cstr = "ns"; break; | ||
default: unit_cstr = nullptr; break; | ||
} | ||
|
||
m_os << m_label << " took " << elapsed_time_in_units << unit_cstr << '\n'; | ||
} | ||
|
||
private: | ||
std::string_view const m_label; | ||
std::ostream &m_os; | ||
std::chrono::time_point<std::chrono::steady_clock> const m_start; | ||
}; | ||
|
||
#endif // NLUKA_SCOPEDTIMER_HPP |