Crypto backtesting
Loading...
Searching...
No Matches
file.cxx
Go to the documentation of this file.
1#include "utils.h"
2#include <algorithm>
3#include <benchmark/benchmark.h>
4#include <cassert>
5#include <charconv>
6#include <fstream>
7#include <ranges>
8#include <sstream>
9#include <vector>
10
11/// Open a file a return a string of the contents
12std::string file_read(const std::string_view file_name) {
13
14 /// Open file and return the contents as a string
15 std::ifstream in{std::string{file_name}};
16
17 if (not in.good())
18 return {};
19
20 // Run to the end
21 in.seekg(0, std::ios::end);
22
23 // Reserve enough storage for the file contents
24 std::string out(in.tellg(), '\0');
25
26 // Back to the start
27 in.seekg(0, std::ios::beg);
28
29 // Copy the file contents
30 in.read(out.data(), out.size());
31
32 return out;
33}
34
35/// Take a row of data, split on delimiter and return a row of floating points
36const auto split_on_comma = [](auto &row) {
37 using namespace std::views;
38 return row | split(',') | transform([](auto &&val) -> double {
39 auto str = std::string{cbegin(val), cend(val)};
40 double value{};
41 std::from_chars(str.data(), str.data() + str.size(), value);
42 return value;
43 });
44};
45
46void BM_split_on_comma(benchmark::State &state) {
47 auto &&row = std::string{"1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0"};
48 for (auto _ : state) {
49 auto &&xs = split_on_comma(row);
50 benchmark::DoNotOptimize(xs);
51 }
52}
53
55
56/// Take a row of data, split on delimiter and return a row of floating points
57const auto split_on_comma2 = [](auto &&row) {
58 using namespace std::views;
59 return row | split(',') | transform([](auto &&val) {
60 auto &&str = std::string{cbegin(val), cend(val)};
61 auto value = double{};
62 std::from_chars(str.data(), str.data() + str.size(), value);
63 return value;
64 });
65};
66
67void BM_split_on_comma2(benchmark::State &state) {
68 auto &&row = std::string{"1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0"};
69 for (auto _ : state) {
70 auto &&xs = split_on_comma2(row);
71 benchmark::DoNotOptimize(xs);
72 }
73}
74
76
77/// Open a CSV file and return a vector of floating points for each row
78std::vector<std::vector<double>> to_series(const std::string csv) {
79
80 // Read line by line
81 std::istringstream in{csv};
82 std::ranges::istream_view<std::string> rows(in);
83
84 // Read prices, drop the heading, and split each row
85 return rows | std::views::drop(1) | std::views::transform(split_on_comma)
86 | std::ranges::to<std::vector<std::vector<double>>>();
87};
88
89/// Open a CSV file and return a vector of floating points for each row
90std::vector<std::vector<double>> to_series3(const std::string &csv) {
91
92 // Read line by line
93 std::istringstream in{csv};
94 std::ranges::istream_view<std::string> rows(in);
95
96 // Read prices, drop the heading, and split each row
97 auto &&px =
98 rows | std::views::drop(1) | std::views::transform(split_on_comma2);
99
100 // Create a default row to populate
101 auto &&xs = std::vector<double>(fx::cells);
102
103 // Evaluate the view by copying into a vector
104 auto &&series = std::vector<std::vector<double>>{};
105
106 for (auto v : px) {
107
108 // Copy the individual prices
109 std::ranges::copy(v, std::begin(xs));
110
111 // And store the full complement
112 series.emplace_back(xs);
113 }
114
115 return std::move(series);
116};
117
118/// Open a CSV file and return a vector of floating points for each row
119std::vector<std::vector<double>> to_series4(const std::string &csv) {
120
121 // Read line by line
122 std::istringstream in{csv};
123 std::ranges::istream_view<std::string> rows(in);
124
125 // Read prices, drop the heading, and split each row
126 auto &&px =
127 rows | std::views::drop(1) | std::views::transform(split_on_comma2);
128
129 // Evaluate the view by copying into a vector
130 auto &&series = std::vector<std::vector<double>>{};
131 series.reserve(std::ranges::distance(px));
132
133 std::ranges::transform(px, std::back_inserter(series), [&](auto &&v) {
134 // Copy the individual prices
135 auto &&xs = std::vector<double>(fx::cells);
136 std::ranges::copy(v, std::begin(xs));
137 return xs;
138 });
139
140 return series;
141};
void BM_split_on_comma2(benchmark::State &state)
Definition file.cxx:67
BENCHMARK(BM_split_on_comma)
const auto split_on_comma2
Take a row of data, split on delimiter and return a row of floating points.
Definition file.cxx:57
const auto split_on_comma
Take a row of data, split on delimiter and return a row of floating points.
Definition file.cxx:36
std::vector< std::vector< double > > to_series3(const std::string &csv)
Open a CSV file and return a vector of floating points for each row.
Definition file.cxx:90
std::string file_read(const std::string_view file_name)
Open a file a return a string of the contents.
Definition file.cxx:12
void BM_split_on_comma(benchmark::State &state)
Definition file.cxx:46
std::vector< std::vector< double > > to_series(const std::string csv)
Open a CSV file and return a vector of floating points for each row.
Definition file.cxx:78
std::vector< std::vector< double > > to_series4(const std::string &csv)
Open a CSV file and return a vector of floating points for each row.
Definition file.cxx:119
constexpr auto cells
Number of cells in a row of price data.
Definition constants.h:23