Crypto backtesting
Loading...
Searching...
No Matches
file.cxx File Reference
#include "utils.h"
#include <algorithm>
#include <benchmark/benchmark.h>
#include <cassert>
#include <charconv>
#include <fstream>
#include <ranges>
#include <sstream>
#include <vector>
Include dependency graph for file.cxx:

Go to the source code of this file.

Functions

std::string file_read (const std::string_view file_name)
 Open a file a return a string of the contents.
void BM_split_on_comma (benchmark::State &state)
 BENCHMARK (BM_split_on_comma)
void BM_split_on_comma2 (benchmark::State &state)
 BENCHMARK (BM_split_on_comma2)
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.
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.
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.

Variables

const auto split_on_comma
 Take a row of data, split on delimiter and return a row of floating points.
const auto split_on_comma2
 Take a row of data, split on delimiter and return a row of floating points.

Function Documentation

◆ BENCHMARK() [1/2]

BENCHMARK ( BM_split_on_comma )
Here is the call graph for this function:

◆ BENCHMARK() [2/2]

BENCHMARK ( BM_split_on_comma2 )
Here is the call graph for this function:

◆ BM_split_on_comma()

void BM_split_on_comma ( benchmark::State & state)

Definition at line 46 of file file.cxx.

46 {
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}
const auto split_on_comma
Take a row of data, split on delimiter and return a row of floating points.
Definition file.cxx:36
const std::vector< std::vector< double > > xs
An example of a full set of price data.
Definition main.cxx:13
Here is the caller graph for this function:

◆ BM_split_on_comma2()

void BM_split_on_comma2 ( benchmark::State & state)

Definition at line 67 of file file.cxx.

67 {
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}
const auto split_on_comma2
Take a row of data, split on delimiter and return a row of floating points.
Definition file.cxx:57
Here is the caller graph for this function:

◆ file_read()

std::string file_read ( const std::string_view file_name)

Open a file a return a string of the contents.

Open file and return the contents as a string

Definition at line 12 of file file.cxx.

12 {
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}
Here is the caller graph for this function:

◆ to_series()

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 at line 78 of file file.cxx.

78 {
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};
Here is the caller graph for this function:

◆ to_series3()

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 at line 90 of file file.cxx.

90 {
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};
constexpr auto cells
Number of cells in a row of price data.
Definition constants.h:23
Here is the caller graph for this function:

◆ to_series4()

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 at line 119 of file file.cxx.

119 {
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};
Here is the caller graph for this function:

Variable Documentation

◆ split_on_comma

const auto split_on_comma
Initial value:
= [](auto &row) {
using namespace std::views;
return row | split(',') | transform([](auto &&val) -> double {
auto str = std::string{cbegin(val), cend(val)};
double value{};
std::from_chars(str.data(), str.data() + str.size(), value);
return value;
});
}

Take a row of data, split on delimiter and return a row of floating points.

Definition at line 36 of file file.cxx.

36 {
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};

◆ split_on_comma2

const auto split_on_comma2
Initial value:
= [](auto &&row) {
using namespace std::views;
return row | split(',') | transform([](auto &&val) {
auto &&str = std::string{cbegin(val), cend(val)};
auto value = double{};
std::from_chars(str.data(), str.data() + str.size(), value);
return value;
});
}

Take a row of data, split on delimiter and return a row of floating points.

Definition at line 57 of file file.cxx.

57 {
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};