commit 0d9d0a6d82aa302b9191fbeabb4e972554e9673f
parent 8b2996b83953d4a1a973a01ed848cd03812b3509
Author: David Freifeld <freifeld.david@gmail.com>
Date: Sun, 20 Sep 2020 21:21:58 -0700
Fileread continues to be a not straightforward microoptimization that will ultimately end up being a waste of time and make me sad
AVX calls p2 :(
Diffstat:
1 file changed, 16 insertions(+), 18 deletions(-)
diff --git a/src/experimental/fileread.cpp b/src/experimental/fileread.cpp
@@ -14,6 +14,7 @@
#include <ctime>
#include <chrono>
#include <iostream>
+#include <cmath>
#include <stdint.h>
// No safety checks whatsoever. Use at your own risk.
@@ -27,7 +28,7 @@
int main () {
auto start = std::chrono::high_resolution_clock::now();
static const auto BUFFER_SIZE = 16*1024;
- int fd = open("../../data_banknote_authentication.txt", O_RDONLY);
+ int fd = open("../../data_banknote_authentication.txt", O_RDONLY & O_NONBLOCK);
if(fd == -1) {
printf("fd == -1\n");
return 1;
@@ -39,6 +40,7 @@ int main () {
char buf[BUFFER_SIZE + 1];
uintmax_t lines = 0;
float tmp;
+ int bufs = 0;
while(size_t bytes_read = read(fd, buf, BUFFER_SIZE))
{
if(bytes_read == (size_t)-1) {
@@ -46,28 +48,22 @@ int main () {
return 1;
}
if (!bytes_read) break;
- 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;
- if (bound < 0) break; // Stop.
- // printf("%p (%p + %ld) vs %p\n", p+bound, p, bound, buf+BUFFER_SIZE);
+ for(char *p = buf;; ++p) {
+ char* bound = (char*) memchr(p+1, '\n', (buf + bytes_read) - p+1);
+ if (bound - p < 0) break; // Stop.
tmp = strtod(p, NULL); // Read first float
- //int delims = 0;
- // printf("%f\n", tmp);
- for (int i = 0; i < bound; i++) {
- if (*(p+i) == ',') {
- tmp = strtod(p+i+1, NULL); // Read float following delimiter
- //printf("%f\n", tmp);
- // delims++;
- // printf("Comma: %f %d\n", tmp, delims);
+ for (; p < bound; ++p) {
+ if (*p == ',') {
+ tmp = strtod(p, NULL); // Read float following delimiter
}
}
++lines;
}
-
+ bufs++;
}
- printf("Final %f\n", tmp);
+ printf("Final %f, %i bufs\n", tmp, bufs);
auto end = std::chrono::high_resolution_clock::now();
- double time = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
+ double time = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count() / pow(10,9);
auto f_start = std::chrono::high_resolution_clock::now();
int flines = 0;
@@ -86,7 +82,9 @@ int main () {
}
auto f_end = std::chrono::high_resolution_clock::now();
if (flines == lines) std::cout << "Linecount is valid!" << "\n";
+ else std::cout << "WARN: INVALID LINECOUNT " << lines << " vs. " << flines << "\n";
if (ftmp == tmp) std::cout << "Final value read is valid!" << "\n";
- double ftime = std::chrono::duration_cast<std::chrono::nanoseconds>(f_end - f_start).count();
- std::cout << ftime << " " << time << " " << ftime/time << "\n";
+ else std::cout << "WARN: INVALID LAST READ VAL " << tmp << " vs. " << ftmp << "\n";
+ double ftime = std::chrono::duration_cast<std::chrono::nanoseconds>(f_end - f_start).count() / pow(10,9);
+ std::cout << ftime << " " << time << " so " << ftime/time << "% speedup\n";
}