commit a6fd46e0f458d0deb3160c54a240f03e37911021
parent 1c820db25f3367af48b601704371484eaa0821da
Author: David Freifeld <freifeld.david@gmail.com>
Date: Sun, 20 Sep 2020 13:51:51 -0700
Headway into faster file read + parse
Diffstat:
1 file changed, 25 insertions(+), 3 deletions(-)
diff --git a/src/experimental/fileread.cpp b/src/experimental/fileread.cpp
@@ -14,6 +14,7 @@
#include <ctime>
#include <chrono>
#include <iostream>
+#include <stdint.h>
int main (){
auto start = std::chrono::high_resolution_clock::now();
@@ -26,10 +27,10 @@ int main (){
/* Advise the kernel of our access pattern. */
// posix_fadvise(fd, 0, 0, 1); // FDADVICE_SEQUENTIAL
-
+
char buf[BUFFER_SIZE + 1];
uintmax_t lines = 0;
-
+ float tmp;
while(size_t bytes_read = read(fd, buf, BUFFER_SIZE))
{
if(bytes_read == (size_t)-1) {
@@ -37,10 +38,21 @@ int main (){
return 1;
}
if (!bytes_read) break;
+ int prev = 0;
for(char *p = buf; (p = (char*) memchr(p, '\n', (buf + bytes_read) - p)); ++p) {
+ long bound = (char*) memchr(p+1, '\n', (buf + bytes_read) - p+1) - p;
+ tmp = strtod(p, NULL);
+ for (int i = 0; i < bound; i++) {
+ if (*(p+i) == ',') {
+ tmp = strtod(p+i+1, NULL);
+ //printf("Comma %f\n", tmp);
+ }
+ // printf("%c", *(p+i));
+ }
++lines;
}
}
+ printf("Final %f\n", tmp);
auto end = std::chrono::high_resolution_clock::now();
double time = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
@@ -48,9 +60,19 @@ int main (){
int flines = 0;
FILE* fptr = fopen("../../data_banknote_authentication.txt", "r");
char fbuf[1024];
- while (fgets(fbuf, 1024, fptr) != NULL) flines++;
+ float ftmp;
+ while (fgets(fbuf, 1024, fptr) != NULL) {
+ char *p;
+ p = strtok(fbuf,",");
+ for (int j = 0; j < 4; j++) {
+ ftmp = strtod(p, NULL);
+ p = strtok(NULL,",");
+ }
+ flines++;
+ }
auto f_end = std::chrono::high_resolution_clock::now();
std::cout << flines << " " << lines << "\n";
+ std::cout << ftmp << " " << tmp << "\n";
double ftime = std::chrono::duration_cast<std::chrono::nanoseconds>(f_end - f_start).count();
std::cout << ftime << " " << time << " " << ftime/time << "\n";
}