Zip and Skip Tries
Loading...
Searching...
No Matches
data.cpp
Go to the documentation of this file.
1
5#include "data.hpp"
6
7#include <fstream>
8#include <string>
9#include <iostream> // Required for std::endl used indirectly by save_* functions
10
11void save_search_data(const std::string& method, size_t n, size_t m, size_t l, size_t num_nanoseconds, size_t num_repetitions, size_t min_par_compare, bool is_genetic)
12{
13 // Construct the full path and filename for the CSV data file.
15 std::string filename = PREFIX + method + CSV_EXTENSION;
16
17 // Open the file in append mode. Creates the file if it doesn't exist.
18 std::ofstream file(filename, std::ios::app);
19 // Write the data as a single line, comma-separated.
20 file << n << "," << m << "," << l << "," << num_nanoseconds << "," << num_repetitions << "," << min_par_compare << std::endl;
21 // File is automatically closed when 'file' goes out of scope.
22}
23
24void save_construction_data(const std::string& method, size_t n, size_t m, size_t l, size_t num_nanoseconds, size_t num_repetitions, size_t min_par_compare, bool is_genetic)
25{
26 // Construct the full path and filename for the CSV data file.
28 std::string filename = PREFIX + method + CSV_EXTENSION;
29
30 // Open the file in append mode. Creates the file if it doesn't exist.
31 std::ofstream file(filename, std::ios::app);
32 // Write the data as a single line, comma-separated.
33 file << n << "," << m << "," << l << "," << num_nanoseconds << "," << num_repetitions << "," << min_par_compare << std::endl;
34 // File is automatically closed when 'file' goes out of scope.
35}
36
37void save_removal_data(const std::string& method, size_t n, size_t m, size_t l, size_t num_nanoseconds, size_t num_repetitions, size_t min_par_compare, bool is_genetic)
38{
39 // Construct the full path and filename for the CSV data file.
41 std::string filename = PREFIX + method + CSV_EXTENSION;
42
43 // Open the file in append mode. Creates the file if it doesn't exist.
44 std::ofstream file(filename, std::ios::app);
45 // Write the data as a single line, comma-separated.
46 file << n << "," << m << "," << l << "," << num_nanoseconds << "," << num_repetitions << "," << min_par_compare << std::endl;
47 // File is automatically closed when 'file' goes out of scope.
48}
49
50std::string get_hostname()
51{
52 std::string hostname;
53 // Attempt to open the hostname file provided by the kernel (Linux-specific).
54 std::ifstream file("/proc/sys/kernel/hostname");
55 if (file.is_open()) // Check if the file was successfully opened
56 {
57 // Read the entire line (hostname) from the file.
58 std::getline(file, hostname);
59 // File is automatically closed when 'file' goes out of scope.
60 }
61 // Return the retrieved hostname (or an empty string if reading failed).
62 return hostname;
63}
64
65std::string pretty_print(size_t nanoseconds)
66{
67 size_t milliseconds = nanoseconds / 1e6;
68 size_t seconds = nanoseconds / 1e9;
69 size_t minutes = seconds / 60;
70 size_t hours = minutes / 60;
71
72 // Build the output string based on the largest significant unit.
73 if (hours > 0)
74 {
75 // Format as h, m, s, ms
76 return std::to_string(hours) + "h " + std::to_string(minutes % 60) + "m " + std::to_string(seconds % 60) + "s " + std::to_string(milliseconds % 1000) + "ms";
77 }
78 else if (minutes > 0)
79 {
80 // Format as m, s, ms
81 return std::to_string(minutes) + "m " + std::to_string(seconds % 60) + "s " + std::to_string(milliseconds % 1000) + "ms";
82 }
83 else if (seconds > 0)
84 {
85 // Format as s, ms
86 return std::to_string(seconds) + "s " + std::to_string(milliseconds % 1000) + "ms";
87 }
88 else // Only milliseconds or less
89 {
90 // Format as ms
91 return std::to_string(milliseconds) + "ms";
92 }
93}
T * alloc_to_device(size_t size)
Allocates memory on the CUDA device.
void save_search_data(const std::string &method, size_t n, size_t m, size_t l, size_t num_nanoseconds, size_t num_repetitions, size_t min_par_compare, bool is_genetic)
Saves search performance data to a CSV file.
Definition data.cpp:11
void save_construction_data(const std::string &method, size_t n, size_t m, size_t l, size_t num_nanoseconds, size_t num_repetitions, size_t min_par_compare, bool is_genetic)
Saves construction performance data to a CSV file.
Definition data.cpp:24
std::string get_hostname()
Retrieves the hostname of the machine executing the code.
Definition data.cpp:50
void save_removal_data(const std::string &method, size_t n, size_t m, size_t l, size_t num_nanoseconds, size_t num_repetitions, size_t min_par_compare, bool is_genetic)
Saves removal performance data to a CSV file.
Definition data.cpp:37
std::string pretty_print(size_t nanoseconds)
Converts a duration in nanoseconds to a human-readable string format.
Definition data.cpp:65
Defines utilities for managing performance data logging and timing.
static const std::string REMOVAL_DATA_FILENAME
Base filename prefix for removal performance data files.
Definition data.hpp:27
static const std::string CSV_EXTENSION
Standard file extension for Comma Separated Value files.
Definition data.hpp:29
static const std::string SEARCH_DATA_FILENAME
Base filename prefix for search performance data files.
Definition data.hpp:23
static const std::string CONSTRUCTION_DATA_FILENAME
Base filename prefix for construction performance data files.
Definition data.hpp:25
static const std::string HOSTNAME
Constant string storing the hostname, retrieved once at program startup.
Definition data.hpp:99
const std::string & get_data_directory(bool is_genetic=false)
Gets the appropriate data directory path based on the data type.
Definition data.hpp:36